I have a lot of files I'm trying to rename, I tried to make a regular expression to match them, but even that I got stuck on the files are named like:
File Name 01
File Name 100
File Name 02
File Name 03
etc, I would like to add a "0" (zero), behind any of file that are less than 100, like this:
File Name 001
File Name 100
File Name 002
File Name 003
The closest I got to so much as matching them was using this find -type d | sort -r | grep ' [1-9][0-9]$' however I could not figure out how to replace them. Thanks in advance for any help you can offer me. Im on CentOS if that is of any help, all this is being done via SSH.
perl -e 'foreach $f (glob("File\\ Name*")) { $nf = $f; $nf =~ s/(\d+)$/sprintf("%03d",$1)/e; print `mv \"$f\" \"$nf\"`;}'
A bit overkill maybe, but it does what is asked.
find . -type d -print0 | xargs -0 rename 's/(\d+)/sprintf "%03d", $1/e'
or something like that, provided
-print0
and -0
)Is this a one-time thing? If so, I'm going to suggest something that might seem to be a cop out by many programmers here:
Pipe the output of your command (find -type d | sort -r | grep ' [1-9][0-9]$'
) to a file and use an editor along with some global search/replace magic to create a script that does the renames.
Then throw away the script.
There's little fuss and little chance that you'll end up shooting yourself in the foot by having some attempt at a clever (but inadequately debugged) one-liner go off into the weeds on your files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With