Topics:
DVR
nvrec
Mplayer
Links
Misc
Commands
Humor
New user
uploaded files
|
(linux_command_line)-> (Parent)->linux rename command+examples |
submited by Russell Tue 31 Jan 06 Edited Mon 07 Dec 09 |
(not all renames are created equal Alt version here) to rename a single file just use move (mv):
mv oldfilename newfilename
To rename a bunch of files in the same way use rename
rename from to filespec
replaces all from with to in all the files that fit filespec
say you have a list of files:
DSCF0001.JPG
DSCF0002.JPG
DSCF0003.JPG
DSCF0004.JPG
.....
to rename these files all at once use:
rename DSCF xmas_photos DSCF*
this will rename the files so you now have:
xmas_photos0001.JPG
xmas_photos0002.JPG
xmas_photos0003.JPG
xmas_photos0004.JPG
.....
To delete text from the filenames use:
rename something_to_delete "" filespec
This can also be used to cut the extension from the file name:
rename .JPG "" *.JPG
Or to rename one extention to another
rename .jpeg .jpg *.jpeg
But to do this, you must issue the command for each type of extension that you have (.JPG, .jpg, .jpeg etc )
To rename filenames with spaces to filenames without spaces:
rename " " _ *\ *
Will change spaces in filenames to underscore. (note there is a space after the blackslash in the last parameter of that command)
Repeat this command until you get :rename: renaming * * to *_* failed: No such file or directory because it only changes the first occorance of the space in the filename. Be Mindful of the warning below
WARNING, BE CAREFUL:!!!
This WILL overwite files without warning you. say you have files:
aa.txt
other_file.txt
And you use the command (I know this is silly but it makes the point):
rename other_file aa *.txt
this will overwrite without warning aa.txt with other_file.txt, leaving only one file named aa.txt. To avoid this gotcha, I usualy move all the files that I want to rename into a temp directory ( or copy them if I am felling really paranoid ) and do the rename there. The command does not appear to support an interactive option like mv -i ( prompt before overwrite )
Dec 6,2009
Sadly all renames are not created equal. I just installed ubuntu on my latop and discovered that the rename command is different. The version installed on ubuntu uses a perl replace method, which is much more powerful. what is bad about this is that the command's behavior is Different for the various versions.
First : Which version do you have installed?
type man rename. if the top of the page says
Perl Programmers Reference Guide
you have the perl version. If it says:
Linux Programmer's Manual
you have the standard (older) version. use the notes above for this command. if you have the perl version the paramters are different.
They look like this:
rename 's/\.bak$//' *.bak
the quotes and the s are required. the format is 's/FROM/TO/' so the from and to values are in the first paramter instead of the first two. It's not that complicated, and using perl regeular expressions it is quite powerful. What I do not like is that any script that uses the old format will need to be updated.
Replys:
|
|