Tell `ls` to print only the base filename

While xargs -0 is intended to be used for input delimited by \0 (like find -print0), ls has no such option to delimit its output in this way.

However,

ls -1 /path/glob | tr '\n' '\0' | xargs -0 -n 1 basename

would do the trick to convert newlines to nulls along the way. This then allows xargs to work with names that have spaces.

EDIT: added -n 1 to xargs


I use this:

ls | tr '\n' '\n'

It gives a list like:

file1.mp3
file2.mp3
file3.mp3
...

ls -1 <path> | sed 's#.*/##'

Both GNU basename and FreeBSD basename accept an -a argument allowing you to pass multiple paths to the command. This works great with shell globbing.

basename -a /path/glob*