You can do cat $(ls -1)
if you override IFS. IFS is the shell variable that tells BASH which characters to use as a deliminator. The default value for IFS is space, tab, newline. If you change IFS to just newline then you can do cat $(ls -1)
.
There is nothing wrong with the other answers but this might be a more direct answer to how to deal with spaces in general and this answer introduces the IFS variable which most are not familiar with.
#!/bin/bash
dir=$(mktemp -d)
for x in $(seq 10); do
echo $x > "$dir/$(date) $x.txt"
done
pushd $dir
ls -1 $dir
# Set IFS to newline only inorder to deal with the spaces in the file names
#
oldIFS=$IFS
IFS=$'\n'
cat $(ls -1 $dir)
IFS=$oldIFS
Output
[sri@localhost test]$ ./test
/tmp/tmp.IuXCBzbTLj ~/test
Mon Nov 17 06:38:52 EST 2014 10.txt
Mon Nov 17 06:38:52 EST 2014 1.txt
Mon Nov 17 06:38:52 EST 2014 2.txt
Mon Nov 17 06:38:52 EST 2014 3.txt
Mon Nov 17 06:38:52 EST 2014 4.txt
Mon Nov 17 06:38:52 EST 2014 5.txt
Mon Nov 17 06:38:52 EST 2014 6.txt
Mon Nov 17 06:38:52 EST 2014 7.txt
Mon Nov 17 06:38:52 EST 2014 8.txt
Mon Nov 17 06:38:52 EST 2014 9.txt
10
1
2
3
4
5
6
7
8
9
Here is a link to some documentation on IFS. http://bash.cyberciti.biz/guide/$IFS. I think that I learned about it a long time ago by reading O'Rielly's BASH book.
ls
. Usefind
instead.find
can use a NUL byte as the delimiter, and bothsort
andxargs
can accept that delimiter, so that approach can even handle filenames that contain\n
.*
,?
,[
), tab or newline, started with-
or some of the*.sql
files were of type directory (in which casels
lists their content).