Home » Questions » Computers [ Ask a new question ]

find: -exec vs xargs (aka Why does "find | xargs basename" break?)

find: -exec vs xargs (aka Why does "find | xargs basename" break?)

I was trying to find all files of a certain type spread out in subdirectories, and for my purposes I only needed the filename. I tried stripping out the path component via basename, but it did't work with xargs:

Asked by: Guest | Views: 48
Total answers/comments: 2
Guest [Entry]

"Because basename wants just one parameter... not LOTS of. And xargs creates a lot of parameters.

To solve your real problem (only list the filenames):

find . -name '*.deb' -printf ""%f\n""

Which prints just the 'basename' (man find):

%f File's name with any leading directories
removed (only the last element)."
Guest [Entry]

basename only accepts a single argument. Using -exec works properly because each {} is replaced by the current filename being processed, and the command is run once per matched file, instead of trying to send all of the arguments to basename in one go.