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

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
[/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

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
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.