just brew it! wrote:
Heh... the xargs solution isn't really that
much easier to understand (and is actually longer
) than my solution. I think I'll stick with my find/sed/bash pipeline method for stuff like this!
Well, I primarily prefer the "purity" of the find ... -print0 | xargs -0 approach (or find ... -exec as notfred suggested) because this is what it was designed for: xargs takes the filenames and throws them into exec with the program directly before the shell gets a chance to molest special characters. With your | bash solution, you are building up lines of text to be executed as shell commands, so they are subject to globbing and escaping and what have you. Some filenames can cause this to explode if the text in the filename interacts poorly with the shell or any of the other text you put in your commands. Consider what happens in your solution when you have a file named:
james brown "the boss".mp3
The quotes in the filename close the quotes in your textual command and you get:
mv "./mp3s/james brown "the boss".mp3" destination-folder-name
which now has spaces and looks like two separate files, failing to move. It's not really a big deal in this case, but sometimes such interactions can cause the command to do something unexpectedly nasty. Or alternately:
Although I Dropped $100000 (I Found a Million Dollars in Your Smile).mp3
or many other uses of $ will cause the shell to manhandle it as an environment variable (since you used double quotes in your constructed commands rather than single quotes).
And notfred, I'd say the primary reason to prefer xargs to -exec on find is that -exec on find executes per file, whereas xargs gives you the opportunity to collect a bunch of files and execute the command once or a smaller number of times (or in increments of N). I agree if you're doing -n1 with xargs you could probably just use -exec.