views:

672

answers:

7

What is the quickest and most pragmatic way to combine all *.txt file in a directory into one large text file?

Currently I'm using windows with cygwin so I have access to BASH.

Windows shell command would be nice too but I doubt there is one.

+19  A: 

This appends the output to all.txt

cat *.txt >> all.txt

This overwrites all.txt

cat *.txt > all.txt
Robert Greiner
Please format your code samples as source code (4 leading spaces).
Andrey Vlasovskikh
my mistake. Thanks.
Robert Greiner
you may run into a problem where it cats all.txt into all.txt... I have this problem with grep sometimes, not sure if cat has the same behavior.
rmeador
@rmeador yes, that is true, if all.txt already exists you will have this problem. This problem is solved by providing the output file with a different extension, or moving all.txt to a different folder.
Robert Greiner
+3  A: 

You can use Windows shell copy to concatenate files.

C:\> copy *.txt outputfile

From the help:

To append files, specify a single file for destination, but multiple files for source (using wildcards or file1+file2+file3 format).

Carl Norum
+6  A: 

The Windows shell command type can do this:

type *.txt >outputfile

Type type command also writes file names to stderr, which are not captured by the > redirect operator (but will show up on the console).

Greg Hewgill
+5  A: 

Just remember, for all the solutions given so far, the shell decides the order in which the files are concatenated. For Bash, IIRC, that's alphabetical order. If the order is important, you should either name the files appropriately (01file.txt, 02file.txt, etc...) or specify each file in the order you want it concatenated.

$ cat file1 file2 file3 file4 file5 file6 > out.txt
Chinmay Kanchi
A: 
DIR="/your/dir/here/";
PREFIX="txt sh";
FILENAME="/allmyfiles.txt";
cd $DIR;
touch $FILENAME;
for a in $PREFIX; do
for b in $(ls $DIR$PREFIX); do
echo "    
# Start $b
$(cat $b)
# End $b" >> $FILENAME;
done
done;

;)

EDIT: Store/Append directly to the file

CuSS
Yuck, concatenating them into a shell variable? Just wait until you try to stitch together thirty 2GB files and see how well that works out for you... :-p
Steven Schlansker
stored directly on the file now :)
CuSS
A: 

the most pragmatic way with the shell is the cat command. other ways include,

awk '1' *.txt > all.txt
perl -ne 'print;' *.txt > all.txt
ghostdog74
+1  A: 

all of that is nasty....

ls | grep *.txt | while read file; do cat $file >> ./output.txt; done;

easy stuff.

kSiR
Eeek! Don't do that. Do `find . -iname "*.txt" -maxdepth 1 -exec cat {} >> out.txt \;`
Chinmay Kanchi