How to rename batches of filenames?

B

bashcommando

Guest
I have over 7000 files I got from a frame extraction in vlc. I want to get rid of all the zeros before the filenames and not get rid of the zeros after the... thing. Example:
000000100.png -> 1.png <- No
000000100.png -> 100.png <- Yes
How do I do this from the command line?
 


Perl to the rescue. There is a bit assumed here, but the basics are below. Note the single quotes are intentional and required on the split.

while ($filename = glob("*.png"))
{
($number, $extension) = split '\.', $filename;
$number = s/^0*(\d+)$/$1/;
rename($filename, "$number.$extension");
}
 
...and forgive the formatting - I did not see how to embed multiple spaces to indent the code in the { }.

You can also add 1 and subtract 1, but that is messy if you get a non-numeric character in the string.
 
...and forgive the formatting - I did not see how to embed multiple spaces to indent the code in the { }.

You can also add 1 and subtract 1, but that is messy if you get a non-numeric character in the string.
And forgive me for not knowing perl.
 
Perl to the rescue. There is a bit assumed here, but the basics are below. Note the single quotes are intentional and required on the split.

while ($filename = glob("*.png"))
{
($number, $extension) = split '\.', $filename;
$number = s/^0*(\d+)$/$1/;
rename($filename, "$number.$extension");
}
I ran your code and all the files got deleted. T_T
Edit: *dopeslap*
 
I ran your code and all the files got deleted. T_T
Edit: *dopeslap*
First of all, before you run ANY new script on a live system, you need to THOROUGHLY test it on a set of sample files first!

Although it is not a command line app, you may also want to look at another rename utility called, "gprename" It should be included in your Distro, but if not, it can be downloaded from: http://gprename.sourceforge.net/

It is a GPL3, Gtk2-perl app. And just for you @bashcommando, it has an undo! ;^)

I am a firm believer in bash to do a lot, if not most of the administrative work I need to do in Linux, but there are exceptions. This is one. The more complex the changes made, the easier to do when you can visually see the changes before you commit, as opposed to running extensive tests on the command line. You may need to run the utility multiple times as you cannot combine the four different types of changes, Change Case, Insert/Delete, Replace/Remove, Numerical.
 

Members online


Top