Linux/Bash: Redirect Stderr and Stdout

This post will guide you how to redirect the standard error output to a file in Bash shell prompt. How do I redirect error messages from command prompt to a file. How to redirect both stdout and stderr to a file called fio.txt in Linux operating system.

The Standard error is also called as stderr, and it is the default error output from bash prompt. And standard output is also called as stdout, and it is the output of a command or scripts.

Redirecting Stderr only to a File


If you only want to redirect a standard error output to a file called fio.txt in Linux, you can use the 2> redirecting character in bash shell, type:

#ls /etc/xxxx 2> fio.txt

Outputs:

root@ubuntu-dev:/# ls /etc/xxxx

ls: cannot access '/etc/xxxx': No such file or directory

root@ubuntu-dev:/# ls /etc/xxxx 2>fio.txt

root@ubuntu-dev:/# cat fio.txt

ls: cannot access '/etc/xxxx': No such file or directory


Redirecting Stdout and Stderr Output into a File


If you want to redirect both the standard output and standard error output into a file called fio.txt, you can use the &> redirecting character, type:

# find /  -name a*.conf &> fio.txt

Outputs:

root@ubuntu-dev:~# find /  -name a*.conf &> fio.txt

root@ubuntu-dev:~# cat fio.txt

find: ‘/run/user/1000/gvfs’: Permission denied

/lib/modprobe.d/aliases.conf

/etc/security/access.conf

/etc/appstream.conf

/etc/init/avahi-cups-reload.conf

/etc/init/apparmor.conf

/etc/init/avahi-daemon.conf

/etc/init/alsa-utils.conf

/etc/init/acpid.conf

/etc/init/apport.conf

/etc/init/anacron.conf

/etc/sane.d/abaton.conf

/etc/sane.d/agfafocus.conf

/etc/sane.d/artec.conf

Redirecting Standard Error to Standard Output


If you want to redirect the standard error message to the standard output, you can use 2>&1 redirecting character. Type:

# find /  -name a*.conf 2>&1

 

You might also like:

Sidebar



back to top