Python for Network Engineers

Rename Files From 1.Name or Name1 to 01Name

by: George El., January 2019, Reading time: 3 minutes

there is nothing more annoying than seeing unordered files, and this happens a lot when you have numbers in front of files like 1.file, 2.file etc. the command is

ls -1 | sed -r -e 's/([a-zA-z]+)([0-9]+)(\.txt)/\0 \2 \1 \3/' |  xargs -n4 printf "%s %02d%s%s\n" | xargs -n2 mv

and we will break it down

$ ls -1
10.file.txt
11.file.txt
12.file.txt
1.file.txt
2.file.txt
3.file.txt
4.file.txt
5.file.txt
6.file.txt
7.file.txt
8.file.txt
9.file.txt

we can use awk, grep, sed, to rename them, the logic is always the same. split the filename into logical groups and then move the parts however you like and then rename the file using mv oldfilename newfilename. so lets use sed which I think is easier. I have two groups \1 is the number part and \2 the rest

$ ls -1 | sed -r -e 's/([0-9]+)(\.\w+\.txt)/\1 \2/' | xargs -n2 
10 .file.txt
11 .file.txt
12 .file.txt
1 .file.txt
2 .file.txt
3 .file.txt
4 .file.txt
5 .file.txt
6 .file.txt
7 .file.txt
8 .file.txt
9 .file.txt

lets pass this arguments to printf in order to create the new file name

$ ls -1 | sed -r -e 's/([0-9]+)(\.\w+\.txt)/\1 \2/' | xargs -n2 printf "%02d%s\n"
10.file.txt
11.file.txt
12.file.txt
01.file.txt
02.file.txt
03.file.txt
04.file.txt
05.file.txt
06.file.txt
07.file.txt
08.file.txt
09.file.txt

Now i need also the old file name which I get from \0

$ls -1 | sed -r -e 's/([0-9]+)(\.\w+\.txt)/\0 \1 \2/' | xargs -n3 printf "%s %02d%s\n"
10.file.txt 10.file.txt
11.file.txt 11.file.txt
12.file.txt 12.file.txt
1.file.txt 01.file.txt
2.file.txt 02.file.txt
3.file.txt 03.file.txt
4.file.txt 04.file.txt
5.file.txt 05.file.txt
6.file.txt 06.file.txt
7.file.txt 07.file.txt
8.file.txt 08.file.txt
9.file.txt 09.file.txt

I need to pass each line to xargs and do a mv

$ ls -1 | sed -r -e 's/([0-9]+)(\.\w+\.txt)/\0 \1 \2/' | xargs -n3 printf "%s %02d%s\n" | xargs -n2 -t mv
mv 10.file.txt 10.file.txt 
mv: '10.file.txt' and '10.file.txt' are the same file
mv 11.file.txt 11.file.txt 
mv: '11.file.txt' and '11.file.txt' are the same file
mv 12.file.txt 12.file.txt 
mv: '12.file.txt' and '12.file.txt' are the same file
mv 1.file.txt 01.file.txt 
mv 2.file.txt 02.file.txt 
mv 3.file.txt 03.file.txt 
mv 4.file.txt 04.file.txt 
mv 5.file.txt 05.file.txt 
mv 6.file.txt 06.file.txt 
mv 7.file.txt 07.file.txt 
mv 8.file.txt 08.file.txt 
mv 9.file.txt 09.file.txt 

the output is now

$ ls -1
01.file.txt
02.file.txt
03.file.txt
04.file.txt
05.file.txt
06.file.txt
07.file.txt
08.file.txt
09.file.txt
10.file.txt
11.file.txt
12.file.txt

Now lets do the same but now the files are file1.txt file2.txt etc

ls -1
file10.txt
file11.txt
file12.txt
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
file6.txt
file7.txt
file8.txt
file9.txt

Now I have to split it in three groups, the file, the numbers and the extension because \w matches also numbers, i replaced \w+ with [a-zA-z]+ Now obviously if your filename has also other characters you have to modify the captured group accordingly

$ ls -1 | sed -r -e 's/([a-zA-z]+)([0-9]+)(\.txt)/ \2 \1 \3/'
 10 file .txt
 11 file .txt
 12 file .txt
 1 file .txt
 2 file .txt
 3 file .txt
 4 file .txt
 5 file .txt
 6 file .txt
 7 file .txt
 8 file .txt
 9 file .txt
$ ls -1 | sed -r -e 's/([a-zA-z]+)([0-9]+)(\.txt)/ \2 \1 \3/' |  xargs -n3 printf "%02d%s%s\n"
10file.txt
11file.txt
12file.txt
01file.txt
02file.txt
03file.txt
04file.txt
05file.txt
06file.txt
07file.txt
08file.txt
09file.txt
$ ls -1 | sed -r -e 's/([a-zA-z]+)([0-9]+)(\.txt)/\0 \2 \1 \3/' |  xargs -n4 printf "%s %02d%s%s\n"
file10.txt 10file.txt
file11.txt 11file.txt
file12.txt 12file.txt
file1.txt 01file.txt
file2.txt 02file.txt
file3.txt 03file.txt
file4.txt 04file.txt
file5.txt 05file.txt
file6.txt 06file.txt
file7.txt 07file.txt
file8.txt 08file.txt
file9.txt 09file.txt
$ ls -1 | sed -r -e 's/([a-zA-z]+)([0-9]+)(\.txt)/\0 \2 \1 \3/' |  xargs -n4 printf "%s %02d%s%s\n" | xargs -n2 mv
$ ls -1
01file.txt
02file.txt
03file.txt
04file.txt
05file.txt
06file.txt
07file.txt
08file.txt
09file.txt
10file.txt
11file.txt
12file.txt
comments powered by Disqus