How can I prepend filenames with ascending numbers like 1_ 2_?
up vote
2
down vote
favorite
How can I add numbers to the files in one directory?
In one directory I have files like below:
fileA
fileB
fileC
fileD
I want to prepend ascending numbers to them, like this:
1_fileA
2_fileB
3_fileC
4_fileD
Thank you in advance.
command-line bash batch-rename
New contributor
add a comment |
up vote
2
down vote
favorite
How can I add numbers to the files in one directory?
In one directory I have files like below:
fileA
fileB
fileC
fileD
I want to prepend ascending numbers to them, like this:
1_fileA
2_fileB
3_fileC
4_fileD
Thank you in advance.
command-line bash batch-rename
New contributor
So you want to rename the files?
– PerlDuck
yesterday
@PerlDuck Yes, I have to enumarte them changing their name.
– paweljvn
yesterday
Related: askubuntu.com/q/839959
– Justin
22 hours ago
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
How can I add numbers to the files in one directory?
In one directory I have files like below:
fileA
fileB
fileC
fileD
I want to prepend ascending numbers to them, like this:
1_fileA
2_fileB
3_fileC
4_fileD
Thank you in advance.
command-line bash batch-rename
New contributor
How can I add numbers to the files in one directory?
In one directory I have files like below:
fileA
fileB
fileC
fileD
I want to prepend ascending numbers to them, like this:
1_fileA
2_fileB
3_fileC
4_fileD
Thank you in advance.
command-line bash batch-rename
command-line bash batch-rename
New contributor
New contributor
edited yesterday
Zanna
49k13123234
49k13123234
New contributor
asked yesterday
paweljvn
185
185
New contributor
New contributor
So you want to rename the files?
– PerlDuck
yesterday
@PerlDuck Yes, I have to enumarte them changing their name.
– paweljvn
yesterday
Related: askubuntu.com/q/839959
– Justin
22 hours ago
add a comment |
So you want to rename the files?
– PerlDuck
yesterday
@PerlDuck Yes, I have to enumarte them changing their name.
– paweljvn
yesterday
Related: askubuntu.com/q/839959
– Justin
22 hours ago
So you want to rename the files?
– PerlDuck
yesterday
So you want to rename the files?
– PerlDuck
yesterday
@PerlDuck Yes, I have to enumarte them changing their name.
– paweljvn
yesterday
@PerlDuck Yes, I have to enumarte them changing their name.
– paweljvn
yesterday
Related: askubuntu.com/q/839959
– Justin
22 hours ago
Related: askubuntu.com/q/839959
– Justin
22 hours ago
add a comment |
4 Answers
4
active
oldest
votes
up vote
4
down vote
accepted
One of the solutions:
cd <your dir>
then run in bash (copy and paste in command-line):
n=1; for f in *; do mv "$f" "$((n++))_$f"; done
Bash script case:
#!/bin/bash
n=1
for f in *
do
if [ "$f" = "rename.sh" ]
then
continue
fi
mv "$f" "$((n++))_$f"
done
save it as rename.sh
to dir with files to rename, chmod +x rename.sh
then run it ./rename.sh
but can I also run this from vim? I mean to put your code in vim as #!/bin/bash and the run it from the command line?
– paweljvn
yesterday
Sure. But you need add this simple command to bash file. Then You need to solve self-renaming of this rename.sh file.
– S_Flash
yesterday
1
Added example with bash file. If you need something like bash command to run it in any file system place, you need use first example saved in bash file and add this bash file to PATH with alias.
– S_Flash
yesterday
add a comment |
up vote
4
down vote
If there are more than 9 files, I would use printf
to pad the number to get the expected sort order, like this
n=0
for f in *
do printf -v new "%2d$((++n))_$f"
echo mv -v -- "$f" "$new"
done
Remove echo
when you see the correct result.
Explanation
In this line, do printf -v new "%2d$((++n))_$f"
we create a format for the new filenames and put it into the variable new
.
%2d
is a 2 digit decimal number. Instead of 2d
, you can use 3d
etc to get another leading 0 (if you have more than 99 files).
((++n))
increments the variable n
(which we set to 0 at the start of the script. Since it is iterated once each time the loop is run, files get incremented name prefixes.
-v
makes mv
print what will be changed.
--
in the mv
statement is to prevent filenames that start with -
being interpreted as options.
add a comment |
up vote
2
down vote
That’s a job for rename
:
rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
This defines a variable, increases it if it’s not set (else it would start with 0
instead of 1
) and replaces the beginning of the filename with the number increasing it every time. You can change the format easily, e.g. to make it three-digit (001
, 002
, …) use "%03d_"
. Running it with -n
only prints the changes, to actually perform the renaming remove this flag.
Example run
$ ls -1
fileA
fileB
fileC
fileD
$ rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
rename(fileA, 1_fileA)
rename(fileB, 2_fileB)
rename(fileC, 3_fileC)
rename(fileD, 4_fileD)
$ rename 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
$ ls -1
1_fileA
2_fileB
3_fileC
4_fileD
add a comment |
up vote
0
down vote
One option is
cd /path/to/folder/
ls -1v | rename -n -v 's/^/sprintf("%02d_", ++our$i)/e'
This works fine, except with the file names with new line "n". Switch '-1v'takes care of spaces and tabs,
Other commands posted here change the order of files with numbers e.g. 10a comes before 1a.
Whichever suits in a situation.
I had some time to test all these commands. Here are the results.
$ ls
001abc.txt '10a bc.txt' 1abc.txt '2ab c.txt' 'a'$'t''bc.txt'
$ ls -1v | rename -n -v 's/^/sprintf("%02d_", ++our$i)/e'
Reading filenames from file handle (GLOB(0x55cb57991b28))
rename(001abc.txt, 01_001abc.txt)
rename(1abc.txt, 02_1abc.txt)
rename(2ab c.txt, 03_2ab c.txt)
rename(10a bc.txt, 04_10a bc.txt)
rename(a bc.txt, 05_a bc.txt)
$ n=1; for f in *; do echo mv "$f" "$((n++))_$f"; done
mv 001abc.txt 1_001abc.txt
mv 10a bc.txt 2_10a bc.txt
mv 1abc.txt 3_1abc.txt
mv 2ab c.txt 4_2ab c.txt
mv a bc.txt 5_a bc.txt
$ for f in *; do printf -v new "%2d$((++n))_$f"; echo mv -v -- "$f" "$new"; done
mv -v -- 001abc.txt 09_001abc.txt
mv -v -- 10a bc.txt 010_10a bc.txt
mv -v -- 1abc.txt 011_1abc.txt
mv -v -- 2ab c.txt 012_2ab c.txt
mv -v -- a bc.txt 013_a bc.txt
$ ./rename.sh
mv 001abc.txt 1_001abc.txt
mv 10a bc.txt 2_10a bc.txt
mv 1abc.txt 3_1abc.txt
mv 2ab c.txt 4_2ab c.txt
mv a bc.txt 5_a bc.txt
$ rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
rename(001abc.txt, 1_001abc.txt)
rename(10a bc.txt, 2_10a bc.txt)
rename(1abc.txt, 3_1abc.txt)
rename(2ab c.txt, 4_2ab c.txt)
rename(a bc.txt, 5_a bc.txt)
rename(rename.sh, 6_rename.sh)
$ for f in *; do printf -v new "%2d$((++n))_$f"; echo mv -v -- "$f" "$new"; done
mv -v -- 001abc.txt 01_001abc.txt
mv -v -- 10a bc.txt 02_10a bc.txt
mv -v -- 1abc.txt 03_1abc.txt
mv -v -- 2ab c.txt 04_2ab c.txt
mv -v -- a bc.txt 05_a bc.txt
mv -v -- rename.sh 06_rename.sh
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
One of the solutions:
cd <your dir>
then run in bash (copy and paste in command-line):
n=1; for f in *; do mv "$f" "$((n++))_$f"; done
Bash script case:
#!/bin/bash
n=1
for f in *
do
if [ "$f" = "rename.sh" ]
then
continue
fi
mv "$f" "$((n++))_$f"
done
save it as rename.sh
to dir with files to rename, chmod +x rename.sh
then run it ./rename.sh
but can I also run this from vim? I mean to put your code in vim as #!/bin/bash and the run it from the command line?
– paweljvn
yesterday
Sure. But you need add this simple command to bash file. Then You need to solve self-renaming of this rename.sh file.
– S_Flash
yesterday
1
Added example with bash file. If you need something like bash command to run it in any file system place, you need use first example saved in bash file and add this bash file to PATH with alias.
– S_Flash
yesterday
add a comment |
up vote
4
down vote
accepted
One of the solutions:
cd <your dir>
then run in bash (copy and paste in command-line):
n=1; for f in *; do mv "$f" "$((n++))_$f"; done
Bash script case:
#!/bin/bash
n=1
for f in *
do
if [ "$f" = "rename.sh" ]
then
continue
fi
mv "$f" "$((n++))_$f"
done
save it as rename.sh
to dir with files to rename, chmod +x rename.sh
then run it ./rename.sh
but can I also run this from vim? I mean to put your code in vim as #!/bin/bash and the run it from the command line?
– paweljvn
yesterday
Sure. But you need add this simple command to bash file. Then You need to solve self-renaming of this rename.sh file.
– S_Flash
yesterday
1
Added example with bash file. If you need something like bash command to run it in any file system place, you need use first example saved in bash file and add this bash file to PATH with alias.
– S_Flash
yesterday
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
One of the solutions:
cd <your dir>
then run in bash (copy and paste in command-line):
n=1; for f in *; do mv "$f" "$((n++))_$f"; done
Bash script case:
#!/bin/bash
n=1
for f in *
do
if [ "$f" = "rename.sh" ]
then
continue
fi
mv "$f" "$((n++))_$f"
done
save it as rename.sh
to dir with files to rename, chmod +x rename.sh
then run it ./rename.sh
One of the solutions:
cd <your dir>
then run in bash (copy and paste in command-line):
n=1; for f in *; do mv "$f" "$((n++))_$f"; done
Bash script case:
#!/bin/bash
n=1
for f in *
do
if [ "$f" = "rename.sh" ]
then
continue
fi
mv "$f" "$((n++))_$f"
done
save it as rename.sh
to dir with files to rename, chmod +x rename.sh
then run it ./rename.sh
edited yesterday
answered yesterday
S_Flash
1,007117
1,007117
but can I also run this from vim? I mean to put your code in vim as #!/bin/bash and the run it from the command line?
– paweljvn
yesterday
Sure. But you need add this simple command to bash file. Then You need to solve self-renaming of this rename.sh file.
– S_Flash
yesterday
1
Added example with bash file. If you need something like bash command to run it in any file system place, you need use first example saved in bash file and add this bash file to PATH with alias.
– S_Flash
yesterday
add a comment |
but can I also run this from vim? I mean to put your code in vim as #!/bin/bash and the run it from the command line?
– paweljvn
yesterday
Sure. But you need add this simple command to bash file. Then You need to solve self-renaming of this rename.sh file.
– S_Flash
yesterday
1
Added example with bash file. If you need something like bash command to run it in any file system place, you need use first example saved in bash file and add this bash file to PATH with alias.
– S_Flash
yesterday
but can I also run this from vim? I mean to put your code in vim as #!/bin/bash and the run it from the command line?
– paweljvn
yesterday
but can I also run this from vim? I mean to put your code in vim as #!/bin/bash and the run it from the command line?
– paweljvn
yesterday
Sure. But you need add this simple command to bash file. Then You need to solve self-renaming of this rename.sh file.
– S_Flash
yesterday
Sure. But you need add this simple command to bash file. Then You need to solve self-renaming of this rename.sh file.
– S_Flash
yesterday
1
1
Added example with bash file. If you need something like bash command to run it in any file system place, you need use first example saved in bash file and add this bash file to PATH with alias.
– S_Flash
yesterday
Added example with bash file. If you need something like bash command to run it in any file system place, you need use first example saved in bash file and add this bash file to PATH with alias.
– S_Flash
yesterday
add a comment |
up vote
4
down vote
If there are more than 9 files, I would use printf
to pad the number to get the expected sort order, like this
n=0
for f in *
do printf -v new "%2d$((++n))_$f"
echo mv -v -- "$f" "$new"
done
Remove echo
when you see the correct result.
Explanation
In this line, do printf -v new "%2d$((++n))_$f"
we create a format for the new filenames and put it into the variable new
.
%2d
is a 2 digit decimal number. Instead of 2d
, you can use 3d
etc to get another leading 0 (if you have more than 99 files).
((++n))
increments the variable n
(which we set to 0 at the start of the script. Since it is iterated once each time the loop is run, files get incremented name prefixes.
-v
makes mv
print what will be changed.
--
in the mv
statement is to prevent filenames that start with -
being interpreted as options.
add a comment |
up vote
4
down vote
If there are more than 9 files, I would use printf
to pad the number to get the expected sort order, like this
n=0
for f in *
do printf -v new "%2d$((++n))_$f"
echo mv -v -- "$f" "$new"
done
Remove echo
when you see the correct result.
Explanation
In this line, do printf -v new "%2d$((++n))_$f"
we create a format for the new filenames and put it into the variable new
.
%2d
is a 2 digit decimal number. Instead of 2d
, you can use 3d
etc to get another leading 0 (if you have more than 99 files).
((++n))
increments the variable n
(which we set to 0 at the start of the script. Since it is iterated once each time the loop is run, files get incremented name prefixes.
-v
makes mv
print what will be changed.
--
in the mv
statement is to prevent filenames that start with -
being interpreted as options.
add a comment |
up vote
4
down vote
up vote
4
down vote
If there are more than 9 files, I would use printf
to pad the number to get the expected sort order, like this
n=0
for f in *
do printf -v new "%2d$((++n))_$f"
echo mv -v -- "$f" "$new"
done
Remove echo
when you see the correct result.
Explanation
In this line, do printf -v new "%2d$((++n))_$f"
we create a format for the new filenames and put it into the variable new
.
%2d
is a 2 digit decimal number. Instead of 2d
, you can use 3d
etc to get another leading 0 (if you have more than 99 files).
((++n))
increments the variable n
(which we set to 0 at the start of the script. Since it is iterated once each time the loop is run, files get incremented name prefixes.
-v
makes mv
print what will be changed.
--
in the mv
statement is to prevent filenames that start with -
being interpreted as options.
If there are more than 9 files, I would use printf
to pad the number to get the expected sort order, like this
n=0
for f in *
do printf -v new "%2d$((++n))_$f"
echo mv -v -- "$f" "$new"
done
Remove echo
when you see the correct result.
Explanation
In this line, do printf -v new "%2d$((++n))_$f"
we create a format for the new filenames and put it into the variable new
.
%2d
is a 2 digit decimal number. Instead of 2d
, you can use 3d
etc to get another leading 0 (if you have more than 99 files).
((++n))
increments the variable n
(which we set to 0 at the start of the script. Since it is iterated once each time the loop is run, files get incremented name prefixes.
-v
makes mv
print what will be changed.
--
in the mv
statement is to prevent filenames that start with -
being interpreted as options.
answered yesterday
Zanna
49k13123234
49k13123234
add a comment |
add a comment |
up vote
2
down vote
That’s a job for rename
:
rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
This defines a variable, increases it if it’s not set (else it would start with 0
instead of 1
) and replaces the beginning of the filename with the number increasing it every time. You can change the format easily, e.g. to make it three-digit (001
, 002
, …) use "%03d_"
. Running it with -n
only prints the changes, to actually perform the renaming remove this flag.
Example run
$ ls -1
fileA
fileB
fileC
fileD
$ rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
rename(fileA, 1_fileA)
rename(fileB, 2_fileB)
rename(fileC, 3_fileC)
rename(fileD, 4_fileD)
$ rename 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
$ ls -1
1_fileA
2_fileB
3_fileC
4_fileD
add a comment |
up vote
2
down vote
That’s a job for rename
:
rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
This defines a variable, increases it if it’s not set (else it would start with 0
instead of 1
) and replaces the beginning of the filename with the number increasing it every time. You can change the format easily, e.g. to make it three-digit (001
, 002
, …) use "%03d_"
. Running it with -n
only prints the changes, to actually perform the renaming remove this flag.
Example run
$ ls -1
fileA
fileB
fileC
fileD
$ rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
rename(fileA, 1_fileA)
rename(fileB, 2_fileB)
rename(fileC, 3_fileC)
rename(fileD, 4_fileD)
$ rename 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
$ ls -1
1_fileA
2_fileB
3_fileC
4_fileD
add a comment |
up vote
2
down vote
up vote
2
down vote
That’s a job for rename
:
rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
This defines a variable, increases it if it’s not set (else it would start with 0
instead of 1
) and replaces the beginning of the filename with the number increasing it every time. You can change the format easily, e.g. to make it three-digit (001
, 002
, …) use "%03d_"
. Running it with -n
only prints the changes, to actually perform the renaming remove this flag.
Example run
$ ls -1
fileA
fileB
fileC
fileD
$ rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
rename(fileA, 1_fileA)
rename(fileB, 2_fileB)
rename(fileC, 3_fileC)
rename(fileD, 4_fileD)
$ rename 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
$ ls -1
1_fileA
2_fileB
3_fileC
4_fileD
That’s a job for rename
:
rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
This defines a variable, increases it if it’s not set (else it would start with 0
instead of 1
) and replaces the beginning of the filename with the number increasing it every time. You can change the format easily, e.g. to make it three-digit (001
, 002
, …) use "%03d_"
. Running it with -n
only prints the changes, to actually perform the renaming remove this flag.
Example run
$ ls -1
fileA
fileB
fileC
fileD
$ rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
rename(fileA, 1_fileA)
rename(fileB, 2_fileB)
rename(fileC, 3_fileC)
rename(fileD, 4_fileD)
$ rename 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
$ ls -1
1_fileA
2_fileB
3_fileC
4_fileD
answered yesterday
dessert
21.2k55896
21.2k55896
add a comment |
add a comment |
up vote
0
down vote
One option is
cd /path/to/folder/
ls -1v | rename -n -v 's/^/sprintf("%02d_", ++our$i)/e'
This works fine, except with the file names with new line "n". Switch '-1v'takes care of spaces and tabs,
Other commands posted here change the order of files with numbers e.g. 10a comes before 1a.
Whichever suits in a situation.
I had some time to test all these commands. Here are the results.
$ ls
001abc.txt '10a bc.txt' 1abc.txt '2ab c.txt' 'a'$'t''bc.txt'
$ ls -1v | rename -n -v 's/^/sprintf("%02d_", ++our$i)/e'
Reading filenames from file handle (GLOB(0x55cb57991b28))
rename(001abc.txt, 01_001abc.txt)
rename(1abc.txt, 02_1abc.txt)
rename(2ab c.txt, 03_2ab c.txt)
rename(10a bc.txt, 04_10a bc.txt)
rename(a bc.txt, 05_a bc.txt)
$ n=1; for f in *; do echo mv "$f" "$((n++))_$f"; done
mv 001abc.txt 1_001abc.txt
mv 10a bc.txt 2_10a bc.txt
mv 1abc.txt 3_1abc.txt
mv 2ab c.txt 4_2ab c.txt
mv a bc.txt 5_a bc.txt
$ for f in *; do printf -v new "%2d$((++n))_$f"; echo mv -v -- "$f" "$new"; done
mv -v -- 001abc.txt 09_001abc.txt
mv -v -- 10a bc.txt 010_10a bc.txt
mv -v -- 1abc.txt 011_1abc.txt
mv -v -- 2ab c.txt 012_2ab c.txt
mv -v -- a bc.txt 013_a bc.txt
$ ./rename.sh
mv 001abc.txt 1_001abc.txt
mv 10a bc.txt 2_10a bc.txt
mv 1abc.txt 3_1abc.txt
mv 2ab c.txt 4_2ab c.txt
mv a bc.txt 5_a bc.txt
$ rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
rename(001abc.txt, 1_001abc.txt)
rename(10a bc.txt, 2_10a bc.txt)
rename(1abc.txt, 3_1abc.txt)
rename(2ab c.txt, 4_2ab c.txt)
rename(a bc.txt, 5_a bc.txt)
rename(rename.sh, 6_rename.sh)
$ for f in *; do printf -v new "%2d$((++n))_$f"; echo mv -v -- "$f" "$new"; done
mv -v -- 001abc.txt 01_001abc.txt
mv -v -- 10a bc.txt 02_10a bc.txt
mv -v -- 1abc.txt 03_1abc.txt
mv -v -- 2ab c.txt 04_2ab c.txt
mv -v -- a bc.txt 05_a bc.txt
mv -v -- rename.sh 06_rename.sh
add a comment |
up vote
0
down vote
One option is
cd /path/to/folder/
ls -1v | rename -n -v 's/^/sprintf("%02d_", ++our$i)/e'
This works fine, except with the file names with new line "n". Switch '-1v'takes care of spaces and tabs,
Other commands posted here change the order of files with numbers e.g. 10a comes before 1a.
Whichever suits in a situation.
I had some time to test all these commands. Here are the results.
$ ls
001abc.txt '10a bc.txt' 1abc.txt '2ab c.txt' 'a'$'t''bc.txt'
$ ls -1v | rename -n -v 's/^/sprintf("%02d_", ++our$i)/e'
Reading filenames from file handle (GLOB(0x55cb57991b28))
rename(001abc.txt, 01_001abc.txt)
rename(1abc.txt, 02_1abc.txt)
rename(2ab c.txt, 03_2ab c.txt)
rename(10a bc.txt, 04_10a bc.txt)
rename(a bc.txt, 05_a bc.txt)
$ n=1; for f in *; do echo mv "$f" "$((n++))_$f"; done
mv 001abc.txt 1_001abc.txt
mv 10a bc.txt 2_10a bc.txt
mv 1abc.txt 3_1abc.txt
mv 2ab c.txt 4_2ab c.txt
mv a bc.txt 5_a bc.txt
$ for f in *; do printf -v new "%2d$((++n))_$f"; echo mv -v -- "$f" "$new"; done
mv -v -- 001abc.txt 09_001abc.txt
mv -v -- 10a bc.txt 010_10a bc.txt
mv -v -- 1abc.txt 011_1abc.txt
mv -v -- 2ab c.txt 012_2ab c.txt
mv -v -- a bc.txt 013_a bc.txt
$ ./rename.sh
mv 001abc.txt 1_001abc.txt
mv 10a bc.txt 2_10a bc.txt
mv 1abc.txt 3_1abc.txt
mv 2ab c.txt 4_2ab c.txt
mv a bc.txt 5_a bc.txt
$ rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
rename(001abc.txt, 1_001abc.txt)
rename(10a bc.txt, 2_10a bc.txt)
rename(1abc.txt, 3_1abc.txt)
rename(2ab c.txt, 4_2ab c.txt)
rename(a bc.txt, 5_a bc.txt)
rename(rename.sh, 6_rename.sh)
$ for f in *; do printf -v new "%2d$((++n))_$f"; echo mv -v -- "$f" "$new"; done
mv -v -- 001abc.txt 01_001abc.txt
mv -v -- 10a bc.txt 02_10a bc.txt
mv -v -- 1abc.txt 03_1abc.txt
mv -v -- 2ab c.txt 04_2ab c.txt
mv -v -- a bc.txt 05_a bc.txt
mv -v -- rename.sh 06_rename.sh
add a comment |
up vote
0
down vote
up vote
0
down vote
One option is
cd /path/to/folder/
ls -1v | rename -n -v 's/^/sprintf("%02d_", ++our$i)/e'
This works fine, except with the file names with new line "n". Switch '-1v'takes care of spaces and tabs,
Other commands posted here change the order of files with numbers e.g. 10a comes before 1a.
Whichever suits in a situation.
I had some time to test all these commands. Here are the results.
$ ls
001abc.txt '10a bc.txt' 1abc.txt '2ab c.txt' 'a'$'t''bc.txt'
$ ls -1v | rename -n -v 's/^/sprintf("%02d_", ++our$i)/e'
Reading filenames from file handle (GLOB(0x55cb57991b28))
rename(001abc.txt, 01_001abc.txt)
rename(1abc.txt, 02_1abc.txt)
rename(2ab c.txt, 03_2ab c.txt)
rename(10a bc.txt, 04_10a bc.txt)
rename(a bc.txt, 05_a bc.txt)
$ n=1; for f in *; do echo mv "$f" "$((n++))_$f"; done
mv 001abc.txt 1_001abc.txt
mv 10a bc.txt 2_10a bc.txt
mv 1abc.txt 3_1abc.txt
mv 2ab c.txt 4_2ab c.txt
mv a bc.txt 5_a bc.txt
$ for f in *; do printf -v new "%2d$((++n))_$f"; echo mv -v -- "$f" "$new"; done
mv -v -- 001abc.txt 09_001abc.txt
mv -v -- 10a bc.txt 010_10a bc.txt
mv -v -- 1abc.txt 011_1abc.txt
mv -v -- 2ab c.txt 012_2ab c.txt
mv -v -- a bc.txt 013_a bc.txt
$ ./rename.sh
mv 001abc.txt 1_001abc.txt
mv 10a bc.txt 2_10a bc.txt
mv 1abc.txt 3_1abc.txt
mv 2ab c.txt 4_2ab c.txt
mv a bc.txt 5_a bc.txt
$ rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
rename(001abc.txt, 1_001abc.txt)
rename(10a bc.txt, 2_10a bc.txt)
rename(1abc.txt, 3_1abc.txt)
rename(2ab c.txt, 4_2ab c.txt)
rename(a bc.txt, 5_a bc.txt)
rename(rename.sh, 6_rename.sh)
$ for f in *; do printf -v new "%2d$((++n))_$f"; echo mv -v -- "$f" "$new"; done
mv -v -- 001abc.txt 01_001abc.txt
mv -v -- 10a bc.txt 02_10a bc.txt
mv -v -- 1abc.txt 03_1abc.txt
mv -v -- 2ab c.txt 04_2ab c.txt
mv -v -- a bc.txt 05_a bc.txt
mv -v -- rename.sh 06_rename.sh
One option is
cd /path/to/folder/
ls -1v | rename -n -v 's/^/sprintf("%02d_", ++our$i)/e'
This works fine, except with the file names with new line "n". Switch '-1v'takes care of spaces and tabs,
Other commands posted here change the order of files with numbers e.g. 10a comes before 1a.
Whichever suits in a situation.
I had some time to test all these commands. Here are the results.
$ ls
001abc.txt '10a bc.txt' 1abc.txt '2ab c.txt' 'a'$'t''bc.txt'
$ ls -1v | rename -n -v 's/^/sprintf("%02d_", ++our$i)/e'
Reading filenames from file handle (GLOB(0x55cb57991b28))
rename(001abc.txt, 01_001abc.txt)
rename(1abc.txt, 02_1abc.txt)
rename(2ab c.txt, 03_2ab c.txt)
rename(10a bc.txt, 04_10a bc.txt)
rename(a bc.txt, 05_a bc.txt)
$ n=1; for f in *; do echo mv "$f" "$((n++))_$f"; done
mv 001abc.txt 1_001abc.txt
mv 10a bc.txt 2_10a bc.txt
mv 1abc.txt 3_1abc.txt
mv 2ab c.txt 4_2ab c.txt
mv a bc.txt 5_a bc.txt
$ for f in *; do printf -v new "%2d$((++n))_$f"; echo mv -v -- "$f" "$new"; done
mv -v -- 001abc.txt 09_001abc.txt
mv -v -- 10a bc.txt 010_10a bc.txt
mv -v -- 1abc.txt 011_1abc.txt
mv -v -- 2ab c.txt 012_2ab c.txt
mv -v -- a bc.txt 013_a bc.txt
$ ./rename.sh
mv 001abc.txt 1_001abc.txt
mv 10a bc.txt 2_10a bc.txt
mv 1abc.txt 3_1abc.txt
mv 2ab c.txt 4_2ab c.txt
mv a bc.txt 5_a bc.txt
$ rename -n 'our $i; if (!$i) {$i++}; s/^/sprintf("%d_", $i++)/e' *
rename(001abc.txt, 1_001abc.txt)
rename(10a bc.txt, 2_10a bc.txt)
rename(1abc.txt, 3_1abc.txt)
rename(2ab c.txt, 4_2ab c.txt)
rename(a bc.txt, 5_a bc.txt)
rename(rename.sh, 6_rename.sh)
$ for f in *; do printf -v new "%2d$((++n))_$f"; echo mv -v -- "$f" "$new"; done
mv -v -- 001abc.txt 01_001abc.txt
mv -v -- 10a bc.txt 02_10a bc.txt
mv -v -- 1abc.txt 03_1abc.txt
mv -v -- 2ab c.txt 04_2ab c.txt
mv -v -- a bc.txt 05_a bc.txt
mv -v -- rename.sh 06_rename.sh
edited 13 hours ago
answered 14 hours ago
Hobbyist
989617
989617
add a comment |
add a comment |
paweljvn is a new contributor. Be nice, and check out our Code of Conduct.
paweljvn is a new contributor. Be nice, and check out our Code of Conduct.
paweljvn is a new contributor. Be nice, and check out our Code of Conduct.
paweljvn is a new contributor. Be nice, and check out our Code of Conduct.
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%2f1095456%2fhow-can-i-prepend-filenames-with-ascending-numbers-like-1-2%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
So you want to rename the files?
– PerlDuck
yesterday
@PerlDuck Yes, I have to enumarte them changing their name.
– paweljvn
yesterday
Related: askubuntu.com/q/839959
– Justin
22 hours ago