Linux / Unix: Use Cat Command To Append Data To a File

by on January 7, 2013 · 5 comments· LAST UPDATED April 27, 2013

in , ,

I am a new Unix user. I have Debian Linux installed. I need to append text to a file called daily.log. How do I use the cat command to append data to a file?

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.

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsNone
Estimated completion timeN/A

Redirection symbol

Syntax

The syntax is:

cat file >> file2
echo 'text to append' >> file2
printf "text to append\n" >> fileName

Examples

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

Sample outputs:

Fig.01: Using the cat and echo command to append a text to a file.

Fig.01: Using the cat and echo command to append a text to a file.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 5 comments… read them below or add one }

1 sindhu March 8, 2014 at 5:03 am

when i execute two commands like echo $val(nn) > t.txt and awk -f throughput.awk wpan.tr >t.txt
i want the file t.txt to have two columns side by side leaving a tab or space in between. but i m getting output like this

7
81.85
7
67.09

pls help in this regard…thanks

Reply

2 Nix Craft March 8, 2014 at 6:19 am

I’m not sure I understood your query. To merge corresponding or subsequent lines of files try paste command.

Reply

3 camden_kid April 23, 2014 at 12:56 pm

Thanks.

Reply

4 chiranjeevi June 18, 2014 at 5:04 am

Hi,

plz help me for below req.

I want to remove common numbers from two files.

ex: In file1 numbers are 1,2,3,4 and In file2 are 1,2,3,4,5,6,7,8,9,10.

I need a out like 5,6,7,8,9,10….
Plz help me
Thx in advances….

Reply

5 Nilabh November 12, 2014 at 12:16 pm

Sed command will suffice your requirement.

Reply

Leave a Comment

Tagged as: , , , ,

Previous Faq:

Next Faq: