Unix / Linux: Save Output To File

by on February 4, 2010 · 0 comments· LAST UPDATED February 4, 2014

in , ,

I am new Linux and Unis shell user. In Unix or Linux, how do I save outputs in an external file say data.txt?

You can save the output to file using shell redirection. To redirect command output to a file, enter:

command > filename

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsNone
Estimated completion time2m
You need to replace command with the command you want to rune and filename with the file to which you want to direct the output for example:

date > output.txt
ls -l /etc/ > filelist.txt

To display saved output use cat command or more command as follows

cat output.txt
OR
more output.txt
Sample outputs:

Animated gif 01: Saving command output to a file

Animated gif 01: Saving command output to a file

How do I save (append) output to an existing output file?

You can append the output to the same file using >> operator, enter:

 
echo "Today's date is" >> foo.txt
date >> foo.txt
cat foo.txt

Sample outputs:

Today's date is
Tue Feb  4 23:26:06 IST 2014

How do I save output of one file to another?

You need to use either cp command or cat command as follows:

# use cp to duplicate file1 to file2
cp file1 file2
cat file1
cat file2
 
# use cat command to save output of file3 to file4
cat file3 > file4
cat file4
 

Redirect error and output messages to an output file

The following special command redirect both standard error and standard output data to an output file called file.txt

 
command > file 2>&1
find /etc/ -name "*.conf" > file.txt 2>&1
cat file.txt
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 0 comments… add one now }

Leave a Comment

Tagged as: , , , ,

Previous Faq:

Next Faq: