Updated: 17 October 2026 · Applies to: bash on any Linux distribution; tested on Ubuntu 26.04 (uutils tee 0.10.0, util-linux script 2.41.3, OpenSSH sftp)

To save terminal output to a file in Linux, add > file.txt after the command, for example df -h > df.txt. > replaces the file, >> adds to the end of it, > file.txt 2>&1 also saves error messages, and command | tee file.txt shows the output on screen and saves it at the same time.

1. Save output to a file: > and >>

uptime > report.txt       # create or overwrite report.txt
date -u >> report.txt     # add a line at the end
cat report.txt

> empties the file first without asking, so use >> for logs you want to keep. To put the date in the file name: df -h > "df-$(date +%F).txt".

2. Include error messages

Programs write normal output and error messages to two different streams (stdout and stderr). > saves only the normal output; errors still appear on screen. Lab example from our Ubuntu 26.04 test container:

$ ls /etc/hostname /nonexistent > out.txt
ls: cannot access '/nonexistent': No such file or directory
$ cat out.txt
/etc/hostname
  • command > all.txt 2>&1: output and errors in one file. command &> all.txt is the shorter bash form.
  • command 2> errors.txt: errors only.
  • command 2>/dev/null: throw errors away.

The order matters: command 2>&1 > file.txt still printed the error on screen in our test, because 2>&1 was applied before stdout went to the file. Write the file first, then 2>&1. More in how to redirect standard error in bash.

3. See the output and save it: tee

tee reads what a command prints, shows it on screen and writes it to a file. This is the way to pipe terminal output to a file while you still watch it:

$ df -h / | tee df.log
Filesystem      Size  Used Avail Use% Mounted on
overlay          48G   16G   30G  35% /
  • command | tee -a file.txt: append instead of overwrite.
  • command 2>&1 | tee file.txt or command |& tee file.txt: include errors.
  • command | tee a.txt b.txt: save to two files at once.
  • command | tee file.txt > /dev/null: save without showing it.

For tee with xargs and longer pipelines, see using pipelines, xargs and tee.

4. Write text into a file

echo "Hello" > hello.txt
printf 'line 1\nline 2\n' > lines.txt
cat > config.txt <<'EOF'
server_name example.com;
listen 80;
EOF

The last form (a "here document") writes several lines in one go; the quotes around 'EOF' stop bash from expanding $variables inside the text.

5. Writing to a file owned by root

sudo does not cover the >: your own shell opens the file before sudo runs. In our test:

$ sudo echo "nameserver 192.0.2.53" > /etc/lab.conf
-bash: line 1: /etc/lab.conf: Permission denied

Pipe into sudo tee instead, with -a to append:

echo "nameserver 192.0.2.53" | sudo tee /etc/lab.conf
echo "options edns0" | sudo tee -a /etc/lab.conf > /dev/null

6. Save a whole terminal session: script

To record everything you type and see, start script, work as usual, and type exit when done:

script session.log

For a single command: script -q -c 'df -h' session.log. The log starts with a "Script started on" line and ends with "Script done on" and the exit code. It keeps terminal control characters (line endings showed as ^M with cat -A), so read it with cat or less -R.

7. Save the commands, or the file you are editing

  • Save the commands you typed: history > commands.txt. Each line gets its number (1 echo one).
  • Save a file you are editing: in nano press Ctrl+O then Enter (see nano: save, exit and shortcuts); in vim type :w and Enter, or :wq to save and quit.
  • Protect files from an accidental >: set -o noclobber. bash then refuses with report.txt: cannot overwrite existing file; >| overwrites on purpose.

Is there a put command in Linux?

Not in the shell: typing it gives bash: put: command not found. put is a command inside sftp (and old ftp) that uploads a file to the server you are connected to, and get downloads one. Lab example (shortened):

sftp> cd /srv/upload
sftp> put report.txt
sftp> ls -l
-rw-r--r--    ? root     root            7 Oct 12 05:53 report.txt

Connect with sftp user@server first; put -r folder uploads a whole folder. See what port SFTP uses for the connection side. To keep output in a variable instead of a file, see assign the output of a command to a variable.

Official documentation: Bash manual: Redirections.

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Managed dedicated servers

our engineers look after the server for you, from $135 a month.

Was this answer helpful?

Still stuck? Open a ticket