How to rename in ascending order of files in a folder
up vote
0
down vote
favorite
I have a folder named FOLDER and inside, I have files named
file.2.fna
file.5.fna
file.6.fna
file.8.fna
file.12.fna
I want to rename the number part of each file in an ascending order starting with 0.
The desired output is
file.0.fna
file.1.fna
file.2.fna
file.3.fna
file.4.fna
I can do this manually by
mv FOLDER/file.2.fna FOLDER/file.0.fna
rm FOLDER/file.2.fna
But it does not seem appropriate since I have several dozens of files
Is there a for loop to do the job?
The for loop I've tried is
for file in FOLDER/file.*.fna; do n=0; mv file FOLDER/file.${file}.fna
FOLDER/file.$n.fna; n=$n+1 ; done
This results in only single file with index 0.
I'm not sure how to increase n by 1 here.
Thank you
for-loop rename
add a comment |
up vote
0
down vote
favorite
I have a folder named FOLDER and inside, I have files named
file.2.fna
file.5.fna
file.6.fna
file.8.fna
file.12.fna
I want to rename the number part of each file in an ascending order starting with 0.
The desired output is
file.0.fna
file.1.fna
file.2.fna
file.3.fna
file.4.fna
I can do this manually by
mv FOLDER/file.2.fna FOLDER/file.0.fna
rm FOLDER/file.2.fna
But it does not seem appropriate since I have several dozens of files
Is there a for loop to do the job?
The for loop I've tried is
for file in FOLDER/file.*.fna; do n=0; mv file FOLDER/file.${file}.fna
FOLDER/file.$n.fna; n=$n+1 ; done
This results in only single file with index 0.
I'm not sure how to increase n by 1 here.
Thank you
for-loop rename
I would start by looking atls -1which sorts your files alphabetically. You could then write a loop to rename your ordered files as you wish.
– Bishal
Nov 12 at 6:31
Hi @Bishal, I inserted the for loop code I've tried. Still have some problems.Thank you.
– Sumin Kim
Nov 12 at 6:37
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a folder named FOLDER and inside, I have files named
file.2.fna
file.5.fna
file.6.fna
file.8.fna
file.12.fna
I want to rename the number part of each file in an ascending order starting with 0.
The desired output is
file.0.fna
file.1.fna
file.2.fna
file.3.fna
file.4.fna
I can do this manually by
mv FOLDER/file.2.fna FOLDER/file.0.fna
rm FOLDER/file.2.fna
But it does not seem appropriate since I have several dozens of files
Is there a for loop to do the job?
The for loop I've tried is
for file in FOLDER/file.*.fna; do n=0; mv file FOLDER/file.${file}.fna
FOLDER/file.$n.fna; n=$n+1 ; done
This results in only single file with index 0.
I'm not sure how to increase n by 1 here.
Thank you
for-loop rename
I have a folder named FOLDER and inside, I have files named
file.2.fna
file.5.fna
file.6.fna
file.8.fna
file.12.fna
I want to rename the number part of each file in an ascending order starting with 0.
The desired output is
file.0.fna
file.1.fna
file.2.fna
file.3.fna
file.4.fna
I can do this manually by
mv FOLDER/file.2.fna FOLDER/file.0.fna
rm FOLDER/file.2.fna
But it does not seem appropriate since I have several dozens of files
Is there a for loop to do the job?
The for loop I've tried is
for file in FOLDER/file.*.fna; do n=0; mv file FOLDER/file.${file}.fna
FOLDER/file.$n.fna; n=$n+1 ; done
This results in only single file with index 0.
I'm not sure how to increase n by 1 here.
Thank you
for-loop rename
for-loop rename
edited Nov 12 at 7:00
asked Nov 12 at 6:26
Sumin Kim
938
938
I would start by looking atls -1which sorts your files alphabetically. You could then write a loop to rename your ordered files as you wish.
– Bishal
Nov 12 at 6:31
Hi @Bishal, I inserted the for loop code I've tried. Still have some problems.Thank you.
– Sumin Kim
Nov 12 at 6:37
add a comment |
I would start by looking atls -1which sorts your files alphabetically. You could then write a loop to rename your ordered files as you wish.
– Bishal
Nov 12 at 6:31
Hi @Bishal, I inserted the for loop code I've tried. Still have some problems.Thank you.
– Sumin Kim
Nov 12 at 6:37
I would start by looking at
ls -1 which sorts your files alphabetically. You could then write a loop to rename your ordered files as you wish.– Bishal
Nov 12 at 6:31
I would start by looking at
ls -1 which sorts your files alphabetically. You could then write a loop to rename your ordered files as you wish.– Bishal
Nov 12 at 6:31
Hi @Bishal, I inserted the for loop code I've tried. Still have some problems.Thank you.
– Sumin Kim
Nov 12 at 6:37
Hi @Bishal, I inserted the for loop code I've tried. Still have some problems.Thank you.
– Sumin Kim
Nov 12 at 6:37
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
I have got around this by
#!/bin/bash
a=0
echo $a
for i in file_*; do
new=$(printf "file_%d" "$a")
echo $new
mv -i -- "$i" "$new"
let a=a+1
done
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I have got around this by
#!/bin/bash
a=0
echo $a
for i in file_*; do
new=$(printf "file_%d" "$a")
echo $new
mv -i -- "$i" "$new"
let a=a+1
done
add a comment |
up vote
0
down vote
I have got around this by
#!/bin/bash
a=0
echo $a
for i in file_*; do
new=$(printf "file_%d" "$a")
echo $new
mv -i -- "$i" "$new"
let a=a+1
done
add a comment |
up vote
0
down vote
up vote
0
down vote
I have got around this by
#!/bin/bash
a=0
echo $a
for i in file_*; do
new=$(printf "file_%d" "$a")
echo $new
mv -i -- "$i" "$new"
let a=a+1
done
I have got around this by
#!/bin/bash
a=0
echo $a
for i in file_*; do
new=$(printf "file_%d" "$a")
echo $new
mv -i -- "$i" "$new"
let a=a+1
done
answered Nov 12 at 7:21
Sumin Kim
938
938
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%2f53256890%2fhow-to-rename-in-ascending-order-of-files-in-a-folder%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
I would start by looking at
ls -1which sorts your files alphabetically. You could then write a loop to rename your ordered files as you wish.– Bishal
Nov 12 at 6:31
Hi @Bishal, I inserted the for loop code I've tried. Still have some problems.Thank you.
– Sumin Kim
Nov 12 at 6:37