Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have to concatenate a number of files in a directory structure which contains spaces in the folder names looking like this: ./CH 0000100014/A10/11XT/11xt#001.csv

find . -name "*.csv" -type f -print0 | xargs -0  cat > allmycsv.txt

does the job, however now I need to include the information contained in the path, i.e. CH 0000100014/A10/11XT as a header of each inputfile to cat.

find . -name "*.csv" -type f -print0 | xargs -0 -I % sh -c 'echo %; cat %' >allmycsv.txt

would do the job, if I had no spaces in the path, but in my case, cat does not get along with the space in the path name. Is there a way out?

Cheers, E

P.S. I am working on bash on OSX

share|improve this question
up vote 1 down vote accepted

You can read the file list in a while loop (see BashFAQ #020):

find . -name "*.csv" -type f -print0 | while IFS= read -d $'\0' -r file ; do
    echo "$file"
    cat "$file"
done >allmycsv.txt
share|improve this answer
You can also just use -d '' instead of -d $'\0', which works either because bash special cases the empty string, or because the "first" character of the null string is the null-byte terminator (I'm not sure which). – chepner Aug 30 '12 at 1:36
Thank you! Works perfekt, and btw great wiki you pointed me to. – manolo Aug 30 '12 at 6:51

Try adding double quote characters around the % characters:

sh -c 'echo "%"; cat "%" '

share|improve this answer
Cheers Scott, nice and clean works perfekt – manolo Aug 30 '12 at 6:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.