Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 9. Finding Files: find, locate, ... > Speeding Up Operations on Found File... - Pg. 187

So, to rewrite the solution from Recipe 9.1, "Finding All Your MP3 Files" to handle odd characters: $ 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: $ locate P1100087.JPG PC220010.JPG PA310075.JPG PA310076.JPG | xargs -i cp '{}' . Discussion There are two problems with this approach. One is that not all versions of xargs sup- port 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: $ find some_directory -type f -print0 | xargs -0 chmod 0644 See Also · man find · man xargs · Recipe 9.1, "Finding All Your MP3 Files" · Recipe 15.13, "Working Around "argument list too long" Errors" 9.3 Speeding Up Operations on Found Files Problem You used a find command like the one in Recipe 9.1, "Finding All Your MP3 Files" and the resulting operations take a long time because you found a lot of files, so you want to speed it up. Speeding Up Operations on Found Files | 187