2

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 Answers2

5
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.

Jonas Berlin
  • 3,344
  • 1
  • 27
  • 33
0

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
slim
  • 40,215
  • 13
  • 94
  • 127