Rename multiple files sequentially and starting by the same prefix
I have a folder containing like 1500 files, I want to rename them automatically via the terminal or a shell script as follows: "prefix_number.extension".
Example: cin_1.jpg, cin_2.png ...
command-line scripts rename batch-rename
add a comment |
I have a folder containing like 1500 files, I want to rename them automatically via the terminal or a shell script as follows: "prefix_number.extension".
Example: cin_1.jpg, cin_2.png ...
command-line scripts rename batch-rename
Likely related: Sequential renaming of files
– steeldriver
Jan 8 at 22:42
add a comment |
I have a folder containing like 1500 files, I want to rename them automatically via the terminal or a shell script as follows: "prefix_number.extension".
Example: cin_1.jpg, cin_2.png ...
command-line scripts rename batch-rename
I have a folder containing like 1500 files, I want to rename them automatically via the terminal or a shell script as follows: "prefix_number.extension".
Example: cin_1.jpg, cin_2.png ...
command-line scripts rename batch-rename
command-line scripts rename batch-rename
edited Jan 9 at 15:14
singrium
asked Jan 8 at 22:32
singriumsingrium
1,211424
1,211424
Likely related: Sequential renaming of files
– steeldriver
Jan 8 at 22:42
add a comment |
Likely related: Sequential renaming of files
– steeldriver
Jan 8 at 22:42
Likely related: Sequential renaming of files
– steeldriver
Jan 8 at 22:42
Likely related: Sequential renaming of files
– steeldriver
Jan 8 at 22:42
add a comment |
2 Answers
2
active
oldest
votes
Try:
count=0; for f in *; do [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"; done
Or, if you prefer your commands spread over multiple lines:
count=0
for f in *
do
[ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"
done
How it works
count=0
This initializes the variable
count
to zero.
for f in *; do
This starts a loop over all files in the current directory.
[ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"
[ -f "$f" ]
tests to see if the file$f
is a regular file (not a directory).
If
$f
is a regular file, then the move (mv
) command is run.-i
tellsmv
not to overwrite any existing files without asking."cin_$((++count)).${f##*.}"
is the name of the new file.cin_
is the prefix.$((++count))
returns the value ofcount
after it has been incremented.${f##*.}
is the extension of file$f
.
done
This marks the end of the loop.
Example
Consider a directory with these three files:
$ ls
alpha.jpg beta.png gamma.txt
Let's run our command:
$ count=0; for f in *; do [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"; done
After running our command, the files in the directory now are:
$ ls
cin_1.jpg cin_2.png cin_3.txt
1
That did exactly what I wanted, and it is super fast (faster than with Python). Thank you!
– singrium
Jan 8 at 22:50
add a comment |
A rename
oneliner:
rename -n 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
This matches every file with a dot in its name (the last one, if multiple) and renames the part until the dot with cin_
followed by an incrementing number and a dot. With the -n
flag it just prints what it would do, remove it to perform the renaming.
Example run
$ ls
a.jpg b.jpg d.png e.png
$ rename -n 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
rename(a.jpg, cin_1.jpg)
rename(b.jpg, cin_2.jpg)
rename(d.png, cin_3.png)
rename(e.png, cin_4.png)
$ rename 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
$ ls
cin_1.jpg cin_2.jpg cin_3.png cin_4.png
Source: How to rename multiple files sequentially from command line?
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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%2faskubuntu.com%2fquestions%2f1108141%2frename-multiple-files-sequentially-and-starting-by-the-same-prefix%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try:
count=0; for f in *; do [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"; done
Or, if you prefer your commands spread over multiple lines:
count=0
for f in *
do
[ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"
done
How it works
count=0
This initializes the variable
count
to zero.
for f in *; do
This starts a loop over all files in the current directory.
[ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"
[ -f "$f" ]
tests to see if the file$f
is a regular file (not a directory).
If
$f
is a regular file, then the move (mv
) command is run.-i
tellsmv
not to overwrite any existing files without asking."cin_$((++count)).${f##*.}"
is the name of the new file.cin_
is the prefix.$((++count))
returns the value ofcount
after it has been incremented.${f##*.}
is the extension of file$f
.
done
This marks the end of the loop.
Example
Consider a directory with these three files:
$ ls
alpha.jpg beta.png gamma.txt
Let's run our command:
$ count=0; for f in *; do [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"; done
After running our command, the files in the directory now are:
$ ls
cin_1.jpg cin_2.png cin_3.txt
1
That did exactly what I wanted, and it is super fast (faster than with Python). Thank you!
– singrium
Jan 8 at 22:50
add a comment |
Try:
count=0; for f in *; do [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"; done
Or, if you prefer your commands spread over multiple lines:
count=0
for f in *
do
[ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"
done
How it works
count=0
This initializes the variable
count
to zero.
for f in *; do
This starts a loop over all files in the current directory.
[ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"
[ -f "$f" ]
tests to see if the file$f
is a regular file (not a directory).
If
$f
is a regular file, then the move (mv
) command is run.-i
tellsmv
not to overwrite any existing files without asking."cin_$((++count)).${f##*.}"
is the name of the new file.cin_
is the prefix.$((++count))
returns the value ofcount
after it has been incremented.${f##*.}
is the extension of file$f
.
done
This marks the end of the loop.
Example
Consider a directory with these three files:
$ ls
alpha.jpg beta.png gamma.txt
Let's run our command:
$ count=0; for f in *; do [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"; done
After running our command, the files in the directory now are:
$ ls
cin_1.jpg cin_2.png cin_3.txt
1
That did exactly what I wanted, and it is super fast (faster than with Python). Thank you!
– singrium
Jan 8 at 22:50
add a comment |
Try:
count=0; for f in *; do [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"; done
Or, if you prefer your commands spread over multiple lines:
count=0
for f in *
do
[ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"
done
How it works
count=0
This initializes the variable
count
to zero.
for f in *; do
This starts a loop over all files in the current directory.
[ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"
[ -f "$f" ]
tests to see if the file$f
is a regular file (not a directory).
If
$f
is a regular file, then the move (mv
) command is run.-i
tellsmv
not to overwrite any existing files without asking."cin_$((++count)).${f##*.}"
is the name of the new file.cin_
is the prefix.$((++count))
returns the value ofcount
after it has been incremented.${f##*.}
is the extension of file$f
.
done
This marks the end of the loop.
Example
Consider a directory with these three files:
$ ls
alpha.jpg beta.png gamma.txt
Let's run our command:
$ count=0; for f in *; do [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"; done
After running our command, the files in the directory now are:
$ ls
cin_1.jpg cin_2.png cin_3.txt
Try:
count=0; for f in *; do [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"; done
Or, if you prefer your commands spread over multiple lines:
count=0
for f in *
do
[ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"
done
How it works
count=0
This initializes the variable
count
to zero.
for f in *; do
This starts a loop over all files in the current directory.
[ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"
[ -f "$f" ]
tests to see if the file$f
is a regular file (not a directory).
If
$f
is a regular file, then the move (mv
) command is run.-i
tellsmv
not to overwrite any existing files without asking."cin_$((++count)).${f##*.}"
is the name of the new file.cin_
is the prefix.$((++count))
returns the value ofcount
after it has been incremented.${f##*.}
is the extension of file$f
.
done
This marks the end of the loop.
Example
Consider a directory with these three files:
$ ls
alpha.jpg beta.png gamma.txt
Let's run our command:
$ count=0; for f in *; do [ -f "$f" ] && mv -i "$f" "cin_$((++count)).${f##*.}"; done
After running our command, the files in the directory now are:
$ ls
cin_1.jpg cin_2.png cin_3.txt
edited Jan 8 at 22:53
dessert
23.3k565103
23.3k565103
answered Jan 8 at 22:45
John1024John1024
9,9692434
9,9692434
1
That did exactly what I wanted, and it is super fast (faster than with Python). Thank you!
– singrium
Jan 8 at 22:50
add a comment |
1
That did exactly what I wanted, and it is super fast (faster than with Python). Thank you!
– singrium
Jan 8 at 22:50
1
1
That did exactly what I wanted, and it is super fast (faster than with Python). Thank you!
– singrium
Jan 8 at 22:50
That did exactly what I wanted, and it is super fast (faster than with Python). Thank you!
– singrium
Jan 8 at 22:50
add a comment |
A rename
oneliner:
rename -n 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
This matches every file with a dot in its name (the last one, if multiple) and renames the part until the dot with cin_
followed by an incrementing number and a dot. With the -n
flag it just prints what it would do, remove it to perform the renaming.
Example run
$ ls
a.jpg b.jpg d.png e.png
$ rename -n 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
rename(a.jpg, cin_1.jpg)
rename(b.jpg, cin_2.jpg)
rename(d.png, cin_3.png)
rename(e.png, cin_4.png)
$ rename 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
$ ls
cin_1.jpg cin_2.jpg cin_3.png cin_4.png
Source: How to rename multiple files sequentially from command line?
add a comment |
A rename
oneliner:
rename -n 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
This matches every file with a dot in its name (the last one, if multiple) and renames the part until the dot with cin_
followed by an incrementing number and a dot. With the -n
flag it just prints what it would do, remove it to perform the renaming.
Example run
$ ls
a.jpg b.jpg d.png e.png
$ rename -n 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
rename(a.jpg, cin_1.jpg)
rename(b.jpg, cin_2.jpg)
rename(d.png, cin_3.png)
rename(e.png, cin_4.png)
$ rename 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
$ ls
cin_1.jpg cin_2.jpg cin_3.png cin_4.png
Source: How to rename multiple files sequentially from command line?
add a comment |
A rename
oneliner:
rename -n 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
This matches every file with a dot in its name (the last one, if multiple) and renames the part until the dot with cin_
followed by an incrementing number and a dot. With the -n
flag it just prints what it would do, remove it to perform the renaming.
Example run
$ ls
a.jpg b.jpg d.png e.png
$ rename -n 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
rename(a.jpg, cin_1.jpg)
rename(b.jpg, cin_2.jpg)
rename(d.png, cin_3.png)
rename(e.png, cin_4.png)
$ rename 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
$ ls
cin_1.jpg cin_2.jpg cin_3.png cin_4.png
Source: How to rename multiple files sequentially from command line?
A rename
oneliner:
rename -n 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
This matches every file with a dot in its name (the last one, if multiple) and renames the part until the dot with cin_
followed by an incrementing number and a dot. With the -n
flag it just prints what it would do, remove it to perform the renaming.
Example run
$ ls
a.jpg b.jpg d.png e.png
$ rename -n 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
rename(a.jpg, cin_1.jpg)
rename(b.jpg, cin_2.jpg)
rename(d.png, cin_3.png)
rename(e.png, cin_4.png)
$ rename 's/.+./our $i; sprintf("cin_%d.", 1+$i++)/e' *
$ ls
cin_1.jpg cin_2.jpg cin_3.png cin_4.png
Source: How to rename multiple files sequentially from command line?
answered Jan 8 at 22:52
dessertdessert
23.3k565103
23.3k565103
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- 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%2faskubuntu.com%2fquestions%2f1108141%2frename-multiple-files-sequentially-and-starting-by-the-same-prefix%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
Likely related: Sequential renaming of files
– steeldriver
Jan 8 at 22:42