Rename multiple files
November 5th, 2006 toydi Posted in Common, Regular Expression, for, mv | Hits: 100922 |
Often over time, we will want to reorganize a group of files by renaming them.
To rename *.txt to *.bak
(e.g. to rename ham.txt to ham.bak)
for f in *.txt; do mv "$f" "${f%.txt}.bak"; done
To remove ‘new-’ from new-*
(e.g. to rename new-ham.txt to ham.txt)
for f in new-*; do mv "$f" "${f#new-}"; done
${variable%pattern} vs ${variable#pattern}
The funny-looking symbol, ${f%.txt} is a useful match-and-remove string operator:
If the pattern ‘.txt’ matches the end of variable $f, it will remove the matching part (that’s ‘.txt‘) and return the rest. Try this:
f=new-ham.txt # define $f as 'new-ham.txt'
echo ${f%.txt} # display 'new-ham'
What about ${f#new-}? It’s almost the same, but it matches the pattern at the beginning of the variable.
echo ${f#new-} # display 'ham.txt'
Live Chat!







November 6th, 2006 at 12:02 am
Great tips, this is an example related to regular expression, why don’t add in to RE category?
November 6th, 2006 at 12:07 am
As you wish. ;-)
July 11th, 2007 at 12:46 pm
This is the 2-cent tip that worths $1,000,00 dollars. Thank you very much. It’s what I was looking for days ago.
July 27th, 2007 at 1:03 am
rename ’s/\.txt/.bak/’ *.txt
July 27th, 2007 at 1:36 am
Cool, that is convenient. I have trace it . It is a perl script ‘prename’ in my box.
July 30th, 2007 at 3:24 am
Hi
You can accomplish the same trick and much more using Vladimir Lanin’s mmv utility. It is available for most distros.
July 30th, 2007 at 4:13 am
At first I was like “NO WAY” that can be done, “it looks exactly like bash/sh syntx” but then I realized that it was on linux.
MS DOS have some syntax in common with UNIX shell syntaxes, but not that much.
BTW I saw that you could use a hosts.deny in MS Windows (just put it in C:\hosts.deny apparently). I dont think it has anything to do with the linux/BSD tcp wrappers though ;D
August 9th, 2007 at 1:13 pm
great tip! i can now speed up my `split`s w00t
October 21st, 2007 at 9:04 am
I tried rename ’s/\.txt/.bak/’ *.txt but it does nothing what so ever on my system (Suse 10.2). Looks simple enough but nothing ever is with Linux, lol
October 21st, 2007 at 10:26 pm
Ubuntu 7.04 works
November 7th, 2007 at 10:20 am
thanks for the tip
November 9th, 2007 at 11:25 pm
/usr/bin/rename appears to come-with Perl (v5.8.8 on my system). It’s a Perl script.
April 15th, 2008 at 6:48 pm
this is very useful. first time i thik it can’t be possible. but the question was logical.so i tryed to find it.
June 6th, 2008 at 2:40 pm
[...] reveal about you?DavidGorcey.com- A funny blog by David Gorcey to keep you marginally entertainedRename multiple files ? Linux by Examples Tags baby shower favor wedding favor party favor candy favor bar wrappers wrapper This product [...]
February 3rd, 2010 at 10:39 am
should you need to rename or change the filename on multiple files where the char or chars that needs to be replaced is in the middle e.g. file-is-this.txt say you want to replace “is” with “or” you can do this:
for i in *.txt ; do mv “${i}” “${i/is/or}” ;done;
i hope that helps someone
February 19th, 2010 at 6:25 am
Thanks this helped allot. I have to rename all the chapters in the bible now it is not a problem. Thanks again