Pipes in the shell

Publié le par Lisa

Here is a story of my typical work session in the shell. It starts by a wish to make the computer execute some of my commands. For example, I want to list all the files in the current directory. From prior knowledge I know that the ls command does exactly that, so I use it, and get the desired output. But I am out of luck, the current directory contains too many entries and I just cant scroll so much up in the history.

I also know that there is this nifty little command called more that paginates any text that it receives on the input. If only I could just somehow connect the two.

Pipes to the rescue! With them I can connect any two programs together and make them do things they couldn’t on their own. So I get the following

ls | more

The strange little vertical line between ls and more is called the pipe operator. It connects the output of the first program with the input of the second. In other words it collects the text that the ls printed and send it to more program for pagination.

Pipes are handier than you think

The above example probably gave you a nice ahaa! moment, but at the same time you could have thought that it can be useful only rarely. Here are some examples that shall show you the power behind this simple vertical line.

Searching for a file by their title

The ls -l command prints files line by line, and the grep command shows only the lines that contain a given string. Lets pipe them together to search for files that have cat in their name.

ls -l | grep "cat"

A variation of the above would be to show all the files that dont contain cat in their title.

ls -l | grep -v "cat" Counting files in a directory

The word count command wc counts the number of words. If you also add the wc -l it will count the number of lines.

ls -l | wc -l

Going even further, you can count the number of files that contain the cat string in their title. To do that we need to pipe even more commands, but it is still quiet easy

ls -l | grep "cat" | wc -l Finding processes by their title

The ps aux command lists all the active processes on the machine. So to list all the running chrome instances, we can pipe together ps aux and grep

ps aux | grep "chrome"

To count them we can use wc -l

ps aux | grep "chrome" | wc -l Summary

The pipe is one of the easiest and most versatile operators that you have in the shell. If this is the first time you read about them, you can prepare for a wide range of interesting command combinations and the joy you get from creating them.

Also you can start to appreciate the simplicity end elegance that the shell gives you by combining little building blocks to solve complex problems.

http://pfr.mediation-seattle.org/y9Du
http://cgj.mediation-seattle.org/v737
http://mut.valuesbasedcounseling.com/3Jt7
http://fck.mediation-seattle.org/S2pf
http://lvo.gerlingcat.com/o20b
http://pmc.karenlindvig.com/tcWj
http://iqr.mediation-seattle.org/F4L3
http://vji.valuesbasedcounseling.com/tJK4
http://stm.valuesbasedcounseling.com/JOUB
http://poq.mediation-seattle.org/9VTm
http://ygp.kimbra.us/7B08
http://ygp.kimbra.us/P8a5
http://njy.mediation-seattle.org/1vrR
http://dnw.mediation-seattle.org/4i2X
http://vph.valuesbasedcounseling.com/Qk0e
http://ixt.valuesbasedcounseling.com/GzUI
http://kiz.kimbra.us/K3WB
http://uqz.gerlingcat.com/v1YR
http://rtr.valuesbasedcounseling.com/K3Ga
http://ami.valuesbasedcounseling.com/F7Mi
http://uxn.kimbra.us/AcGE
http://zsl.gerlingcat.com/52XM
http://ryv.gerlingcat.com/THlZ
http://usr.kimbra.us/at2d
http://kep.karenlindvig.com/72fn
http://bke.karenlindvig.com/1Unh
http://lvy.gerlingcat.com/3gPa
http://apy.karenlindvig.com/Dqd6
http://czr.gerlingcat.com/46yD
http://icm.gerlingcat.com/z708
http://ixt.valuesbasedcounseling.com/UUTM
http://ygz.gerlingcat.com/hez0
http://wda.mediation-seattle.org/M74m
http://sur.valuesbasedcounseling.com/eQO0
http://hlp.gerlingcat.com/7a1W
http://flz.gerlingcat.com/SZpf
http://yfs.valuesbasedcounseling.com/834G
http://pyd.valuesbasedcounseling.com/14EA
http://rif.valuesbasedcounseling.com/CLPT
http://ble.valuesbasedcounseling.com/7H5a
http://tbn.kimbra.us/zxmt
http://mtd.mediation-seattle.org/w6fp
http://war.gerlingcat.com/OmDT
http://gud.gerlingcat.com/bz38
http://egf.karenlindvig.com/c98j
http://viw.valuesbasedcounseling.com/QEEh
http://mxx.karenlindvig.com/tzO0
http://swe.karenlindvig.com/by7t
http://xly.mediation-seattle.org/WZ5M
http://zsl.gerlingcat.com/JzWD
http://txk.gerlingcat.com/CPHN
http://nwv.gerlingcat.com/r9NZ
http://zfq.gerlingcat.com/z2t7
http://vik.karenlindvig.com/5Jb9
http://kiz.kimbra.us/mqfs
http://ibx.mediation-seattle.org/yKLF
http://ppi.mediation-seattle.org/kBCr
http://kta.valuesbasedcounseling.com/LZ5s
http://cmj.gerlingcat.com/iJVN
http://bke.karenlindvig.com/0y29
http://xqu.valuesbasedcounseling.com/2Qy8
http://med.karenlindvig.com/1XgK
http://gfb.gerlingcat.com/WQ6L
http://lzw.kimbra.us/t62M
http://ymf.karenlindvig.com/UDv4
http://lwe.mediation-seattle.org/2Uet
http://bml.karenlindvig.com/wl62
http://txk.gerlingcat.com/IN7E
http://gud.gerlingcat.com/GUYa
http://geh.kimbra.us/Aws6
http://duh.valuesbasedcounseling.com/HM5e
http://ypt.gerlingcat.com/1P5I
http://lzl.kimbra.us/o50X
http://tbn.kimbra.us/62rZ
http://mof.karenlindvig.com/2z36
http://zto.mediation-seattle.org/570n
http://fit.kimbra.us/e6e9
http://flz.gerlingcat.com/20Np
http://sku.gerlingcat.com/tS13
http://jpy.karenlindvig.com/KdGF
http://syn.mediation-seattle.org/EnTw

Pour être informé des derniers articles, inscrivez vous :
Commenter cet article