Friday, January 11, 2008

Rename files

Thanks to Stephan [my room-mate :-) ] I discovered the command "rename"!

N.B. The following works on Debian:

Let's say I want to rename a file callled "test11.txt" to "test12.txt"; the command which does the job is:

rename 's/11/12/' test11.txt

where 's/11/12/' is a perl-style Regular Expression (RE), where 's' stands for "string".

If you are not sure about your RE you can test it with the "-n" option, as:

rename -n 's/11/12/' test11.txt

it just shows to you the result of the operation without actually doing it.

If you have many files in a folder and you want to rename all of them you can use the "*" wildcard:

rename 's/11/12/' *

the command above renames all files containing a string "11" in the name, changing it to "12".

N.B. And the following works on SLC4 (and I guess on Fedora Core then)

the command:

rename .htm .html *.html

changes all the files ending with .htm into .html

P.S.
And if you want, for example, to rename or to add a suffix to a list of files in a directory you can use these commands:

for FILE in * ; do mv $FILE $FILE.txt ; done

where we add the suffix .txt to all files in the directory.
(Note for physicists: Useful to rename AOD or DPD data files to .root files ;-) )

0 commenti: