Concatenate Files to Another File


cat file1 file2 > file3

In the previous example, you concatenated two files and printed them on the screen to stdout. This might not be what you want, though. If you're concatenating two files, it might be nice to save the newly joined creation as another file you can use. To do this, redirect your output from stdout to a file, as you learned in Chapter 4, "Building Blocks."

$ ls housman_-_rue.txt quarles_-_the_world.txt $ cat housman_-_rue.txt quarles_-_the_world.txt > poems.txt$ ls housman_-_rue.txt poems.txt quarles_-_the_world.txt 


Now you can do whatever you want with poems.txt. If you want to add more poems to it, that's easy enough to do:

$ cat housman_-_one-and-twenty.txt >> poems.txt 


Notice that the >> was used to append the new poem to poems.txt this time. The following command would not have worked:

$ cat housman_-_one-and-twenty.txt poems.txt > poems.txt 


If you tried concatenating a file to itself, you'd see this error message, helpfully explaining that it is impossible to fulfill your request:

cat: poems.txt: input file is output file 




Linux Phrasebook
Linux Phrasebook
ISBN: 0672328380
EAN: 2147483647
Year: 2007
Pages: 288

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net