DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Commands revisited: pipes and redirection

Appending one file to another

You can use cat with redirection to append a file to another file. You do this by using the append redirection symbol, ``>>''. To append one file to the end of another, type cat, the file you want to append, then >>, then the file you want to append to, and press <Enter>.

For example, to append a file called report2 to the end of report1, type:

cat report2 >> report1

You can use the append symbol ``>>'' with any command that writes output. For example, you could append a directory listing to a file called log with:

ls >> log

Try working through the following cat tutorial:

  1. If you are not already in your home directory, go there by typing cd and pressing <Enter>.

  2. Use cat to create three files: report1, report2, and report3.

    Type cat > report1 and press <Enter>. Type report 1 and press <Enter>. Then, type Keeping a cat is a serious responsibility and press <Enter>. Now, press <Ctrl>D.

    Create report2 by typing cat > report2 and pressing <Enter>. Type report 2 and press <Enter>. Then, type Cats need a balanced diet and press <Enter>. Now, press <Ctrl>D.

    Create report3 by typing cat > report3 and pressing <Enter>. Type report 3 and press <Enter>. Then, type Responsible cat owners will neuter or spay their pets and press <Enter>. Now, press <Ctrl>D.

    $ cat > report1
    report 1
    Keeping a cat is a serious responsibility
    <Ctrl>D
    $ cat > report2
    report 2
    Cats need a balanced diet
    <Ctrl>D
    $ cat > report3
    report 3
    Responsible cat owners will neuter or spay their pets
    <Ctrl>D
    

  3. Now, put the three files together into a file called allreps. Type cat report1 report2 report3 > allreps and press <Enter>.

  4. Look at allreps by typing cat allreps and pressing <Enter>.

  5. Now try using the append symbol to put the files together in the reverse order. Type cat report3 report2 > repsagain and press <Enter>. Then type cat report1 >> repsagain.

  6. Type cat repsagain and press <Enter> to see what happened.
    $ cat report1 report2 report3 > allreps
    cat allreps
    report 1
    Keeping a cat is a serious responsibility
    report 2
    Cats need a balanced diet
    report 3
    Responsible cat owners will neuter or spay their pets
    $ cat report3 report2 > repsagain
    $ cat report1 >> repsagain
    $ cat repsagain
    report 3
    Responsible cat owners will neuter or spay their pets
    report 2
    Cats need a balanced diet
    report 1
    Keeping a cat is a serious responsibility
    

Next topic: Using pipes to build your own utilities
Previous topic: Background processing

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 03 June 2005