Bash – echoing basename under xargs

bashquotingshellxargs

I have the following command :

ls /some/path/*dat | xargs -n 1 -I @ sh -c "echo `basename @`"

with the directory /some/path/ containing :

/some/path/a
/some/path/b
/some/path/c
/some/path/d

I want to get the output :

a
b
c
d

But I'm still getting full paths. What am I doing wrong ?

edit: BTW, I know that there is an easier way, I can just run basname instead of

echo `basename @`

But I need to run a complex command, something like

octave --silent --eval "somefunction('`basename @`','@',...))"

edit2:

Here is the actual command :

 ls ~/phd/data/conll2012/dev.megam/*dat | xargs -P 16 -n 1 -I@ timeout -k 1s 15m sh -c "if [ ! -f '~/phd/xp/conll2012/dev.megam/@.$epsilon$mink$minn$alpha' ]; then octave --silent --eval \"xprp('~/phd/xp/conll2012/dev.megam/@.$epsilon$mink$minn$alpha.rp','@',$alpha,$mink,$minn,$epsilon);\" 2>> ~/xpgrid.log;fi"

Basically, I have a set of files in a directory and this command feeds on these files to output result in another directory, checking if the result is not already there.

So, I need both files with fullpath and files with only basename.

How to make it work on ? The easier example at the beginning can help make this clearer.

Best Answer

ls /some/path/*dat | xargs -n 1 -I @ sh -c 'echo `basename "@"`'

The basename is executed too early in your code.

If you are not sure that there are no spaces or tabs in the paths then you should use -d \\n (or find ... -print0 | xargs -0 ...) and mind the " around the @.