Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


LPI Linux Essentials Exam 010-160 - Topic 3.2 - Input/Output Redirection

The capability to direct streams of data within a shell environment is a cornerstone of effective Linux administration and scripting. Stream redirection manipulates how a command reads its input, writes its output, or reports errors. By mastering this skill, you not only improve data processing but also achieve more control over the system and your scripts. This guide walks you through the key operators for input/output redirection in Linux.

Fundamental Operators for Stream Redirection

The Pipe Operator |

The Pipe operator, denoted by |, takes the standard output (stdout) of one command and feeds it as the standard input (stdin) to another. This operator essentially links commands together in a chain, making it possible to create complex data transformations by connecting simple, single-purpose programs.

Exercise: Use the pipe operator to use the stdout of a command as stdin to another:

Use the ls command to list directory contents and pipe the output to wc to count the number of returned rows:

ls -l | wc -l

Redirecting Output to a File >

When you need to save the output of a command into a file, the > operator comes in handy. Using this operator will create a new file if it doesn't exist or overwrite an existing one.

Exercise: Save the output of a directory listing by redirecting to a file:

ls -l > redirect.txt

view the contents of redirect.txt using the cat command:

cat redirect.txt

Appending Output to a File >>

The >> operator is akin to the > operator, but instead of overwriting the existing content, it appends the new output at the end of the specified file.

Exercise: Redirect the output from the date command to append it to the bottom of redirect.txt:

date >> redirect.txt

Use tail to display the last 10 rows of redirect.txt to confirm the date is on the last line:

tail redirect.txt

Redirecting Input from a File <

The < operator is used to direct the input for a command from a file. This means that the command will use the contents of the specified file as its stdin instead of waiting for user input.

Exercise: Use input from file redirect.txt to count how many lines it has:

wc -l < redirect.txt

Using a Here Document <<

The Here Document, denoted by <<, is particularly useful for feeding multiple lines of input to a command directly within the shell or a script.

Exercise: provide multiple lines to the cat command for printing to screen:

cat << EOF
Line 1 of input
Line 2 of input
EOF

Capturing Error Output 2>

The stream redirection operators (>, >>, <) by default work with stdout. However, when you want to capture error messages in a file, you use 2> followed by the file name instead, with 2 representing stderr.

Exercise: Try listing a non-existent file to get an error and write it to error.txt:

First run the command without error redirection to see the error on screen:

ls idonotexist.txt

Now run the command with error redirection:

ls idonotexist.txt 2> error.txt

Redirecting Multiple Streams

There are instances when you might want to redirect both stdout and stderr. You can accomplish this by employing the &> operator or by using the special 2>&1 method. As stderr is represented by 2 and stdout is represented by 1, 2>&1 reads as: stderr goes to the same place as as stdout.

Exercise: Redirect both stdout and stderr to a file:

List a file that exists and does not exist and redirect the stdout and stderr to the same file using the &> method:

ls /etc/passwd idonotexist.txt &> output_and_error.txt

Display the contents of the output_and_error.txt:

cat output_and_error.txt

It should display:

ls: cannot access 'idonotexist.txt': No such file or directory
/etc/passwd

List a file that exists and does not exist and redirect the stdout and stderr to the same file using the 2>&1 method:

ls /etc/passwd idonotexist.txt > error_and_output.txt 2>&1

Display the contents of the error_and_output.txt:

cat error_and_output.txt

It too should display:

ls: cannot access 'idonotexist.txt': No such file or directory
/etc/passwd

Concluding Thoughts

Stream redirection is an indispensable skill. It offers you a versatile toolkit for data manipulation, log management, error handling, and creating intricate pipelines of commands. As you grow more proficient with these operators, you'll find that even intricate tasks become simpler to manage.


Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.