How to append one file to another in Linux from the shell?

后端 未结 8 1491
我寻月下人不归
我寻月下人不归 2020-12-04 04:44

I have two files: file1 and file2. How do I append the contents of file2 to file1 so that contents of file1

相关标签:
8条回答
  • 2020-12-04 05:37

    You can also do this without cat, though honestly cat is more readable:

    >> file1 < file2

    The >> appends STDIN to file1 and the < dumps file2 to STDIN.

    0 讨论(0)
  • 2020-12-04 05:42

    Just for reference, using ddrescue provides an interruptible way of achieving the task if, for example, you have large files and the need to pause and then carry on at some later point:

    ddrescue -o $(wc --bytes file1 | awk '{ print $1 }') file2 file1 logfile
    

    The logfile is the important bit. You can interrupt the process with Ctrl-C and resume it by specifying the exact same command again and ddrescue will read logfile and resume from where it left off. The -o A flag tells ddrescue to start from byte A in the output file (file1). So wc --bytes file1 | awk '{ print $1 }' just extracts the size of file1 in bytes (you can just paste in the output from ls if you like).

    As pointed out by ngks in the comments, the downside is that ddrescue will probably not be installed by default, so you will have to install it manually. The other complication is that there are two versions of ddrescue which might be in your repositories: see this askubuntu question for more info. The version you want is the GNU ddrescue, and on Debian-based systems is the package named gddrescue:

    sudo apt install gddrescue
    

    For other distros check your package management system for the GNU version of ddrescue.

    0 讨论(0)
提交回复
热议问题