LFCS ADMIN EXAM PREPARATION GUIDE – Use input-output redirection (e.g. >, >>, |, 2>)

Use input-output redirection (e.g. >, >>, |, 2>)
Use input-output redirection (e.g. >, >>, |, 2>)

LFCS Admin Exam preparation guide series, main page can be found here.

Today, let us talk about input-output redirection available with Linux commands.

File Descriptors

stdin (0) – standard input – display how data is processed or entered for processing, typically this is input from the keyboard or the mouse, but it can be from the application.

stdout (1) – standard output – display data returned from command, the example will be running ls command on directory and display it to the screen

stderr (2) – standard error – display error message generated by the command. Below example, we can see an error message when we try to display the content of no existing file.

stderr example

As a server administrator, you will be performing list directories operation frequently. If you don’t want to see an error message on the screen or like to store it for later investigation, we can redirect the stderr to the file.

stderr message redirect to the file

Redirection Operators

| pipe – used to send output from one command to another command for processing – cat /var/log/syslog | less

The pipe operator is handy for displaying the output of the command and modifying it on the fly. In the below example, we want to view the content of the country-code file we want to be sorted and only display the first ten entries.

pipe example

> create/overwrite – used to write command output to the file, the file will be created if not exist already, it will overwrite the file if the file already exits – ls -l /etc/network > network.txt

The example below showing how we can use the > operator to create a file that contains a list of files and directories inside the directory. We are creating a file with an index of /etc/dpkg directory content because the file with name packages.txt was created as it didn’t exist before running the command it, and we can see the output of the command inside the file.

> operator usage example

>> create/append – it will append output to the file, it will create the file if not exist and add the output to existing file ls -l /etc/network >> network.txt

In the previous example, we create file packages.txt, we can add the information to this file by using >> operator. We run list command on the /etc/apt directory, and we append the output to the existing file, because of this we can see that the file contains new information add at the end of the file.

append operator usage example

< input – it will send data to the script for processing, but we can use with commands as well less < network.txt

Let us try now the input operator, we can see in the below example, we took the packages.txt file and inputted it the cat command. Using the input operation is not necessary in case of cat command, as we can run this command directly on the file.

input operator example

That is all for today, thank you for reading. Stay safe and healthy.

Advertisements