THE BOOK cover
The Unwritten Book is Finally Written!
An in-depth analysis of: The sacrifice bunt, batter/pitcher matchups, the intentional base on balls, optimizing a batting lineup, hot and cold streaks, clutch performance, platooning strategies, and much more.
Read Excerpts & Customer Reviews

Buy The Book from Amazon


SABR101 required reading if you enter this site. Check out the Sabermetric Wiki. And interesting baseball books.
MOST RECENT ARTICLES
MAIL : You ask | We say

Advanced


THE BOOK--Playing The Percentages In Baseball

<< Back to main

Friday, January 02, 2009

Renaming multiple files in UNIX

By Tangotiger, 11:33 AM

Just thought I’d put it out there for those in the same position as I was:

for i in *old*; do mv $i `echo $i | sed 's/old/new/'`; done

This renames all file names that have “old” in it to “new”.  Be careful between the two different kinds of tick marks.  One is next to the number 1 on your keyboard, and the other is next to the enter key.


Web Admin
#1    Gordon      (see all posts) 2009/01/02 (Fri) @ 12:37

There’s also mmv, if you happen to be on a *NIX-alike that has it.


#2    joe arthur      (see all posts) 2009/01/02 (Fri) @ 18:51

Tom,

I’m not a master of many unix shells, but I think that syntax is shell-specific; for example it should work in kornshell, but may not work in c-shell.

Your method is fine, assuming you know enough about where you are operating and what filenames you are operating on ...

1) it assumes your working directory is the directory in which you want to make the changes; the pattern match in the “for” loop is not recursive so you won’t change anything in subdirectories - though possibly this behavior is OS-specific. No recursion may be good, bad or unimportant, depending on what you may have in subdirectories.

2) in your working directory this will rename not just files but also any subdirectory names which contain ‘old’.

3) the pattern you use in the sed replacement will match the first occurrence of ‘old’ in the name, which might not be expected: e.g. bold.old will be renamed bnew.old - probably you can come up with a more precise pattern to avoid that kind of accidental replacement - again if you’re only working with a smallish # of files and you know you won’t get those accidental replacements, this does not matter.

4)I believe the buffer holding the returned string from matching *old* can overflow with an error if too many files match the pattern, though the details of how big that string can safely be are probably OS-specific. I’ve had that sort of overflow error with patterns which matched in the low 1000s of files.

Using the find command can give you more flexibility with concerns 1, 2 and 4


#3    Tangotiger      (see all posts) 2009/01/02 (Fri) @ 19:47

Joe, you are right that it is shell-specific.

I appreciate you pointing out all the potential issues.

It would be helpful if you can offer alternative code as well.


#4    Vinay      (see all posts) 2009/01/03 (Sat) @ 05:38

Regarding the different type of ticks, I’ve gotten away from using backticks, as they are hard to read and can’t be nested.  In bash, you can use the following instead:

for i in *old*; do mv $i ${echo $i sed 's/old/new/'} done

Because the opening and closing braces are distinct (as opposed to backticks, which use the same symbol at the start and finish), I think it’s a little easier to read and to find mistakes.  And you can use nesting with the ${} form, but not with backticks.


Page 1 of 1 pages


Name (required)
E-Mail (optional)
Website (optional)

<< Back to main


Latest...

COMMENTS

Aug 31 15:28
Fans Scouting Report: Update

Sep 02 15:44
The two uncertainties of UZR

Sep 02 15:17
Mail: rWAR v fWAR

Sep 02 14:59
Roger Federer

Sep 02 14:59
It’s hard to beat the crowd (Vegas in this case) no matter how smart you think you are

Sep 02 14:57
Could Rob Dibble have been a comp for Strasburg?

Sep 02 14:15
WOWY Teachers

Sep 02 13:37
Who’s Waldo?

Sep 02 08:36
Team Elin

Sep 02 01:19
Can someone tell me why Trevor Hoffman is still allowed to pitch?