Rename matching files based on a serial number
up vote
1
down vote
favorite
Assuming I have a bunch of files that are mac screenshots:
Screen Shot 2018-11-09 at 12.37.37 PM.png
Screen Shot 2018-11-10 at 4.53.02 PM.png
Screen Shot 2018-11-10 at 9.19.19 PM.png
And I want to use mv to label them to:
Screen_0.png
Screen_1.png
Screen_2.png
The partial command I come up with:
find . -name "Screen*" -exec sh -c 'mv "$1" "Screen_$2"' _ {} ??? ;
How to implement the command so that it can label image by digits? or do I have to resort to a more complicated file.
bash find rename
add a comment |
up vote
1
down vote
favorite
Assuming I have a bunch of files that are mac screenshots:
Screen Shot 2018-11-09 at 12.37.37 PM.png
Screen Shot 2018-11-10 at 4.53.02 PM.png
Screen Shot 2018-11-10 at 9.19.19 PM.png
And I want to use mv to label them to:
Screen_0.png
Screen_1.png
Screen_2.png
The partial command I come up with:
find . -name "Screen*" -exec sh -c 'mv "$1" "Screen_$2"' _ {} ??? ;
How to implement the command so that it can label image by digits? or do I have to resort to a more complicated file.
bash find rename
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Assuming I have a bunch of files that are mac screenshots:
Screen Shot 2018-11-09 at 12.37.37 PM.png
Screen Shot 2018-11-10 at 4.53.02 PM.png
Screen Shot 2018-11-10 at 9.19.19 PM.png
And I want to use mv to label them to:
Screen_0.png
Screen_1.png
Screen_2.png
The partial command I come up with:
find . -name "Screen*" -exec sh -c 'mv "$1" "Screen_$2"' _ {} ??? ;
How to implement the command so that it can label image by digits? or do I have to resort to a more complicated file.
bash find rename
Assuming I have a bunch of files that are mac screenshots:
Screen Shot 2018-11-09 at 12.37.37 PM.png
Screen Shot 2018-11-10 at 4.53.02 PM.png
Screen Shot 2018-11-10 at 9.19.19 PM.png
And I want to use mv to label them to:
Screen_0.png
Screen_1.png
Screen_2.png
The partial command I come up with:
find . -name "Screen*" -exec sh -c 'mv "$1" "Screen_$2"' _ {} ??? ;
How to implement the command so that it can label image by digits? or do I have to resort to a more complicated file.
bash find rename
bash find rename
edited Nov 12 at 0:10
codeforester
17.3k83864
17.3k83864
asked Nov 11 at 22:23
Rocky Li
2,7531315
2,7531315
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
up vote
2
down vote
accepted
I don't think it is possible to pass a sequence number xargs
in the way you want. Use a simple loop instead:
#!/bin/bash
for file in Screen*.png; do
[[ -f $file ]] || continue # skip if not a regular file
mv "$file" "Screen_$((count++)).png"
done
Does$((count++))
initiate the variablecount
as well? Looks like it. I never knew that's possible.
– Rocky Li
Nov 12 at 0:15
1
Yes, it does. Please note the post increment - the initial value will be zero. If you want the initial value to be 1, use$((++count))
.
– codeforester
Nov 12 at 0:17
add a comment |
up vote
1
down vote
If you use an array, no arithmetics is needed, just use the index of each element:
#!/bin/bash
files=('Screen Shot'*.png)
for i in "${!files[@]}" ; do
mv "${files[i]}" Screen_$i.png
done
Thanks, I was able to condense your code into a single line:files=(Screen*); for i in ${!files[@]}; do mv ${files[i]} "Screen_$i.png"; done
that works in the terminal, much thanks!
– Rocky Li
Nov 12 at 0:28
add a comment |
up vote
1
down vote
With Perl one liner also, you could do it easily.
> ls -1 Screen*
Screen Shot 2018-11-09 at 12.37.37 PM.png
Screen Shot 2018-11-10 at 4.53.02 PM.png
Screen Shot 2018-11-10 at 9.19.19 PM.png
> perl -ne ' BEGIN { for(glob("Screen*")) { rename "$_", "Screen_".$x++.".png" } ; exit }'
> ls -1 Screen*
Screen_0.png
Screen_1.png
Screen_2.png
>
add a comment |
up vote
1
down vote
You can just use rename
, a.k.a. Perl rename:
rename --dry-run 's/.*/Screen_$N.png/' Screenshot*png
Sample Output
'Screenshot 2018-11-12 at 11.54.32.png' would be renamed to 'Screen_1.png'
'Screenshot 2018-11-12 at 11.54.38.png' would be renamed to 'Screen_2.png'
'Screenshot 2018-11-12 at 11.54.42.png' would be renamed to 'Screen_3.png'
If you like the look of the output, run again without --dry-run
.
If you are on macOS, you can install Perl rename
with homebrew:
brew install rename
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
I don't think it is possible to pass a sequence number xargs
in the way you want. Use a simple loop instead:
#!/bin/bash
for file in Screen*.png; do
[[ -f $file ]] || continue # skip if not a regular file
mv "$file" "Screen_$((count++)).png"
done
Does$((count++))
initiate the variablecount
as well? Looks like it. I never knew that's possible.
– Rocky Li
Nov 12 at 0:15
1
Yes, it does. Please note the post increment - the initial value will be zero. If you want the initial value to be 1, use$((++count))
.
– codeforester
Nov 12 at 0:17
add a comment |
up vote
2
down vote
accepted
I don't think it is possible to pass a sequence number xargs
in the way you want. Use a simple loop instead:
#!/bin/bash
for file in Screen*.png; do
[[ -f $file ]] || continue # skip if not a regular file
mv "$file" "Screen_$((count++)).png"
done
Does$((count++))
initiate the variablecount
as well? Looks like it. I never knew that's possible.
– Rocky Li
Nov 12 at 0:15
1
Yes, it does. Please note the post increment - the initial value will be zero. If you want the initial value to be 1, use$((++count))
.
– codeforester
Nov 12 at 0:17
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
I don't think it is possible to pass a sequence number xargs
in the way you want. Use a simple loop instead:
#!/bin/bash
for file in Screen*.png; do
[[ -f $file ]] || continue # skip if not a regular file
mv "$file" "Screen_$((count++)).png"
done
I don't think it is possible to pass a sequence number xargs
in the way you want. Use a simple loop instead:
#!/bin/bash
for file in Screen*.png; do
[[ -f $file ]] || continue # skip if not a regular file
mv "$file" "Screen_$((count++)).png"
done
edited Nov 11 at 22:42
answered Nov 11 at 22:36
codeforester
17.3k83864
17.3k83864
Does$((count++))
initiate the variablecount
as well? Looks like it. I never knew that's possible.
– Rocky Li
Nov 12 at 0:15
1
Yes, it does. Please note the post increment - the initial value will be zero. If you want the initial value to be 1, use$((++count))
.
– codeforester
Nov 12 at 0:17
add a comment |
Does$((count++))
initiate the variablecount
as well? Looks like it. I never knew that's possible.
– Rocky Li
Nov 12 at 0:15
1
Yes, it does. Please note the post increment - the initial value will be zero. If you want the initial value to be 1, use$((++count))
.
– codeforester
Nov 12 at 0:17
Does
$((count++))
initiate the variable count
as well? Looks like it. I never knew that's possible.– Rocky Li
Nov 12 at 0:15
Does
$((count++))
initiate the variable count
as well? Looks like it. I never knew that's possible.– Rocky Li
Nov 12 at 0:15
1
1
Yes, it does. Please note the post increment - the initial value will be zero. If you want the initial value to be 1, use
$((++count))
.– codeforester
Nov 12 at 0:17
Yes, it does. Please note the post increment - the initial value will be zero. If you want the initial value to be 1, use
$((++count))
.– codeforester
Nov 12 at 0:17
add a comment |
up vote
1
down vote
If you use an array, no arithmetics is needed, just use the index of each element:
#!/bin/bash
files=('Screen Shot'*.png)
for i in "${!files[@]}" ; do
mv "${files[i]}" Screen_$i.png
done
Thanks, I was able to condense your code into a single line:files=(Screen*); for i in ${!files[@]}; do mv ${files[i]} "Screen_$i.png"; done
that works in the terminal, much thanks!
– Rocky Li
Nov 12 at 0:28
add a comment |
up vote
1
down vote
If you use an array, no arithmetics is needed, just use the index of each element:
#!/bin/bash
files=('Screen Shot'*.png)
for i in "${!files[@]}" ; do
mv "${files[i]}" Screen_$i.png
done
Thanks, I was able to condense your code into a single line:files=(Screen*); for i in ${!files[@]}; do mv ${files[i]} "Screen_$i.png"; done
that works in the terminal, much thanks!
– Rocky Li
Nov 12 at 0:28
add a comment |
up vote
1
down vote
up vote
1
down vote
If you use an array, no arithmetics is needed, just use the index of each element:
#!/bin/bash
files=('Screen Shot'*.png)
for i in "${!files[@]}" ; do
mv "${files[i]}" Screen_$i.png
done
If you use an array, no arithmetics is needed, just use the index of each element:
#!/bin/bash
files=('Screen Shot'*.png)
for i in "${!files[@]}" ; do
mv "${files[i]}" Screen_$i.png
done
answered Nov 11 at 22:47
choroba
153k14139201
153k14139201
Thanks, I was able to condense your code into a single line:files=(Screen*); for i in ${!files[@]}; do mv ${files[i]} "Screen_$i.png"; done
that works in the terminal, much thanks!
– Rocky Li
Nov 12 at 0:28
add a comment |
Thanks, I was able to condense your code into a single line:files=(Screen*); for i in ${!files[@]}; do mv ${files[i]} "Screen_$i.png"; done
that works in the terminal, much thanks!
– Rocky Li
Nov 12 at 0:28
Thanks, I was able to condense your code into a single line:
files=(Screen*); for i in ${!files[@]}; do mv ${files[i]} "Screen_$i.png"; done
that works in the terminal, much thanks!– Rocky Li
Nov 12 at 0:28
Thanks, I was able to condense your code into a single line:
files=(Screen*); for i in ${!files[@]}; do mv ${files[i]} "Screen_$i.png"; done
that works in the terminal, much thanks!– Rocky Li
Nov 12 at 0:28
add a comment |
up vote
1
down vote
With Perl one liner also, you could do it easily.
> ls -1 Screen*
Screen Shot 2018-11-09 at 12.37.37 PM.png
Screen Shot 2018-11-10 at 4.53.02 PM.png
Screen Shot 2018-11-10 at 9.19.19 PM.png
> perl -ne ' BEGIN { for(glob("Screen*")) { rename "$_", "Screen_".$x++.".png" } ; exit }'
> ls -1 Screen*
Screen_0.png
Screen_1.png
Screen_2.png
>
add a comment |
up vote
1
down vote
With Perl one liner also, you could do it easily.
> ls -1 Screen*
Screen Shot 2018-11-09 at 12.37.37 PM.png
Screen Shot 2018-11-10 at 4.53.02 PM.png
Screen Shot 2018-11-10 at 9.19.19 PM.png
> perl -ne ' BEGIN { for(glob("Screen*")) { rename "$_", "Screen_".$x++.".png" } ; exit }'
> ls -1 Screen*
Screen_0.png
Screen_1.png
Screen_2.png
>
add a comment |
up vote
1
down vote
up vote
1
down vote
With Perl one liner also, you could do it easily.
> ls -1 Screen*
Screen Shot 2018-11-09 at 12.37.37 PM.png
Screen Shot 2018-11-10 at 4.53.02 PM.png
Screen Shot 2018-11-10 at 9.19.19 PM.png
> perl -ne ' BEGIN { for(glob("Screen*")) { rename "$_", "Screen_".$x++.".png" } ; exit }'
> ls -1 Screen*
Screen_0.png
Screen_1.png
Screen_2.png
>
With Perl one liner also, you could do it easily.
> ls -1 Screen*
Screen Shot 2018-11-09 at 12.37.37 PM.png
Screen Shot 2018-11-10 at 4.53.02 PM.png
Screen Shot 2018-11-10 at 9.19.19 PM.png
> perl -ne ' BEGIN { for(glob("Screen*")) { rename "$_", "Screen_".$x++.".png" } ; exit }'
> ls -1 Screen*
Screen_0.png
Screen_1.png
Screen_2.png
>
answered Nov 12 at 10:33
stack0114106
1,7221416
1,7221416
add a comment |
add a comment |
up vote
1
down vote
You can just use rename
, a.k.a. Perl rename:
rename --dry-run 's/.*/Screen_$N.png/' Screenshot*png
Sample Output
'Screenshot 2018-11-12 at 11.54.32.png' would be renamed to 'Screen_1.png'
'Screenshot 2018-11-12 at 11.54.38.png' would be renamed to 'Screen_2.png'
'Screenshot 2018-11-12 at 11.54.42.png' would be renamed to 'Screen_3.png'
If you like the look of the output, run again without --dry-run
.
If you are on macOS, you can install Perl rename
with homebrew:
brew install rename
add a comment |
up vote
1
down vote
You can just use rename
, a.k.a. Perl rename:
rename --dry-run 's/.*/Screen_$N.png/' Screenshot*png
Sample Output
'Screenshot 2018-11-12 at 11.54.32.png' would be renamed to 'Screen_1.png'
'Screenshot 2018-11-12 at 11.54.38.png' would be renamed to 'Screen_2.png'
'Screenshot 2018-11-12 at 11.54.42.png' would be renamed to 'Screen_3.png'
If you like the look of the output, run again without --dry-run
.
If you are on macOS, you can install Perl rename
with homebrew:
brew install rename
add a comment |
up vote
1
down vote
up vote
1
down vote
You can just use rename
, a.k.a. Perl rename:
rename --dry-run 's/.*/Screen_$N.png/' Screenshot*png
Sample Output
'Screenshot 2018-11-12 at 11.54.32.png' would be renamed to 'Screen_1.png'
'Screenshot 2018-11-12 at 11.54.38.png' would be renamed to 'Screen_2.png'
'Screenshot 2018-11-12 at 11.54.42.png' would be renamed to 'Screen_3.png'
If you like the look of the output, run again without --dry-run
.
If you are on macOS, you can install Perl rename
with homebrew:
brew install rename
You can just use rename
, a.k.a. Perl rename:
rename --dry-run 's/.*/Screen_$N.png/' Screenshot*png
Sample Output
'Screenshot 2018-11-12 at 11.54.32.png' would be renamed to 'Screen_1.png'
'Screenshot 2018-11-12 at 11.54.38.png' would be renamed to 'Screen_2.png'
'Screenshot 2018-11-12 at 11.54.42.png' would be renamed to 'Screen_3.png'
If you like the look of the output, run again without --dry-run
.
If you are on macOS, you can install Perl rename
with homebrew:
brew install rename
edited Nov 12 at 12:08
answered Nov 12 at 12:00
Mark Setchell
84.9k672171
84.9k672171
add a comment |
add a 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53253838%2frename-matching-files-based-on-a-serial-number%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