Rename matching files based on a serial number
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 |
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 |
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 '18 at 0:10
codeforester
17.4k83864
17.4k83864
asked Nov 11 '18 at 22:23
Rocky Li
2,8201316
2,8201316
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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 '18 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 '18 at 0:17
add a comment |
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 '18 at 0:28
add a comment |
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 |
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 |
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%2f53253838%2frename-matching-files-based-on-a-serial-number%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '18 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 '18 at 0:17
add a comment |
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 '18 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 '18 at 0:17
add a comment |
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 '18 at 22:42
answered Nov 11 '18 at 22:36
codeforester
17.4k83864
17.4k83864
Does$((count++))
initiate the variablecount
as well? Looks like it. I never knew that's possible.
– Rocky Li
Nov 12 '18 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 '18 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 '18 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 '18 at 0:17
Does
$((count++))
initiate the variable count
as well? Looks like it. I never knew that's possible.– Rocky Li
Nov 12 '18 at 0:15
Does
$((count++))
initiate the variable count
as well? Looks like it. I never knew that's possible.– Rocky Li
Nov 12 '18 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 '18 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 '18 at 0:17
add a comment |
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 '18 at 0:28
add a comment |
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 '18 at 0:28
add a comment |
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 '18 at 22:47
choroba
154k14140202
154k14140202
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 '18 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 '18 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 '18 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 '18 at 0:28
add a comment |
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 |
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 |
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 '18 at 10:33
stack0114106
2,1811417
2,1811417
add a comment |
add a comment |
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 |
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 |
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 '18 at 12:08
answered Nov 12 '18 at 12:00
Mark Setchell
86.1k673172
86.1k673172
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