Add or subtract a number from the names of all the files in a directory2019 Community Moderator ElectionMeaning of `10#N`For each subfolder, sort files by name and rename them to sequential padded numbers (regardless of extension)Batch rename files with unknown names and unknown extensionsHow to batch clean filenames containing invalid charactersHow do I rename lots of files on a directory without changing their extensions?How to find out all the files in other machines using bash shell script?What exactly files whose names' prefix is “AT.postflight.” are needed for?can mmv rename files by incrementing an index?How can I remove certain string from file name?errors in parameter expansion on filenames named after negative numbersRename all files in a directory to add a leading zero - for filenames with varying 'stems'
Basic combinatorial probability problem
Electoral considerations aside, what are potential benefits, for the US, of policy changes proposed by the tweet recognizing Golan annexation?
Are the IPv6 address space and IPv4 address space completely disjoint?
Fear of getting stuck on one programming language / technology that is not used in my country
What is this called? Old film camera viewer?
A social experiment. What is the worst that can happen?
Why a symmetric relation is defined: ∀x∀y( xRy⟹yRx) and not ∀x∀y (xRy⟺yRx)?
Pre-mixing cryogenic fuels and using only one fuel tank
Why is so much work done on numerical verification of the Riemann Hypothesis?
Melting point of aspirin, contradicting sources
Aragorn's "guise" in the Orthanc Stone
What does chmod -u do?
What is going wrong in this circuit which supposedly should step down AC to arduino friendly voltage?
Is Witten's Proof of the Positive Mass Theorem Rigorous?
What does "Scientists rise up against statistical significance" mean? (Comment in Nature)
How to write values with uncertainty and units with brackets: (339+-14) m/s
Is (0,1] a closed or open set?
Creepy dinosaur pc game identification
Added a new user on Ubuntu, set password not working?
How could a planet have erratic days?
Multiplicative persistence
What is the evidence for the "tyranny of the majority problem" in a direct democracy context?
How to advoid Unknown field: MyJSON.number
Creating nested elements dynamically
Add or subtract a number from the names of all the files in a directory
2019 Community Moderator ElectionMeaning of `10#N`For each subfolder, sort files by name and rename them to sequential padded numbers (regardless of extension)Batch rename files with unknown names and unknown extensionsHow to batch clean filenames containing invalid charactersHow do I rename lots of files on a directory without changing their extensions?How to find out all the files in other machines using bash shell script?What exactly files whose names' prefix is “AT.postflight.” are needed for?can mmv rename files by incrementing an index?How can I remove certain string from file name?errors in parameter expansion on filenames named after negative numbersRename all files in a directory to add a leading zero - for filenames with varying 'stems'
I have a number of png and jpg files whose names are numbers, e.g.0100.png, in a directory,
How can I add
1to their names, for example, to get0002.pngand0003.pngfrom0001.pngand0002.pngrespectively, without overwriting?How can I subtract
2from their names, so that0100.pngwill not become098.pngbut0098.pnginstead?
Related https://stackoverflow.com/questions/26770060/subtracting-a-number-from-the-names-of-all-the-files-in-a-directory, but more difficult here.
bash filenames rename arithmetic
add a comment |
I have a number of png and jpg files whose names are numbers, e.g.0100.png, in a directory,
How can I add
1to their names, for example, to get0002.pngand0003.pngfrom0001.pngand0002.pngrespectively, without overwriting?How can I subtract
2from their names, so that0100.pngwill not become098.pngbut0098.pnginstead?
Related https://stackoverflow.com/questions/26770060/subtracting-a-number-from-the-names-of-all-the-files-in-a-directory, but more difficult here.
bash filenames rename arithmetic
add a comment |
I have a number of png and jpg files whose names are numbers, e.g.0100.png, in a directory,
How can I add
1to their names, for example, to get0002.pngand0003.pngfrom0001.pngand0002.pngrespectively, without overwriting?How can I subtract
2from their names, so that0100.pngwill not become098.pngbut0098.pnginstead?
Related https://stackoverflow.com/questions/26770060/subtracting-a-number-from-the-names-of-all-the-files-in-a-directory, but more difficult here.
bash filenames rename arithmetic
I have a number of png and jpg files whose names are numbers, e.g.0100.png, in a directory,
How can I add
1to their names, for example, to get0002.pngand0003.pngfrom0001.pngand0002.pngrespectively, without overwriting?How can I subtract
2from their names, so that0100.pngwill not become098.pngbut0098.pnginstead?
Related https://stackoverflow.com/questions/26770060/subtracting-a-number-from-the-names-of-all-the-files-in-a-directory, but more difficult here.
bash filenames rename arithmetic
bash filenames rename arithmetic
edited May 23 '17 at 12:39
Community♦
1
1
asked Nov 13 '14 at 22:46
TimTim
28k78269488
28k78269488
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
I would probably end up using temporary directory in this case:
for file in [[:digit:]]*.png; do
echo mv $file tmp/$(printf %04d $((10#$file%.png+1))).png
done
The important part is 10#N which forces bash to interpret 000N as just N, otherwise leading zeros denotes octal numbers.
For example:
$ touch 0001.png 0002.png 0010.png 0020.png 0100.png 0200.png
$ for file in [[:digit:]]*.png; do echo mv $file tmp/$(printf %04d $((10#$file%.png-1))).png; done
mv 0001.png tmp/0000.png
mv 0002.png tmp/0001.png
mv 0010.png tmp/0009.png
mv 0020.png tmp/0019.png
mv 0100.png tmp/0099.png
mv 0200.png tmp/0199.png
So just run this command two times changingpng->jpg, or store extension in variable withext=$file/*./or add additional loop over all different extensions...
– jimmij
Nov 13 '14 at 23:35
BTW, inzshunder default settings you don't need to set base for numbers. Also it is simpler to strip extension from file so expression for new filename becometmp/$(printf %04d $(($file:r-1))).png
– jimmij
Nov 14 '14 at 0:04
Is thetmpdir under current dir? I run your command, but it doesn't do anything.
– Tim
Nov 14 '14 at 0:10
@Tim Yes in my casetmpis in current directory, so at the beginningmkdir tmpis need and at the endmv tmp/* .; rm -r tmp. Of course you can use/tmp(or whatever) as well, I assume you will modify such details anyway so don't even include this to the answer. And of course there isechoat the beginning of the line which should be removed if you like what you see on the screen, I hope it is obvious...
– jimmij
Nov 14 '14 at 0:16
I created a dir tmp under the current one, but still it is empty.
– Tim
Nov 14 '14 at 0:20
|
show 2 more comments
The solution for (1)
Sort the files according to their numbers and start renaming with the one with the highest number. That makes collisions impossible.
ls *.png | sort -rn | while read ...; do ... mv ...; done
The solution for (2)
Determine the number of digits (if it not the same for all files) and then use printf for keeping that length:
printf %04d.png 98
0098.png
Can we automate the process?
– Tim
Nov 13 '14 at 22:53
Thanks, but what are...? Can we also add and subtract a number?
– Tim
Nov 13 '14 at 22:58
add a comment |
Using Perl's rename :
-2 :
$ rename -n 's@bd+b@sprintf("%04d", $& - 2)@e' 0100.png
0100.png -> 0098.png
+1 :
$ rename -n 's@bd+b@sprintf("%04d", $& + 1)@e' 0001.png 0002.png
0001.png -> 0002.png
0002.png -> 0003.png
You can remove the -n (dry-run mode switch) when your tests become valids.
There are other tools with the same name which may or may not be able to do this, so be careful.
If you run the following command (linux)
$ file $(readlink -f $(type -p rename))
and you have a result like
.../rename: Perl script, ASCII text executable
then this seems to be the right tool =)
If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :
$ sudo update-alternatives --set rename /path/to/rename
(replace /path/to/rename to the path of your perl's rename command.
If you don't have this command, search your package manager to install it or do it manually
Last but not least, this tool was originally written by Larry Wall, the Perl's dad.
How do these work - without an intermediate temporary file?
– steeldriver
Nov 14 '14 at 0:03
On Ubuntu 12.04, I just get0001.png not renamed: 0002.png already exists
– steeldriver
Nov 14 '14 at 0:22
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f167862%2fadd-or-subtract-a-number-from-the-names-of-all-the-files-in-a-directory%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I would probably end up using temporary directory in this case:
for file in [[:digit:]]*.png; do
echo mv $file tmp/$(printf %04d $((10#$file%.png+1))).png
done
The important part is 10#N which forces bash to interpret 000N as just N, otherwise leading zeros denotes octal numbers.
For example:
$ touch 0001.png 0002.png 0010.png 0020.png 0100.png 0200.png
$ for file in [[:digit:]]*.png; do echo mv $file tmp/$(printf %04d $((10#$file%.png-1))).png; done
mv 0001.png tmp/0000.png
mv 0002.png tmp/0001.png
mv 0010.png tmp/0009.png
mv 0020.png tmp/0019.png
mv 0100.png tmp/0099.png
mv 0200.png tmp/0199.png
So just run this command two times changingpng->jpg, or store extension in variable withext=$file/*./or add additional loop over all different extensions...
– jimmij
Nov 13 '14 at 23:35
BTW, inzshunder default settings you don't need to set base for numbers. Also it is simpler to strip extension from file so expression for new filename becometmp/$(printf %04d $(($file:r-1))).png
– jimmij
Nov 14 '14 at 0:04
Is thetmpdir under current dir? I run your command, but it doesn't do anything.
– Tim
Nov 14 '14 at 0:10
@Tim Yes in my casetmpis in current directory, so at the beginningmkdir tmpis need and at the endmv tmp/* .; rm -r tmp. Of course you can use/tmp(or whatever) as well, I assume you will modify such details anyway so don't even include this to the answer. And of course there isechoat the beginning of the line which should be removed if you like what you see on the screen, I hope it is obvious...
– jimmij
Nov 14 '14 at 0:16
I created a dir tmp under the current one, but still it is empty.
– Tim
Nov 14 '14 at 0:20
|
show 2 more comments
I would probably end up using temporary directory in this case:
for file in [[:digit:]]*.png; do
echo mv $file tmp/$(printf %04d $((10#$file%.png+1))).png
done
The important part is 10#N which forces bash to interpret 000N as just N, otherwise leading zeros denotes octal numbers.
For example:
$ touch 0001.png 0002.png 0010.png 0020.png 0100.png 0200.png
$ for file in [[:digit:]]*.png; do echo mv $file tmp/$(printf %04d $((10#$file%.png-1))).png; done
mv 0001.png tmp/0000.png
mv 0002.png tmp/0001.png
mv 0010.png tmp/0009.png
mv 0020.png tmp/0019.png
mv 0100.png tmp/0099.png
mv 0200.png tmp/0199.png
So just run this command two times changingpng->jpg, or store extension in variable withext=$file/*./or add additional loop over all different extensions...
– jimmij
Nov 13 '14 at 23:35
BTW, inzshunder default settings you don't need to set base for numbers. Also it is simpler to strip extension from file so expression for new filename becometmp/$(printf %04d $(($file:r-1))).png
– jimmij
Nov 14 '14 at 0:04
Is thetmpdir under current dir? I run your command, but it doesn't do anything.
– Tim
Nov 14 '14 at 0:10
@Tim Yes in my casetmpis in current directory, so at the beginningmkdir tmpis need and at the endmv tmp/* .; rm -r tmp. Of course you can use/tmp(or whatever) as well, I assume you will modify such details anyway so don't even include this to the answer. And of course there isechoat the beginning of the line which should be removed if you like what you see on the screen, I hope it is obvious...
– jimmij
Nov 14 '14 at 0:16
I created a dir tmp under the current one, but still it is empty.
– Tim
Nov 14 '14 at 0:20
|
show 2 more comments
I would probably end up using temporary directory in this case:
for file in [[:digit:]]*.png; do
echo mv $file tmp/$(printf %04d $((10#$file%.png+1))).png
done
The important part is 10#N which forces bash to interpret 000N as just N, otherwise leading zeros denotes octal numbers.
For example:
$ touch 0001.png 0002.png 0010.png 0020.png 0100.png 0200.png
$ for file in [[:digit:]]*.png; do echo mv $file tmp/$(printf %04d $((10#$file%.png-1))).png; done
mv 0001.png tmp/0000.png
mv 0002.png tmp/0001.png
mv 0010.png tmp/0009.png
mv 0020.png tmp/0019.png
mv 0100.png tmp/0099.png
mv 0200.png tmp/0199.png
I would probably end up using temporary directory in this case:
for file in [[:digit:]]*.png; do
echo mv $file tmp/$(printf %04d $((10#$file%.png+1))).png
done
The important part is 10#N which forces bash to interpret 000N as just N, otherwise leading zeros denotes octal numbers.
For example:
$ touch 0001.png 0002.png 0010.png 0020.png 0100.png 0200.png
$ for file in [[:digit:]]*.png; do echo mv $file tmp/$(printf %04d $((10#$file%.png-1))).png; done
mv 0001.png tmp/0000.png
mv 0002.png tmp/0001.png
mv 0010.png tmp/0009.png
mv 0020.png tmp/0019.png
mv 0100.png tmp/0099.png
mv 0200.png tmp/0199.png
answered Nov 13 '14 at 23:21
jimmijjimmij
32.3k875109
32.3k875109
So just run this command two times changingpng->jpg, or store extension in variable withext=$file/*./or add additional loop over all different extensions...
– jimmij
Nov 13 '14 at 23:35
BTW, inzshunder default settings you don't need to set base for numbers. Also it is simpler to strip extension from file so expression for new filename becometmp/$(printf %04d $(($file:r-1))).png
– jimmij
Nov 14 '14 at 0:04
Is thetmpdir under current dir? I run your command, but it doesn't do anything.
– Tim
Nov 14 '14 at 0:10
@Tim Yes in my casetmpis in current directory, so at the beginningmkdir tmpis need and at the endmv tmp/* .; rm -r tmp. Of course you can use/tmp(or whatever) as well, I assume you will modify such details anyway so don't even include this to the answer. And of course there isechoat the beginning of the line which should be removed if you like what you see on the screen, I hope it is obvious...
– jimmij
Nov 14 '14 at 0:16
I created a dir tmp under the current one, but still it is empty.
– Tim
Nov 14 '14 at 0:20
|
show 2 more comments
So just run this command two times changingpng->jpg, or store extension in variable withext=$file/*./or add additional loop over all different extensions...
– jimmij
Nov 13 '14 at 23:35
BTW, inzshunder default settings you don't need to set base for numbers. Also it is simpler to strip extension from file so expression for new filename becometmp/$(printf %04d $(($file:r-1))).png
– jimmij
Nov 14 '14 at 0:04
Is thetmpdir under current dir? I run your command, but it doesn't do anything.
– Tim
Nov 14 '14 at 0:10
@Tim Yes in my casetmpis in current directory, so at the beginningmkdir tmpis need and at the endmv tmp/* .; rm -r tmp. Of course you can use/tmp(or whatever) as well, I assume you will modify such details anyway so don't even include this to the answer. And of course there isechoat the beginning of the line which should be removed if you like what you see on the screen, I hope it is obvious...
– jimmij
Nov 14 '14 at 0:16
I created a dir tmp under the current one, but still it is empty.
– Tim
Nov 14 '14 at 0:20
So just run this command two times changing
png->jpg, or store extension in variable with ext=$file/*./ or add additional loop over all different extensions...– jimmij
Nov 13 '14 at 23:35
So just run this command two times changing
png->jpg, or store extension in variable with ext=$file/*./ or add additional loop over all different extensions...– jimmij
Nov 13 '14 at 23:35
BTW, in
zsh under default settings you don't need to set base for numbers. Also it is simpler to strip extension from file so expression for new filename become tmp/$(printf %04d $(($file:r-1))).png– jimmij
Nov 14 '14 at 0:04
BTW, in
zsh under default settings you don't need to set base for numbers. Also it is simpler to strip extension from file so expression for new filename become tmp/$(printf %04d $(($file:r-1))).png– jimmij
Nov 14 '14 at 0:04
Is the
tmp dir under current dir? I run your command, but it doesn't do anything.– Tim
Nov 14 '14 at 0:10
Is the
tmp dir under current dir? I run your command, but it doesn't do anything.– Tim
Nov 14 '14 at 0:10
@Tim Yes in my case
tmp is in current directory, so at the beginning mkdir tmp is need and at the end mv tmp/* .; rm -r tmp. Of course you can use /tmp (or whatever) as well, I assume you will modify such details anyway so don't even include this to the answer. And of course there is echo at the beginning of the line which should be removed if you like what you see on the screen, I hope it is obvious...– jimmij
Nov 14 '14 at 0:16
@Tim Yes in my case
tmp is in current directory, so at the beginning mkdir tmp is need and at the end mv tmp/* .; rm -r tmp. Of course you can use /tmp (or whatever) as well, I assume you will modify such details anyway so don't even include this to the answer. And of course there is echo at the beginning of the line which should be removed if you like what you see on the screen, I hope it is obvious...– jimmij
Nov 14 '14 at 0:16
I created a dir tmp under the current one, but still it is empty.
– Tim
Nov 14 '14 at 0:20
I created a dir tmp under the current one, but still it is empty.
– Tim
Nov 14 '14 at 0:20
|
show 2 more comments
The solution for (1)
Sort the files according to their numbers and start renaming with the one with the highest number. That makes collisions impossible.
ls *.png | sort -rn | while read ...; do ... mv ...; done
The solution for (2)
Determine the number of digits (if it not the same for all files) and then use printf for keeping that length:
printf %04d.png 98
0098.png
Can we automate the process?
– Tim
Nov 13 '14 at 22:53
Thanks, but what are...? Can we also add and subtract a number?
– Tim
Nov 13 '14 at 22:58
add a comment |
The solution for (1)
Sort the files according to their numbers and start renaming with the one with the highest number. That makes collisions impossible.
ls *.png | sort -rn | while read ...; do ... mv ...; done
The solution for (2)
Determine the number of digits (if it not the same for all files) and then use printf for keeping that length:
printf %04d.png 98
0098.png
Can we automate the process?
– Tim
Nov 13 '14 at 22:53
Thanks, but what are...? Can we also add and subtract a number?
– Tim
Nov 13 '14 at 22:58
add a comment |
The solution for (1)
Sort the files according to their numbers and start renaming with the one with the highest number. That makes collisions impossible.
ls *.png | sort -rn | while read ...; do ... mv ...; done
The solution for (2)
Determine the number of digits (if it not the same for all files) and then use printf for keeping that length:
printf %04d.png 98
0098.png
The solution for (1)
Sort the files according to their numbers and start renaming with the one with the highest number. That makes collisions impossible.
ls *.png | sort -rn | while read ...; do ... mv ...; done
The solution for (2)
Determine the number of digits (if it not the same for all files) and then use printf for keeping that length:
printf %04d.png 98
0098.png
edited Nov 13 '14 at 22:56
answered Nov 13 '14 at 22:50
Hauke LagingHauke Laging
57.5k1287136
57.5k1287136
Can we automate the process?
– Tim
Nov 13 '14 at 22:53
Thanks, but what are...? Can we also add and subtract a number?
– Tim
Nov 13 '14 at 22:58
add a comment |
Can we automate the process?
– Tim
Nov 13 '14 at 22:53
Thanks, but what are...? Can we also add and subtract a number?
– Tim
Nov 13 '14 at 22:58
Can we automate the process?
– Tim
Nov 13 '14 at 22:53
Can we automate the process?
– Tim
Nov 13 '14 at 22:53
Thanks, but what are
...? Can we also add and subtract a number?– Tim
Nov 13 '14 at 22:58
Thanks, but what are
...? Can we also add and subtract a number?– Tim
Nov 13 '14 at 22:58
add a comment |
Using Perl's rename :
-2 :
$ rename -n 's@bd+b@sprintf("%04d", $& - 2)@e' 0100.png
0100.png -> 0098.png
+1 :
$ rename -n 's@bd+b@sprintf("%04d", $& + 1)@e' 0001.png 0002.png
0001.png -> 0002.png
0002.png -> 0003.png
You can remove the -n (dry-run mode switch) when your tests become valids.
There are other tools with the same name which may or may not be able to do this, so be careful.
If you run the following command (linux)
$ file $(readlink -f $(type -p rename))
and you have a result like
.../rename: Perl script, ASCII text executable
then this seems to be the right tool =)
If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :
$ sudo update-alternatives --set rename /path/to/rename
(replace /path/to/rename to the path of your perl's rename command.
If you don't have this command, search your package manager to install it or do it manually
Last but not least, this tool was originally written by Larry Wall, the Perl's dad.
How do these work - without an intermediate temporary file?
– steeldriver
Nov 14 '14 at 0:03
On Ubuntu 12.04, I just get0001.png not renamed: 0002.png already exists
– steeldriver
Nov 14 '14 at 0:22
add a comment |
Using Perl's rename :
-2 :
$ rename -n 's@bd+b@sprintf("%04d", $& - 2)@e' 0100.png
0100.png -> 0098.png
+1 :
$ rename -n 's@bd+b@sprintf("%04d", $& + 1)@e' 0001.png 0002.png
0001.png -> 0002.png
0002.png -> 0003.png
You can remove the -n (dry-run mode switch) when your tests become valids.
There are other tools with the same name which may or may not be able to do this, so be careful.
If you run the following command (linux)
$ file $(readlink -f $(type -p rename))
and you have a result like
.../rename: Perl script, ASCII text executable
then this seems to be the right tool =)
If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :
$ sudo update-alternatives --set rename /path/to/rename
(replace /path/to/rename to the path of your perl's rename command.
If you don't have this command, search your package manager to install it or do it manually
Last but not least, this tool was originally written by Larry Wall, the Perl's dad.
How do these work - without an intermediate temporary file?
– steeldriver
Nov 14 '14 at 0:03
On Ubuntu 12.04, I just get0001.png not renamed: 0002.png already exists
– steeldriver
Nov 14 '14 at 0:22
add a comment |
Using Perl's rename :
-2 :
$ rename -n 's@bd+b@sprintf("%04d", $& - 2)@e' 0100.png
0100.png -> 0098.png
+1 :
$ rename -n 's@bd+b@sprintf("%04d", $& + 1)@e' 0001.png 0002.png
0001.png -> 0002.png
0002.png -> 0003.png
You can remove the -n (dry-run mode switch) when your tests become valids.
There are other tools with the same name which may or may not be able to do this, so be careful.
If you run the following command (linux)
$ file $(readlink -f $(type -p rename))
and you have a result like
.../rename: Perl script, ASCII text executable
then this seems to be the right tool =)
If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :
$ sudo update-alternatives --set rename /path/to/rename
(replace /path/to/rename to the path of your perl's rename command.
If you don't have this command, search your package manager to install it or do it manually
Last but not least, this tool was originally written by Larry Wall, the Perl's dad.
Using Perl's rename :
-2 :
$ rename -n 's@bd+b@sprintf("%04d", $& - 2)@e' 0100.png
0100.png -> 0098.png
+1 :
$ rename -n 's@bd+b@sprintf("%04d", $& + 1)@e' 0001.png 0002.png
0001.png -> 0002.png
0002.png -> 0003.png
You can remove the -n (dry-run mode switch) when your tests become valids.
There are other tools with the same name which may or may not be able to do this, so be careful.
If you run the following command (linux)
$ file $(readlink -f $(type -p rename))
and you have a result like
.../rename: Perl script, ASCII text executable
then this seems to be the right tool =)
If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :
$ sudo update-alternatives --set rename /path/to/rename
(replace /path/to/rename to the path of your perl's rename command.
If you don't have this command, search your package manager to install it or do it manually
Last but not least, this tool was originally written by Larry Wall, the Perl's dad.
edited Mar 12 at 20:10
Glorfindel
3171511
3171511
answered Nov 13 '14 at 23:27
Gilles QuenotGilles Quenot
16.3k14053
16.3k14053
How do these work - without an intermediate temporary file?
– steeldriver
Nov 14 '14 at 0:03
On Ubuntu 12.04, I just get0001.png not renamed: 0002.png already exists
– steeldriver
Nov 14 '14 at 0:22
add a comment |
How do these work - without an intermediate temporary file?
– steeldriver
Nov 14 '14 at 0:03
On Ubuntu 12.04, I just get0001.png not renamed: 0002.png already exists
– steeldriver
Nov 14 '14 at 0:22
How do these work - without an intermediate temporary file?
– steeldriver
Nov 14 '14 at 0:03
How do these work - without an intermediate temporary file?
– steeldriver
Nov 14 '14 at 0:03
On Ubuntu 12.04, I just get
0001.png not renamed: 0002.png already exists– steeldriver
Nov 14 '14 at 0:22
On Ubuntu 12.04, I just get
0001.png not renamed: 0002.png already exists– steeldriver
Nov 14 '14 at 0:22
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f167862%2fadd-or-subtract-a-number-from-the-names-of-all-the-files-in-a-directory%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
-arithmetic, bash, filenames, rename