How to Redirect Stdout And Stderr To A File in Linux

This post will guide you how to redirect system STDOUT and STDERR to a given file with one command under Linux Operating systems. How do I redirect the out of the stderr and stdout to a file in Linux. How to redirect stderr output to stdout output from the command line in Linux system.

redirect stdout and stderr to a file linux1

Redirect STDOUT to A file


when you run a command called “df -h” at the shell prompt, and it output will print in the screen in your Linux system. And if you want to redirect standard output to a given file called mydf.txt. You can use the following command:

$ df -h > mydf.txt

Outputs:

[root@localhost ~]# df -h > mydf.txt
[root@localhost ~]# cat mydf.txt
Filesystem Size Used Avail Use% Mounted on
devtmpfs 748M 0 748M 0% /dev
tmpfs 765M 0 765M 0% /dev/shm
tmpfs 765M 1.4M 763M 1% /run
tmpfs 765M 0 765M 0% /sys/fs/cgroup
/dev/mapper/fedora-root 40G 8.7G 29G 24% /
tmpfs 765M 80K 765M 1% /tmp
/dev/sda1 976M 133M 777M 15% /boot
tmpfs 153M 5.8M 148M 4% /run/user/1005
tmpfs 153M 0 153M 0% /run/user/0

You can see that the standard output has been redirected to that file named mydf.txt.

Redirect STDERR to A File


If you want to redirect standard error output to a file called myerr.txt on your Linux system, and you can run the following command:

$ command 2> myerr.txt
$ find /etc -name "selinux" 2>myerr.txt

Outputs:

[devops01@localhost ~]$ find /etc -name "selinux" 2>myerr.txt
/etc/selinux
/etc/sysconfig/selinux
[devops01@localhost ~]$ cat myerr.txt
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/lvm/archive’: Permission denied
find: ‘/etc/lvm/backup’: Permission denied
find: ‘/etc/lvm/cache’: Permission denied
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
find: ‘/etc/openvpn/client’: Permission denied
find: ‘/etc/openvpn/server’: Permission denied
find: ‘/etc/dhcp’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/audit’: Permission denied
find: ‘/etc/sssd’: Permission denied
find: ‘/etc/firewalld’: Permission denied
find: ‘/etc/nftables’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied

Redirect STDERR Output to STDOUT Output


If you want to redirect standard error message to your standard output, and you can type the following command:

$ command 2>&1
$ find /etc -name "selinux" 2>&1

Outputs:

[devops01@localhost ~]$ find /etc -name "selinux" 2>&1
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/lvm/archive’: Permission denied
find: ‘/etc/lvm/backup’: Permission denied
find: ‘/etc/lvm/cache’: Permission denied
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
find: ‘/etc/openvpn/client’: Permission denied
find: ‘/etc/openvpn/server’: Permission denied
find: ‘/etc/dhcp’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/audit’: Permission denied
/etc/selinux
find: ‘/etc/sssd’: Permission denied
find: ‘/etc/firewalld’: Permission denied
find: ‘/etc/nftables’: Permission denied
/etc/sysconfig/selinux
find: ‘/etc/sudoers.d’: Permission denied

Redirect Both STDOUT and STDERR Outputs to a Same File


If you want to redirect both standard output and standard error output to a same file named myout.txt, and you can type the following command:

$ command > myout.txt 2>&1
$ find /etc -name "selinux" myout.txt 2>&1

Outputs:

[devops01@localhost ~]$ find /etc -name "selinux" >myout.txt 2>&1
[devops01@localhost ~]$ cat myout.txt
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/lvm/archive’: Permission denied
find: ‘/etc/lvm/backup’: Permission denied
find: ‘/etc/lvm/cache’: Permission denied
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
find: ‘/etc/openvpn/client’: Permission denied
find: ‘/etc/openvpn/server’: Permission denied
find: ‘/etc/dhcp’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/audit’: Permission denied
/etc/selinux
find: ‘/etc/sssd’: Permission denied
find: ‘/etc/firewalld’: Permission denied
find: ‘/etc/nftables’: Permission denied
/etc/sysconfig/selinux
find: ‘/etc/sudoers.d’: Permission denied

Conclusion


You should know that how to use redirection character to redirect stdout and stderr output to a given file from the command line on your CentOS or RHEL or Ubuntu Linux system.

You might also like:

Sidebar



back to top