You can use the cat command to append data or text to a file. The cat command can also append binary data. The main purpose of the cat command is to display data on screen (stdout) or concatenate files under Linux or Unix like operating systems. To append a single line you can use the echo or printf command.
- >> filename : Appends standard output to file.
The syntax is:
cat file >> file2
echo 'text to append' >> file2
printf "text to append\n" >> fileName
Create a text file called foo.txt, type:
cat > foo.txt This is a test. I like Unix operating systems.
To save the changes press CTRL-d i.e. press and hold CTRL and press d. Create another text file called bar.txt as follows:
echo 'For desktop usage I prefer Apple OS X unix operating systems.' > bar.txt
Display both files on sceen, enter:
cat foo.txt cat bar.txt
To append a contains of bar.txt to to foo.txt, enter:
cat bar.txt >> foo.txt cat foo.txt
To append a 'Use unix or die' text to foo.txt file, enter:
echo 'Use unix or die' >> foo.txt cat foo.txt