First, understand that to Unix folks, odd means “anything not a lowercase letter, or maybe a number.”
So uppercase, spaces, punctuation, and character accents are all odd. But you’ll find all of those and more in the names of many songs and bands.
Depending on the oddness of the characters, your system, tools, and goal, it might be enough to simply quote the replacement string (i.e., put single quotes around the {}, as in ‘{}’) .
You did test your command first, right?
If that’s no good, try using the -print0 argument to find and the -0 argument to xargs. -print0 tells find to use the null character (\0) instead of whitespace as the output delimiter between pathnames found. -0 then tells xargs the input delimiter.
These will always work, but they are not supported on every system.
The xargs command takes whitespace delimited (except when using -0) pathnames from standard input and executes a specified command on as many of them as possible (up to a bit less than the system’s ARG_MAX value.
Since there is a lot of overhead associated with calling other commands, using xargs can drastically speed up operations because you are calling the other command as few times as possible, rather than each time a pathname is found.
So, to rewrite the solution from Recipe 9.1, “Finding All Your MP3 Files” to handle odd characters:
1 |
$ find . -name '*.mp3' -print0 | xargs -i -0 mv '{}' ~/songs |
Here is a similar example demonstrating how to use xargs to work around spaces in a path or filename when locating and then coping files:
1 |
$ locate P1100087.JPG PC220010.JPG PA310075.JPG PA310076.JPG | xargs -i cp '{}' . |
There are two problems with this approach.
One is that not all versions of xargs support the -i option, and the other is that the -i option eliminates argument grouping, thus negating the speed increase we were hoping for.
The problem is that the mv command needs the destination directory as the final argument, but traditional xargs will simply take its input and tack it onto the end of the given command until it runs
out of space or input.
The results of that behavior applied to an mv command would be very, very ugly.
So some versions of xargs provide a -i switch that defaults to using {} (like find), but using -i requires that the command be run one at a time.
So the only benefit over using find’s -exec is the odd character handling.
However, the xargs utility is most effective when used in conjunction with find and a command like chmod that just wants a list of arguments to process.
You can really see a vast speed improvement when handling large numbers of pathnames.
For example:
1 |
$ find some_directory -type f -print0 | xargs -0 chmod 0644 |