More on Rename

December 19, 2009

In the previous post I mentioned the rename command, but said that I didn’t know too much about it.   Well after some reading, and messing around I have learned a little bit more, so guess I should share.

Here is the general set up of the command;
rename [options] 's/expr1/expr2/' expr3
expr1, expr2, and expr3 are Perl regular expressions. Basically it will replace expr1 with expr2 in every file that matches expr3.
The options are;
-n (do nothing, just print out what the result would be if the command were to run)
-f (force overwrite of existing files) and
-v (verbose, print out progress while command runs).

For the sake of these example suppose that your folder contains the following files:
Pic_00019876_new.JPG
Pic_00029877_new.JPG
Pic_00039878_new.JPG
Pic_00049879_new.JPG
Pic_00059870_new.JPG

Example 1

Suppose that we want to change the file extensions to .jpg instead. Then just type rename 's/\.JPG/\.jpg/' *.JPG So instead of having Pic_00019876_new.JPG you have Pic_00019876_new.jpg

Example 2

Now suppose that you want to remove the leading 0s from the file number. For this you would type something like
rename 's/Pic_(\d{3})(\d{5})_new\.JPG/Pic_$2_new\.JPG/' *.JPG
The $2 says to use the 2nd matched group, and the (\d{X}) matches X digits.
Here Pic_00019876_new.JPG becomes Pic_19876_new.JPG

Example 3

For the hell of it say that you want to remove the ‘Pic’ prefix, make the first 4 digits the last 4, make .JPG into .jpg and put ‘new’ at the front. For this you could run
rename 's/Pic_(\d{4})(\d{4})_(new)\.JPG/$3_$2$1\.jpg/' *.JPG
One word of caution, make sure the escape the ‘.’ or you might get unexpected results. Now Pic_00019876_new.JPG becomes new_98760001.jpg

Hope that those examples were useful. the ‘s/…’ says that it will be matching a string. There are other things you can use instead of ‘s’, but I don’t know what they are and am not a Perl guru so I wouldn’t be able to tell you. I do however, know that ‘y/…’ is used in changing the case of all characters.

For further reading I would suggest this site.

Advertisements

Rename

December 15, 2009

I’m sure that many of you have experienced this before, and if not I have experienced it enough times to warrant a post.  I download an album but all the track names have extra letter either appended to the beginning or the end.  And as the name of this post suggest I would like to rename them all and remove these characters.

Sure I could use the mv command or even open up the GUI file browser and go from there, but I want to get this done with as little work as possible.  The funny thing is that I probably spent more time trying to find this solution than it would have taken if I were to actually do this the long way.

Well here is the command you need: rename.  I suggest that you read the man page for it since at this point I’m not all to sure how it works.  But I can provide you with an example of what you need to type to get the job done (in this case at least).  Suppose that the following sequence is after what you want AwX3 and they are all .mp3 files

rename -n 's/AwX3//' *.mp3

Doing this will tell you which files will be renamed and what they would be renamed to.  After you have made sure that everything is alright, run it again but this time with out the -n.

Hope this helps someone, and if not I know it will help me in the future when I have this problem again.