chxo internets RSS

A network of memes,
by Chris Snyder

See also
CHXO Internet
twitter.com/64

Archive

Sep
27th
Sat
permalink

Batch rename in unix; mv wildcard via xargs

Sometimes, you need to batch rename a bunch of files that all match the same wildcard. 

Let’s say you have a directory that includes a bunch of date-prefixed filenames, like 2008-09-24-www.txt, and you want to rename them like 2008-09-24-www-old.txt. Well, the following xargs kludge performs the equivalent of mv *.www.tx to *.www-old.txt: 

ls *.www.txt | xargs -I % basename % .www.txt > z.renames
cat z.renames | xargs -I % mv %.www.txt %.www-old.txt
rm z.renames

Uses xargs and basename, and a simple temporary file. This may not be the best way; but it is a way.