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

[donotprint][/donotprint]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

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

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post

How to Make Website WCAG Compliant?

Next Post

Link download Kali Linux 2020.1 (ISO + Torrent)

Related Posts