Every process in Linux is provided with three open files( usually called file descriptor). These files are the standard input, output and error files. By default :
- Standard Input is the keyboard, abstracted as a file to make it easier to write shell scripts.
- Standard Output is the shell window or the terminal from which the script runs, abstracted as a file to again make writing scripts & program easier
- Standard error is the same as standard output:the shell window or terminal from which the script runs.
A file descriptor is simply a number that refers to an open file. By default , file descriptor 0 (zero) refers to the standard input & often abbreviated as stdin. File descriptor 1 refers to standard output (stdout) and file descriptor 2 refers to standard error (stderr). These numbers are important when you need to access a particular file , especially when you want to redirect these files to the other locations, File descriptors numbers go up from zero.
Redirecting Standard Output
Syntax to redirect the output of a command to a file.
# Command_options_and_arguments > output_file
[email protected]:~$ cat /proc/cpuinfo > command.txt
We can see the data that would have gone to the screen with more command :
[email protected]:~$ more command.txt processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 37 model name : Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz stepping : 5 microcode : 0x616 cpu MHz : 0.000 cache size : 6144 KB physical id : 0 siblings : 2 core id : 0 cpu cores : 2 apicid : 0 initial apicid : 0 fpu : yes fpu_exception : yes cpuid level : 5 wp : yes
The > operator tells the shell to redirect the output of the command to the given file. If the file exists , the deletes the old contents of the file and replaces it with the output of the command.
Redirecting a Command’s Input
Syntax to redirect the input of a command to come from a file.
# Command_options_and_arguments < input_file
Use the < operator to redirect the input for a command , example is shown below :
[email protected]:~$ wc -l < command.txt 52
In this example , the input to the ‘wc‘ command comes from the file named command.txt. The shell sends the contents of the file command.txt as a standard input for the wc command.
Note : We can also combine both redirections with following syntax :
# command_options_and_agruments < input_file > output_file.
Redirecting Standard Error
In addition to redirecting the standard input and output for a script or a command, we can also redirect standard error. Even though standard error by defaults goes to the same place as the standard output – the shell window or terminal. There are good reasons why stdout and stderr are treated separately. The main reason is that we can redirect the output of a command or commands to a file but you have no way of knowing whether an error occurred. Separating stderr from stdout allows the error message to appear on your screen while output still goes to a file.
Syntax to redirect stderr from a command to a file.
# command_options_and_agruments 2> output_file.
The 2 in 2> refers to the file descriptor 2, the descriptor number for stderr.
[email protected]:~$ lsash /usr/bin 2> commands-error.txt [email protected]:~$ cat commands-error.txt No command 'lsash' found, did you mean: Command 'sash' from package 'sash' (universe) lsash: command not found
Redirecting both Standard Output & Standard Error
Use 2>&1 Syntax to redirect standard error to the same location as standard output .
[email protected]:~$ ls /usr/bin > command.txt 2>&1
Above Command has three parts.
- ls /usr/bin is the command run
- > command.txt redirects the output of the ls command
- 2>&1 sends the output of the file descriptor 2, stderr , to the same location as the file descriptor 1, stdout.
[email protected]:~$ ls /usr2222/bin > command.txt 2>&1 [email protected]:~$ more command.txt ls: cannot access /usr2222/bin: No such file or directory
Note that above example assumes that your system doesn’t have directory names “/usr2222/bin”
Redirecting Both stderr & stdout at Once
[email protected]:~$ ls /usr2222/bin &> command.txt [email protected]:~$ more command.txt ls: cannot access /usr2222/bin: No such file or directory
In the above command ls is the command , /usr2222/bin is the argument to the ‘ls‘ command and ‘&> command.txt‘ redirect both stdout and stderr to a file named command.txt.
Appending To Files
Use the ‘>>’ operator to redirect the output of a command , but append to the file , if it exists. The syntax is given below :
# Command >> file_to_append.
[email protected]:~$ uptime >> sysload.txt [email protected]:~$ uptime >> sysload.txt [email protected]:~$ uptime >> sysload.txt [email protected]:~$ more sysload.txt 11:49:17 up 1:22, 3 users, load average: 0.28, 0.12, 0.11 11:49:28 up 1:22, 3 users, load average: 0.40, 0.15, 0.12 11:49:36 up 1:23, 3 users, load average: 0.33, 0.14, 0.12
Truncating Files :
We can use a shorthand syntax for truncating files by omitting the command before > operator . The Syntax is given below :
# > file_name
We can also use an alternate format with a colon :
# : > file_name
Both of these command-less command will create the file if it does not exist and truncate the file to zero bytes if the file does exist.
[email protected]:~$ ls /usr/bin > command.txt [email protected]:~$ ls -l command.txt -rw-rw-r-- 1 linuxtechi linuxtechi 19713 Dec 2 12:18 command.txt [email protected]:~$ > command.txt [email protected]:~$ ls -l command.txt -rw-rw-r-- 1 linuxtechi linuxtechi 0 Dec 2 12:18 command.txt
Sending Output to Nowhere Fast
There are some scenarios where you not only want to redirect the output of a command , you want to throw the output away. You can do this by redirecting a command’s output to the null file “/dev/null” The null file consumes all output sent to it , as if /dev/null is a black hole star.
[email protected]:~$ ls /usr/bin > /dev/null
Note : The file /dev/null is often called a bit bucket.
Recommended Read : How to Use Encrypted Password in Linux Bash Shell Script