I want to combine multiple text files in a directory to a new file while leaving new lines between files.


Depending on the situation, it could be as easy as:

some globbing to grab your files (filea, fileb, filec, etc.)

FILES=`ls file*`

then concatenate them

for i in $FILES; do cat "${i}" >> newfile; echo "" >> newfile; done
| improve this answer | |

One way to do it is with GNU find:

td=$(mktemp -d);printf '\n\n' > "$td/2"
find . -maxdepth 1 -type f -exec cat {} "$td/2" \; | head -n -2 > "$td/log"

find gets the regular files in the current directory and passes them to cat which also has the empty lines file concatenated.Finally the output is given to head which deletes the trailing empty lines and then stored in the log file.

perl -lpe '$\ = eof && !eof() ? "\n\n" : $/' *.files
| improve this answer | |

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