Question

I have 54 files in a directory who have the names from *file00_Layer-53.png* to *file52_Layer-1.png*. I would like to rename all these files to file01.png to file53.png

I know I have to use the rename command, but I am having difficulty using it. Thanks in advance

Was it helpful?

Solution

You can use the rename command:

c=0 rename 's/.*/sprintf("file%02d.png", ++$ENV{c})/e' *.png

Use the -n flag if you want to do a test first:

c=0 rename -n 's/.*/sprintf("file%02d.png", ++$ENV{c})/e' *.png

The way this works is, for each argument, it executes the perl s/// expression, and performs the rename from the original to the replaced string. In the replacement string I use sprintf to format the name, where I use the environment variable c as the counter from 1.

Or you can use a simple loop counter:

j=1; for i in *.png; do mv "$i" file"$j".png; let j=j+1; done

If you want to pad with 0 for 2-digit numbers:

j=1; for i in *.png; do mv "$i" file$(printf %02d $j).png; let j=j+1; done
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top