Professional Python® programming and open source software consulting since 2004

Blog


UNIX Tips For The Elderly

huh

Do something to a bunch of files

I often want to do something to a bunch of files on the local filesystem. For example:

$ find Music/ | xargs -I % echo 'Do something to ' %

This works because, according to the xargs man page:

-I replstr
Execute utility for each input line, replacing one or more occurrences of replstr in up to replacements (or 5 if no -R flag is specified) arguments to utility with the entire line of input. The resulting arguments, after replacement is done, will not be allowed to grow beyond replsize (or 255 if no -S flag is specified) bytes; this is implemented by concatenating as much of the argument containing replstr as possible, to the constructed arguments to utility, up to replsize bytes. The size limit does not apply to arguments to utility which do not contain replstr, and furthermore, no replacement will be done on utility itself. Implies -x.

The problem is that sometimes the filenames have spaces in them which will cause:

xargs: unterminated quote

The best fix I've managed to come up with, which I couldn't immediately recall hence this blog post for future senior moments, is to replace the beginning and end of the line with quotes to make the shell happy.

$ find Music/ | sed -e 's/^/"/' -e 's/$/"/'

"Music//iTunes/iTunes Music/Yael Naïm/Yael Naïm/03 New Soul.m4a"

So now I can do things like run the file command on all my files:

$ find Music/ | sed 's/^/"/' | sed 's/$/"/' | xargs -I % file %

Music//iTunes/iTunes Music/Yael Naïm/Yael Naïm/03 New Soul.m4a: ISO Media, MPEG v4 system, iTunes AAC-LC

Awesome!

Blog Now Résumé
ACLARK.NET, LLC