Linux Redirect Error Output To File

I am a new Ubuntu Linux and bash shell user. I also know how to redirect output from display/screen to a file using the following syntax:

ADVERTISEMENTS

cmd > file
ls > file

However, some time errors are displayed on screen. How do I store and redirect output from the computer screen to a file on a Linux or Unix-like systems?

[donotprint]

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsNone
Time2m
[/donotprint] Bash / ksh and other modern shell on Linux has three file descriptors:

  1. stdin (0)
  2. stdout (1)
  3. stderr (2)

Syntax To redirect all output to file

The syntax is as follows to redirect output (stdout) as follows:

command-name >  output.txt
command-name >  stdout.txt

Syntax To redirect all error to file

The syntax is as follows to redirect errors (stderr) as follows:

command-name 2> errors.txt
command-name 2> stderr.txt

Syntax to redirect both output (stdout) and errors (stderr) to different files

The syntax:

command1 > out.txt 2> err.txt
command2 -f -z -y > out.txt 2> err.txt

Syntax to redirect both output (stdout) and errors (stderr) to same file

The syntax is:

command1 > everything.txt 2>&1
command1 -arg > everything.txt 2>&1

Syntax to redirect errors (stderr) to null or zero devices

Data written to a null or zero special file is discarded by your system. This is useful to silence out errors (also know as ‘error spam’):

command1 2> /dev/null
command1 2> /dev/zero
command2 -arg 2> /dev/null
command2 -arg 2> /dev/zero

Tip: Use tee command to redirect to both a file and the screen same time

The syntax is:

command1 |& tee log.txt
## or ##
command1 -arg |& tee log.txt
## or ##
command1 2>&1 | tee log.txt

Another usage:

#!/bin/bash
# My script to do blah ...
foo(){
 :
} 2>&1 | tee foo.log

OR

#!/bin/bash
# My script to do blah ...
{
   command1
   command2
} 2>&1 | tee script.log
🐧 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 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 VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
11 comments… add one
  • Andrey Oliveira Jun 23, 2014 @ 12:15

    In newer versions of bash and sh, for “Syntax to redirect both output (stdout) and errors (stderr) to same file” you can also use:

    command1 &> everything.txt

    • Dexter Nov 26, 2014 @ 5:20

      That good ways…

  • Philippe Petrinko Jun 24, 2014 @ 8:07

    Dear NixCraft (or Vivek?)

    You may consider to explain a bit more [tee] command.

    1) It can output to more than one file
    2) It can overwrite files, but also append to files
    3) It can even ignore interrupts.

    — Philippe

  • Jalal Hajigholamali Jun 24, 2014 @ 8:32

    Hi,

    Thanks a lot
    useful article…

  • Unni Aug 22, 2014 @ 13:27

    is it possible to APPEND stdout and stderr to a file while printing to screen at the same time using TEE command.

  • xuhui Nov 24, 2014 @ 13:19

    Useful for me!!!!

  • Gopal May 24, 2015 @ 2:10

    use tee -a to APPEND output using tee
    example : command | tee -a outfile.txt

  • Gopal May 24, 2015 @ 2:15

    Best way to achieve all echo outputs to both screen and file ( as suggested above )
    {
    …. your complete shell script with multiple echo statements
    } > 2>&1 | tee -a script.log

  • aref ghobadi Aug 15, 2015 @ 9:56

    Hi
    thanks a lot

  • karthikeyan Dec 6, 2015 @ 17:09

    very useful. Thanks a lot.

  • rajesh Nov 27, 2016 @ 10:28

    Thanks very use full :)

Leave a Reply

Your email address will not be published.

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