≡ Menu

Unix / Linux: Save Output To File

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

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
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
Share this tutorial on:

Like this? Follow us on Twitter OR support us by using Patreon

{ 0 comments… add one }
Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , , , ,