First, do not parse ls
. There are many reliable ways of getting file names but ls
is not one of them.
The following uses the shell's globbing to generate file names and passes a nul-separated list of them to xargs
which runs them through cat
:
printf '%s\0' * | xargs -0 cat
I understand that the point of this was to demonstrate files->xargs. Otherwise, of course, unless there are too files to fit on a command line, the above can be replaced with:
cat *
Being more selective
Both ls
and printf '%s\0' *
will display all of a directory's contents, including its subdirectories. If you want to be more selective, say, including regular files but not subdirectories, then, as cas suggests, find
is better tool:
find . -maxdepth 1 -type f -print0 | xargs -0 cat
Or:
find . -maxdepth 1 -type f -exec cat {} +
find
has many useful options. See man find
for details.