How To Save The Output Of A Linux/Unix Command To A File

I am new Linux and Unix-like system user who recently switched from MS-Windows XP. How do I save the output of a Linux / Unix ls command to a file named “lists.txt” using command prompt or POSIX shell such as SH/KSH/BASH?

Tutorial details
Difficulty Easy (rss)
Root privileges No
Requirements None
Time 2m
The Bash / KSH and POSIX shell provides a file redirection option to redirect the output of commands to a file using the following two operators:

  1. > is output redirection symbol.
  2. >> is append output redirection symbol.

Syntax to save the output of a command to a file

The syntax is:

command > filename

Example: Saving the date command output to a file called output.txt

In this example, save the output of date command to a file called output.txt:
$ date > output.txt

Example: Running Unix/Linux command and saving output to a file

Please note that file-lists.txt file is created if it doesn’t exist. And if file file-lists.txt file is overwritten if it exits.

Feel free to replace command with the command you want to run on Linux/Unix and filename with the file to which you want to save (direct) the output. For example, run ls command and store its output the file called “file-lists.txt”:

ls -l /bin > file-lists.txt

To see the contents of file-list.txt, use cat command as follows:

cat file-lists.txt

OR use NA command as follows for the same purpose:

more file-lists.txt

Sample outputs:

Animated gif. 01: Saving output of date and ls commands to a text file

Example: grep text and save to a file

You can use grep command to scan log files and save to a file. In this example, I am going to search/scan a log file called /var/log/httpd/access_log for spammer IP address ‘’ and save to a file called spam-log.txt:

 # command must be run as root #
grep '' /var/log/httpd/access_log  > /root/spam-log.txt

If your log file is too large, run a job (grep command) in the background with an ampersand (&) as follows:

 # command must be run as root #
grep '' /var/log/httpd/access_log  > /root/spam-log.txt &

Syntax to save and append the output of a command to a file

The > operator always overwrite existing output files. To append the output of a command to the same file use >> operator as follows:
command >> filename
In this example run two commands called date and who and save output to the same file called demo.txt:

echo "Test" > demo.txt
## Append to same file ##
echo "A log file by $USER on $HOSTNAME" >> demo.txt
who >> demo.txt
cat demo.txt

Sample outputs:

Animated gif 02: Unix/Linux: Bash shell append command output to a file

Syntax to redirection of both standard error and output to a file

The basic syntax is as follows for redirection of both standard error and output on bash/ksh or POSIX shell:

command-name &>filename
## POSIX syntax ##
command-name >cmd.log 2>&1

🐧 Please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 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 cron 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 VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
6 comments… add one
  • Maryann Grubber Apr 26, 2016 @ 19:36

    Thank you. I have been trying to save files for a machine transfer and trying not to duplicate. Wanted lists to compare and had forgotten how.mUch appreciated.

  • Haw May 12, 2016 @ 15:28

    Please add the tee command as well. Useful for interactive commands.

  • Inukaze Jun 4, 2016 @ 19:29

    Ok for example i wanna set slapd password under ubuntu 10.04 server


    sudo slappasswd > testpass

    But i can’t see when i put the password , i just need the last line of the command output to be redirected to a new file . how i can make it ???

  • DoB Sep 29, 2016 @ 15:08

    How can I store output of a command in a text file tab delimited way.

  • Anonymous Oct 6, 2020 @ 7:49

    Could you show how the error and output can save in the file and at the same time in the linux window(kernel)? However, the explanation is really useful.

Leave a Reply

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

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @