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.
cat *
can cause an arguments list too long error. – cuonglm May 28 '16 at 02:59ls -t | head -1
to another command? based on your answer, I worked outprintf '%s\0' "$(ls -t | head -1)" | xargs -0 gvim
(needed the double quotes for filename having spaces).. tested a few cases, but not sure if this can open latest modified file for all sorts of filenames.. – Sundeep May 28 '16 at 05:58gvim "$(ls -t | head -1)"
. – John1024 May 28 '16 at 06:23printf "%s\0" *
will also output directories, symlinks, device nodes, named pipes and sockets.find . -maxdepth 1 -type f -print0
is better. Usefind -L ...
if you want to follow symlinks (i.e. treat symlinks to files as if they were files) – cas May 28 '16 at 07:08find
examples to the answer. – John1024 May 28 '16 at 07:39