2
votes

i would like to merge multiple files into single file in bash. so i use the code,

cat file1 file2 file3 file4 >> output

But because my computer has low memory i am not able to create the merged file. instead do you know how to simultaneously remove file1 file2 file3 once the data has been added to output file?

2

2 Answers

5
votes
for i in file1 file2 file3 file4 ; do cat "$i" >> output && rm "$i" || break ; done

So for each file, it will append the contents to output and remove the source file once appended. If anything goes wrong, it stops.

0
votes

The best I can think of is:

mv file1 output
cat file2 >> output && rm file2 &&
cat file3 >> output && rm file3 

The rms here are at pretty much the earliest useful opportunity.

You can, of course, do this in a for loop.

mv file1 output
for f in file*; do
   cat $f >> output && rm $f
done