Linux: remove duplicate strings from for and cut
So I have a file structure that looks like this:
- video
- abc.avi
- party_2014.h264
- test.avi
- xxx.mkv
I need to create a directory for each extension and move the corresponding file there so it looks like this
- video
- video-avi
- abc.avi
- test.avi
- video-h264
- party_2014.h264
- video-mkv
- xxx.mkv
- video-avi
so far I have this code:
for i in $(find video -name "*.*" -print)
do cut -d'.' -f2 <<< "$i" | uniq -c;
done
this will return duplicate avi so uniq doesn't seem to be working, how to remove duplicates properly?
linux bash for-loop find
add a comment |
So I have a file structure that looks like this:
- video
- abc.avi
- party_2014.h264
- test.avi
- xxx.mkv
I need to create a directory for each extension and move the corresponding file there so it looks like this
- video
- video-avi
- abc.avi
- test.avi
- video-h264
- party_2014.h264
- video-mkv
- xxx.mkv
- video-avi
so far I have this code:
for i in $(find video -name "*.*" -print)
do cut -d'.' -f2 <<< "$i" | uniq -c;
done
this will return duplicate avi so uniq doesn't seem to be working, how to remove duplicates properly?
linux bash for-loop find
add a comment |
So I have a file structure that looks like this:
- video
- abc.avi
- party_2014.h264
- test.avi
- xxx.mkv
I need to create a directory for each extension and move the corresponding file there so it looks like this
- video
- video-avi
- abc.avi
- test.avi
- video-h264
- party_2014.h264
- video-mkv
- xxx.mkv
- video-avi
so far I have this code:
for i in $(find video -name "*.*" -print)
do cut -d'.' -f2 <<< "$i" | uniq -c;
done
this will return duplicate avi so uniq doesn't seem to be working, how to remove duplicates properly?
linux bash for-loop find
So I have a file structure that looks like this:
- video
- abc.avi
- party_2014.h264
- test.avi
- xxx.mkv
I need to create a directory for each extension and move the corresponding file there so it looks like this
- video
- video-avi
- abc.avi
- test.avi
- video-h264
- party_2014.h264
- video-mkv
- xxx.mkv
- video-avi
so far I have this code:
for i in $(find video -name "*.*" -print)
do cut -d'.' -f2 <<< "$i" | uniq -c;
done
this will return duplicate avi so uniq doesn't seem to be working, how to remove duplicates properly?
linux bash for-loop find
linux bash for-loop find
asked Jan 2 at 19:02
Lada1208Lada1208
274
274
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
This simple script might work for you:
#!/usr/bin/env bash
shopt -s nullglob
dir=video
for file in "$dir"/*.*; do
out=$dir/video-${file##*.}
[[ -d $out ]] || mkdir -- "$out" || continue
mv -- "$file" "$out"
done
You can also specify extensions with: in "$dir"/*.{avi,mkv,h264}
.
Yep this worked, thanks
– Lada1208
Jan 2 at 19:18
1
Make sure you understand how the parameter expansion works (they are incredibly handy for string parsing) Seeman bash
under Parameter Expansion.
– David C. Rankin
Jan 2 at 19:24
1
Tweak -- to protect against whitespaceout="$dir/video-${file##*.}"
(you cover it everywhere else)
– David C. Rankin
Jan 2 at 19:25
2
@DavidC.Rankin Not needed in variable assignment. However, I will add an explanation for OP on the parameter expansion later, good idea :)
– PesaThe
Jan 2 at 19:27
1
@DavidC.Rankin True, it's a force of habit though :) Just like not quoting$out
inside[[...]]
.
– PesaThe
Jan 2 at 19:33
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011790%2flinux-remove-duplicate-strings-from-for-and-cut%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This simple script might work for you:
#!/usr/bin/env bash
shopt -s nullglob
dir=video
for file in "$dir"/*.*; do
out=$dir/video-${file##*.}
[[ -d $out ]] || mkdir -- "$out" || continue
mv -- "$file" "$out"
done
You can also specify extensions with: in "$dir"/*.{avi,mkv,h264}
.
Yep this worked, thanks
– Lada1208
Jan 2 at 19:18
1
Make sure you understand how the parameter expansion works (they are incredibly handy for string parsing) Seeman bash
under Parameter Expansion.
– David C. Rankin
Jan 2 at 19:24
1
Tweak -- to protect against whitespaceout="$dir/video-${file##*.}"
(you cover it everywhere else)
– David C. Rankin
Jan 2 at 19:25
2
@DavidC.Rankin Not needed in variable assignment. However, I will add an explanation for OP on the parameter expansion later, good idea :)
– PesaThe
Jan 2 at 19:27
1
@DavidC.Rankin True, it's a force of habit though :) Just like not quoting$out
inside[[...]]
.
– PesaThe
Jan 2 at 19:33
|
show 1 more comment
This simple script might work for you:
#!/usr/bin/env bash
shopt -s nullglob
dir=video
for file in "$dir"/*.*; do
out=$dir/video-${file##*.}
[[ -d $out ]] || mkdir -- "$out" || continue
mv -- "$file" "$out"
done
You can also specify extensions with: in "$dir"/*.{avi,mkv,h264}
.
Yep this worked, thanks
– Lada1208
Jan 2 at 19:18
1
Make sure you understand how the parameter expansion works (they are incredibly handy for string parsing) Seeman bash
under Parameter Expansion.
– David C. Rankin
Jan 2 at 19:24
1
Tweak -- to protect against whitespaceout="$dir/video-${file##*.}"
(you cover it everywhere else)
– David C. Rankin
Jan 2 at 19:25
2
@DavidC.Rankin Not needed in variable assignment. However, I will add an explanation for OP on the parameter expansion later, good idea :)
– PesaThe
Jan 2 at 19:27
1
@DavidC.Rankin True, it's a force of habit though :) Just like not quoting$out
inside[[...]]
.
– PesaThe
Jan 2 at 19:33
|
show 1 more comment
This simple script might work for you:
#!/usr/bin/env bash
shopt -s nullglob
dir=video
for file in "$dir"/*.*; do
out=$dir/video-${file##*.}
[[ -d $out ]] || mkdir -- "$out" || continue
mv -- "$file" "$out"
done
You can also specify extensions with: in "$dir"/*.{avi,mkv,h264}
.
This simple script might work for you:
#!/usr/bin/env bash
shopt -s nullglob
dir=video
for file in "$dir"/*.*; do
out=$dir/video-${file##*.}
[[ -d $out ]] || mkdir -- "$out" || continue
mv -- "$file" "$out"
done
You can also specify extensions with: in "$dir"/*.{avi,mkv,h264}
.
edited Jan 2 at 19:39
answered Jan 2 at 19:15


PesaThePesaThe
5,8651529
5,8651529
Yep this worked, thanks
– Lada1208
Jan 2 at 19:18
1
Make sure you understand how the parameter expansion works (they are incredibly handy for string parsing) Seeman bash
under Parameter Expansion.
– David C. Rankin
Jan 2 at 19:24
1
Tweak -- to protect against whitespaceout="$dir/video-${file##*.}"
(you cover it everywhere else)
– David C. Rankin
Jan 2 at 19:25
2
@DavidC.Rankin Not needed in variable assignment. However, I will add an explanation for OP on the parameter expansion later, good idea :)
– PesaThe
Jan 2 at 19:27
1
@DavidC.Rankin True, it's a force of habit though :) Just like not quoting$out
inside[[...]]
.
– PesaThe
Jan 2 at 19:33
|
show 1 more comment
Yep this worked, thanks
– Lada1208
Jan 2 at 19:18
1
Make sure you understand how the parameter expansion works (they are incredibly handy for string parsing) Seeman bash
under Parameter Expansion.
– David C. Rankin
Jan 2 at 19:24
1
Tweak -- to protect against whitespaceout="$dir/video-${file##*.}"
(you cover it everywhere else)
– David C. Rankin
Jan 2 at 19:25
2
@DavidC.Rankin Not needed in variable assignment. However, I will add an explanation for OP on the parameter expansion later, good idea :)
– PesaThe
Jan 2 at 19:27
1
@DavidC.Rankin True, it's a force of habit though :) Just like not quoting$out
inside[[...]]
.
– PesaThe
Jan 2 at 19:33
Yep this worked, thanks
– Lada1208
Jan 2 at 19:18
Yep this worked, thanks
– Lada1208
Jan 2 at 19:18
1
1
Make sure you understand how the parameter expansion works (they are incredibly handy for string parsing) See
man bash
under Parameter Expansion.– David C. Rankin
Jan 2 at 19:24
Make sure you understand how the parameter expansion works (they are incredibly handy for string parsing) See
man bash
under Parameter Expansion.– David C. Rankin
Jan 2 at 19:24
1
1
Tweak -- to protect against whitespace
out="$dir/video-${file##*.}"
(you cover it everywhere else)– David C. Rankin
Jan 2 at 19:25
Tweak -- to protect against whitespace
out="$dir/video-${file##*.}"
(you cover it everywhere else)– David C. Rankin
Jan 2 at 19:25
2
2
@DavidC.Rankin Not needed in variable assignment. However, I will add an explanation for OP on the parameter expansion later, good idea :)
– PesaThe
Jan 2 at 19:27
@DavidC.Rankin Not needed in variable assignment. However, I will add an explanation for OP on the parameter expansion later, good idea :)
– PesaThe
Jan 2 at 19:27
1
1
@DavidC.Rankin True, it's a force of habit though :) Just like not quoting
$out
inside [[...]]
.– PesaThe
Jan 2 at 19:33
@DavidC.Rankin True, it's a force of habit though :) Just like not quoting
$out
inside [[...]]
.– PesaThe
Jan 2 at 19:33
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011790%2flinux-remove-duplicate-strings-from-for-and-cut%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown