Piping on Linux 101
Ever wondered how those Linux gurus write boggling commands on the command line and how it gets the job done?
Ever been in situations where you copied code to do something on your Linux machine, but never had a chance to understand what this weird looking fella ‘|’ meant?
Then you are on the wrong page gentleman, go study those huge ass Linux Manual pages and learn it for yourself. You cheeky goose monkey!!JOKING | KIDDING | BEING SARCASTIC
Here is my quick beginner friendly guide ‘Piping on Linux 101’ . Without further ado, let’s dive straight.
What actually is Piping?
For the newcomers, “Piping is nothing but connecting the stream of one command or program to another command or program. Before diving into ‘Piping’ let’s also understand what we meant by ‘stream’, stream is simply the standard output of one command or program. Let’s understand all this using a simple example.
Quick background, there’s a very very very common Linux command called ls
that simply lists all the files or directories in any targeted directory.
By default it lists the files for the directory that the user is currently in. So me currently being in ‘mediumTest’ directory, it listed all the files and directories that were inside it.
Now as you can see that the ‘stream’ in this case was the output of ls
command, we can further process that ‘stream’ to another command and make it do something else for us. But how to do that? This is where ‘Piping’ comes into action.
I’ll now simply pipe the output of ls
command to, say, sort
command that simply sorts whatever is passed to it.
ls | sort
Let’s understand the used jargon here, the Linux systems allow standard output of a command to be used as the standard input of another command using the pipe character ‘|’.
You can also visualize it as as a temporary connection between two or more commands/ programs/ processes.
Pipes are unidirectional, i.e: data flows in one direction, from left to the right.
syntax: command 1 | command 2 | command 3 | command 4...| command n
Here the output of command 1
will be processed as input of command 2
, and the cycle will go on and on till command n
;
Let’s see a few more examples,
ls -l /usr/bin/ | less
this will get the output of ls -l /usr/bin/
that will simply list all the files in /usr/bin/ directory in long format and then that will be passed to our less
command which in turn will output the information one sceenful at a time.
now the real question is, why is there a need to know this all, why couldn’t we simply use ls
and scroll through the output?
The thing is, it’s all about the efficiency of programmer, /usr/bin/
contains almost ‘1676’ files and scrolling through each of them would have been utter waste of time.
Also while quoting the above statement, to get the facts right I used piping, lemme show you how.
I simply piped ls -l /usr/bin
which lists all the files in line by line manner, with the command wc -l
which counts the number of lines.
This is one of the advantages of piping, it gets the job done, takes minimum viable time and is very very efficient.
What is Tee?
If you’ve ever seen plumbers work, all they do is piping, redirecting the flow of stream from one pipe to another. But sometimes they need to combine or divide the pipe flow and this is where, bad boy ‘Tee’ comes into action.
In concepts of piping we too have a tee, let’s see how it’s used through a command and its use case.
For example if you had to list the 3 largest files present in /usr/bin/
of your Linux machine, and you also got to store the same result in another file, say, ‘3badboys.txt’. And all that with one single line of command, how you gonna accomplish it? well, piping with tee is the answer.
Let’s think about the solution together and intuitively. We need to list files, so we can use ls
command, we also have to sort them according to file sizes, so we got to use the command sort
du -ha /usr/bin/ | sort -h | tail -4 | head -3 |tee 3bigboys.txt
du
is better than ls
for dealing with disk usage, -h
is for human readable, -a
for all inner files, here in the end with |tee
we are simply also channeling the output to a new file ‘3badboys.txt’. tail -4 | head -3
(use your head to decipher this, otherwise bug me in the comments section!!)
Let’s run this command,
great, now let’s also check if the file ‘3badboys.txt’ is created or not.
cat 3badboys.txt
That’s pretty much for ‘Piping on Linux 101’, see you homie.