How do I rename files based on the contents of a text file?
April 21, 2006 10:45 PM   Subscribe

I have a folder of 300+ files with three digit file names. I also have a tab-delimited text file which has the current file name and the corresponding desired file name. How can I use the text file to properly rename all my files? (This is on Mac OS X)
posted by nathan_teske to Computers & Internet (14 answers total)
 
perl -nle '@files = split(/\t/, $_); system("mv $files[0] $files[1]");' files.txt

adjust as necessary. :)
posted by kcm at 10:49 PM on April 21, 2006


Response by poster: kcm - thanks but it didn't work out. Copy and pasted that line of perl into terminal, pointed it at my text file and... well.. nothing. OK bash reminding me about proper usage of 'mv', but nothing else.

Help? Please?
posted by nathan_teske at 11:02 PM on April 21, 2006


Best answer: well, I can't think of much since it worked for my simple example after I typed it in, but if you have spaces you'd have to quote the filenames..

perl -nle '@files = split(/\t/, $_); system("mv \"$files[0]\" \"$files[1]\"");' files.txt

Maybe someone else has another method that would be easier for you to debug/modify hands-on.
posted by kcm at 11:23 PM on April 21, 2006


awk '{ system("mv " $1 " " $2) }' filelist
posted by fleacircus at 11:24 PM on April 21, 2006


Response by poster: kcm - that did it. Thanks!
posted by nathan_teske at 11:26 PM on April 21, 2006


Actually you probably want to add a -v flag in there, so:
awk '{ system("mv -v " $1 " " $2) }' filelist
posted by fleacircus at 11:27 PM on April 21, 2006


Bah, awk is cleaner :)
posted by fleacircus at 11:27 PM on April 21, 2006


awk is going to split the lines on spaces, at least there. that's a plus, sometimes, when there's weird variable whitespacing wrt. other tools like cut.
posted by kcm at 11:33 PM on April 21, 2006


system("mv") ? Puke. How about just rename($a, $b) and forget about a whole mess of quoting problems. (Quick: does yours puke when you have filenames with spaces in them? Single quotes? Double quotes? Filenames that begin with a dash?)
posted by Rhomboid at 11:53 PM on April 21, 2006


And don't even get me started on why this is a horrible idea if any of the filenames could come from a network source -- you are just asking to be owned by some kid that enters "; rm / -rf" as a filename. Avoid system() like the plague.
posted by Rhomboid at 11:55 PM on April 21, 2006


I used system and mv because it's easier to debug for a non-Perl user. Go ahead, write him a 200 line program covering all corner cases dressed up with a pretty GUI, since you're solving problems he didn't even have.

I'll write a standard disclaimer header into my code sample on AxMe from now on, just for you.
posted by kcm at 12:01 AM on April 22, 2006


For future informaiton, it's best to avoid system() and its ilk whenever possible.
posted by grouse at 3:24 AM on April 22, 2006


I used system and mv because it's easier to debug for a non-Perl user.
In what warped world is

system("mv \"$files[0]\" \"$files[1]\"");

easier to understand or debug than

rename($files[0], $files[1]);

Who said ANYTHING about needing to write 200 lines, or anything else? All of the concerns that I mentioned are covered by simply using rename() instead of the god-awful system("mv").
you're solving problems he didn't even have.
Posting code samples that are inefficient (fork and exec /bin/sh and then fork and exec /bin/rm when a single syscall will suffice) and highly insecure is NOT somehow okay just because in this particular case it might not be a problem. Somebody might get the bright idea to use this idiom elsewhere. c.f. "Matt's script archive" and the numerous problems those awful scripts have caused over the last decade or so because of the same attitude.
posted by Rhomboid at 10:49 AM on April 22, 2006


if someone makes the bizarre logical leap of using my one-off no-erase off-the-top-of-my-head code from this thread in a popular reusable codebase, they deserve it. in this case, rename() is better. I had absolutely no idea how well rename() works in OS X and/or his build of Perl, his filesystem, etc., so I did it the portable way. I always respect improved solutions presented humbly and actually suggested someone do just that.

I'm glad you found something to nitpick on, and you have a valid concern.. but calm the hell down already. in the future, you are more than welcome to comment on my code constructively, but please set your ego aside in AskMe.
posted by kcm at 12:58 PM on April 22, 2006


« Older Help me find surnames that come from occupations.   |   CVS checkout by date, how? Newer »
This thread is closed to new comments.