2

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

1

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
  • 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
0

Try adding double quote characters around the % characters:

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

  • Cheers Scott, nice and clean works perfekt – manolo Aug 30 '12 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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