12

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:

$ find . -name '*.deb' -print | xargs basename 
basename: extra operand `./pool/main/a/aalib/libaa1_1.4p5-37+b1_i386.deb'
Try `basename --help' for more information.

I get the same thing (exactly the same error) with either of these variations:

$ find . -name '*.deb' -print0 | xargs -0 basename 
$ find . -name '*.deb' -print | xargs basename {}

This, on the other hand, works as expected:

$ find . -name '*.deb' -exec basename {} \;
foo
bar
baz

This happens on up-to-date Cygwin and Debian 5.0.3. My diagnosis is that xargs is for some reason passing two input lines to basename, but why? What's going on here?

quack quixote
  • 43,504

5 Answers5

27

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).
akira
  • 63,447
22

Try this:

find . -name '*.deb' | xargs -n1 basename
perlguy9
  • 389
4

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.

4

xargs can be forced to just pass one argument as well...

find . -name '*.deb' -print | xargs -n1 basename

This works, however the accepted answer is using find in a more appropriate way. I found this question searching for xargs basename problems as I'm using another command to get a list of file locations. The -n1 flag for xargs was the ultimate answer for me.

Flet
  • 141
0

The reason is that by default xargs does not use just one input item to build one command invocation.

In fact, xargs uses as many input items as possible every time it executes the command, where the max number of items used to build one command invocation is a system-defined value and can be overwritten by the -n or -L options. For example, if you run echo a b | xargs ls, instead of executing ls twice with a and b respectively, xargs will build and run a single command ls a b.

So for a command that accepts only one parameter (like basename), you should use the -n or -L option if you want to stick to xargs.

ebk
  • 101