Introduction to Linux_ 5 IO redirection

Posted by pimp3679 on Mon, 03 Jan 2022 16:27:31 +0100

I/O redirection

My blog
It will be discussed in the following sections:

  • Standard input, standard output, standard error
  • Redirection operator
  • How to use the output of one command as the input of another command
  • How to export the output of a command to a file
  • How to add the output of a command to a file as an extension
  • input redirection
  • Handling standard error messages
  • Combined input, output, error stream redirection
  • Output filtering

Simple redirection

What are standard input and standard output

Most Linux commands read input and make output, such as reading input from a file or other part and printing the output to the screen. By default, input is given by the keyboard and output is given by the screen. Your keyboard is your standard input (stdin) device, and your screen or specified terminal window is your standard output (stdout) device.

Because Linux is a flexible system, the default settings do not necessarily meet the above requirements.

Redirection operator

Use > and | for output redirection

Sometimes you want to put the output of one command into a file, or use the output of one command as the input of another command. This is called redirection. Redirection can be achieved by using the symbol > or | to send the output of one command to a file or as input to another command.

As we mentioned earlier, the cat command outputs the contents of the file to standard output. By redirecting to a file, all contents of the file will be output to the new file. If the new file does not exist, it will be created. If it does exist, the file contents will be overwritten:

$ cat file1
hello world
$ cat file2
goodbye world
$ cat file1 file2 > file3
$ cat file3
hello world
goodbye world

Redirecting empty content to a file is equivalent to emptying the file. This process is called truncation:

$ cat file2
goodbye world
$ >file2
$ cat file2

Some examples use pipes, such as matching a pattern in a text and excluding another pattern:

grep pattern1 file | grep -v pattern2

Print the catalog output one page at a time:

ls -la | less

Find a file in a directory:

ls -l | grep part_of_file_name

input redirection

Sometimes you want to use the contents of a file as input to a command, but the command cannot use a file as an option. Use the < operator for input redirection.

In the following example, take the contents in file1 as the input of grep command to find the line with hello:

$ cat file1
hello world
$ grep "hello" < file1
hello world

Redirection combination

The following example combines input and output redirection. First, check the spelling of file1 and redirect the output to a log file:

$ cat file1
abondon help me shat fake real reality
file sale englis dell dile
$ spell < file1 > error.log
$ cat error.log

In the following example, after extracting the keyword examine, save the help information of less to the examine files in less file:

$ less --help | grep -i examin > examin-files-in-less
$ cat examin-files-in-less
  :e [file]            Examine a new file.
  :n                *  Examine the (N-th) next file from the command line.
  :p                *  Examine the (N-th) previous file from the command line.
  :x                *  Examine the first (or N-th) file from the command line.
  +cmd                 Execute the less cmd each time a new file is examined.


If you do not want to overwrite a file, you can append the contents of the file through the > > operator:

$ cat wishlist
more equalization
less poverty
$ date >> wishlist
$ cat wishlist
more equalization
less poverty
2022 Monday, January 3, 2018:58:52 CST

Advanced redirection features

Use descriptor

There are three types of I/O, all of which have separate descriptors called file descriptors:

  • Standard input 0
  • Standard output 1
  • Standard error 2

In the following description, if the file descriptor number is omitted and the first redirection operator is <, the redirection is standard input. If the first redirection descriptor is >, the standard output is redirected.

Here are some examples:

ls >dirlist 2>&1

The standard output and standard errors will be redirected to the dirlist file.

ls 2>&1>dirlist

Will redirect only the standard output to the dirlist.

Don't be confused by & as we introduced earlier & to indicate that commands run in the background. Here, & it is only used to indicate that the number followed is not a file name, but the direction of the data flow. Another thing to note is that there is no space between the > symbol and the file descriptor.


Analysis error

If your process generates many errors, here is a way to view and analyze them one by one:

command 2>&1 | less

This is often used when creating a new file using make, for example:

$ make all 2>&1 | less

Separating standard output from standard errors

The following examples are often used by programmers. In this way, the output is displayed in one terminal window and the error is displayed in another window:

$ make all 2> /dev/pts/7

Standard output and redirection at the same time

Copy the input to standard output and one or more output files by using the tee command. Using TEE's - a option appends the contents of the file back. This command is important if you want to see standard output and save the output at the same time. > And > > symbols do not allow both operations to be supported at the same time.

This tool is usually invoked after a pipeline.

$ date | tee arvin_test_file
2022 Monday, January 3, 2009:31:57 CST
arv@arv-OptiPlex-7090:~$ cat arvin_test_file
2022 Monday, January 3, 2009:31:57 CST


When the program operates on the input content and prints the operation results to the standard output, it is called filtering. Filters are most commonly used to reorganize output.


See details

Filter output

Use the sort command to sort alphabetically:

$ cat people-I-like | sort
Arvin Du
Che Guevara
Friedrich Engels
Karl Marx

sort can also have more operation options, such as viewing the size of files. Using this command, smaller directories are listed first and larger files are listed later:

ls -la | grep -nk 5