Time for some Linux Basics. Because the most important thing in every field is: Have your Basics straight! So let’s talk about Pipes and Redirection in Linux.
What are Pipes and Redirection in Linux?
Every single process in Linux has at least 3 communication channels available:
- Standard Input – STDIN
- Standard Output – STDOUT
- Standard Error – STDERR
The Kernel itself sets up those channels on the behalf of the process. The process itself doesn’t necessarily know where they lead. Most Linux commands accept input from STDIN and write output in STDOUT. Error messages are written to STDERR. This allows you to connect commands together to create pipelines.
The Shell uses the symbols <,> and >> as instructions to reroute the instructions of a command input or output to or from a file. The < symbol is connecting the command’s STDIN to the contents of an existing file. The > and the >> symbols redirect STDOUT. > replaces the file’s existing contents and the >> symbols append to them.
Let’s look at a couple of examples.
The following command would store the text you type between the ” ” in a file. If the file doesn’t exist, it will be created.
echo "Test Message" > testmessage
The next command would send an email with the contents of that file, so only the text, not the file itself, to the user Peter.
mail -s "testmsg" peter < testmessage
An example with the find command
If we use the find command we get a nice demonstration of why you would want to handle STDOUT and STDERR separately. If we run the following command:
find / -name core
We usually get a lot of Permission Denied error messages. To discard all of those error messages you can run the following command instead:
find / -name core 2> /dev/null
Which gives us a much cleaner result.
If we want to connect certain commands, or more specifically the STDOUT of one command and the STDIN of another, we can use the Pipe symbol | to do that. Let’s do an example:
ls | head -4
What this does is, it connects the ls command with the head command through the Pipe |. Meaning, it’s running the ls command with the head -4 extensions, listing only the first 4 files of that folder. You could also go ahead and pipe another command at the end of this one.
If you want that the second command only gets executed when the first one was successful, you can use the && symbols for that. For example:
lpr /tmp/test && rm /tmp/test
Would only remove the test file if it first was successfully queued for printing.
On the other hand, the || command would only execute the second command if the first command failed.
If you work with Linux on a regular basis, knowing what Pipes and Redirection in Linux does is very important. You will use them a lot if you need to work on the Command Line. I will make more of those shorter Linux Basics bits in the future. I don’t want those articles to get too long so that you can take the information in easier. Also, check some other Linux & Open Source Tutorials!