I need to rename the following:
file_001_loremipsum.png
file_002_dolor.png
file_003_sit.png
file_004_amet.png
file_105_randomness.png
into
upl_loremipsum.png
upl_dolor.png
upl_sit.png
upl_amet.png
upl_randomness.png
How do I make it happen with just one simple line of terminal command?
The solution to the above example, using rename:
Usage:
From
man rename
:rename MAY take regex as the arguments.
What we are looking at is the content between the single quotes
'
. You can place regex separated by/
.Formula:
s/(1)/(2)/
where(1)
= search pattern, and(2)
= replace pattern.So, familiarize youself with regex, and enjoy pattern based batch file renaming!
This can be done with little magic of bash parameter expansion!
file_[0-9]*_*;
- First pattern is used to go trough all files that begin with 'file_anynumber_'${f#file_[0-9]*_}
- The second patternfile_[0-9]*_
is used in parameter expansion which tells bash to remove 'file_anynumber_' from the begging of the string.For more information on
Parameter expansion
:if files are in severals directories, use rename after a find like :
the -n after rename is to test, remove it to proceed !-)
like this, you associate find and rename power.
Personally, I used it to rename sources header .h to .hpp