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
Example :
We can see the data that would have gone to the screen with more command :
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 :
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.
Example:
Redirecting both Standard Ouput & Standard Error.
Use 2>&1 Syntax to redirect standard error to the same location as standard output .
Example: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.
Example: 2
Note that above example assumes that your system doesn’t have directory names “/usr2222/bin”
Redirecting Both stderr & stdout at Once
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.
Example:
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.
Sending Ouput 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.
Note : The file /dev/null is often called a bit bucket.
No comments:
Post a Comment