create account

Hack like a pro: shell `pipe` by imil

View this thread on: hive.blogpeakd.comecency.com
· @imil ·
Hack like a pro: shell `pipe`
Hi again UNIX shell lovers, as we saw in our [last course](https://steemit.com/linux/@imil/command-line-101-feel-the-power-of-the-unix-shell), a _shell command_ outputs non-errors messages to a special file called `stdout`, which number is `1` and reads from another special file called `stdin` which number is `0`. Error messages are sent to `stderr` which number is `2`.

To be clear, this means that the following command:

```
$ ls
. .. foo bar baz
```

displayed its result to the special file `stdout`, or `1`.  
This output can actually be _captured_ by another program by using the `|` (pipe) symbol. To illustrate its usage, we will use the `grep` command, which filters the output by matching a _pattern_. For example, if I would like to output only the lines that contain the word `match` in a file called `myfile.txt`, I would do:

```
$ grep match myfile.txt
beautiful match
matching
those matches
```

Instead of using a file as the input stream, we could use `stdout` from a previous command, which the `|` (pipe) command will transform to `grep`'s `stdin`!

```
$ echo 'I like to move it move it'|grep 'move'
I like to move it move it
$ echo 'this phrase won't match'|grep 'not present'
```

Think of `|` as a convenient way of filtering big outputs, for example you could very easily see if a directory contains a `.jpg` file by using `|grep '\.jpg'`. We won't go deep into `grep` patterns just yet to keep those courses nice and short :)

In our next shell tip, we will speak about redirections. Stay tuned!

Read more about standard streams in [Wikipedia's very well written documentation](https://en.wikipedia.org/wiki/Standard_streams).
👍  , ,
properties (23)
authorimil
permlinkhack-like-a-pro-shell-pipe
categorylinux
json_metadata{"tags":["linux","hack","technology","tips"],"links":["https://steemit.com/linux/@imil/command-line-101-feel-the-power-of-the-unix-shell","https://en.wikipedia.org/wiki/Standard_streams"],"app":"steemit/0.1","format":"markdown"}
created2017-06-29 14:46:27
last_update2017-06-29 14:46:27
depth0
children4
last_payout2017-07-06 14:46:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,657
author_reputation1,720,929,270
root_title"Hack like a pro: shell `pipe`"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id6,632,258
net_rshares2,336,439,190
author_curate_reward""
vote details (3)
@mikeill · (edited)
Very cool. I'm always using grep to search recursively: `grep -r 'something' somedirectory` so thanks for the reminder you can use it on a single file.

I also like to use it to filter results of find:

`ls -l | grep '2017'`

Would be files from this year in the current directory.

Following you.
👍  
properties (23)
authormikeill
permlinkre-imil-hack-like-a-pro-shell-pipe-20170629t151116622z
categorylinux
json_metadata{"tags":["linux"],"app":"steemit/0.1"}
created2017-06-29 15:11:15
last_update2017-06-29 15:12:39
depth1
children2
last_payout2017-07-06 15:11:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length297
author_reputation411,190,612,143
root_title"Hack like a pro: shell `pipe`"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id6,635,264
net_rshares1,143,286,972
author_curate_reward""
vote details (1)
@imil · (edited)
Thanks @mikeill, I'll eventually get to more difficult scenarios, my first target here are less fluent linux/unix users, I'd like to make them love the CLI ;) but comments from more educated users are really welcome too! Following you back
👍  
properties (23)
authorimil
permlinkre-mikeill-re-imil-hack-like-a-pro-shell-pipe-20170629t151529805z
categorylinux
json_metadata{"tags":["linux"],"users":["mikeill"],"app":"steemit/0.1"}
created2017-06-29 15:15:30
last_update2017-06-29 15:16:54
depth2
children1
last_payout2017-07-06 15:15:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length239
author_reputation1,720,929,270
root_title"Hack like a pro: shell `pipe`"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id6,635,865
net_rshares1,258,810,467
author_curate_reward""
vote details (1)
@mikeill ·
Sweet. See you around.
properties (22)
authormikeill
permlinkre-imil-re-mikeill-re-imil-hack-like-a-pro-shell-pipe-20170630t121816540z
categorylinux
json_metadata{"tags":["linux"],"app":"steemit/0.1"}
created2017-06-30 12:18:15
last_update2017-06-30 12:18:15
depth3
children0
last_payout2017-07-07 12:18:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length22
author_reputation411,190,612,143
root_title"Hack like a pro: shell `pipe`"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id6,753,403
net_rshares0
@steemitboard ·
Congratulations @imil! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

[![](https://steemitimages.com/70x80/http://steemitboard.com/notifications/posts.png)](http://steemitboard.com/@imil) Award for the number of posts published

Click on any badge to view your own Board of Honnor on SteemitBoard.
For more information about SteemitBoard, click [here](https://steemit.com/@steemitboard)

If you no longer want to receive notifications, reply to this comment with the word `STOP`

By upvoting this notification, you can help all Steemit users. Learn how [here](https://steemit.com/steemitboard/@steemitboard/http-i-cubeupload-com-7ciqeo-png)!
👍  
properties (23)
authorsteemitboard
permlinksteemitboard-notify-imil-20170630t171150000z
categorylinux
json_metadata{"image":["https://steemitboard.com/img/notifications.png"]}
created2017-06-30 17:11:48
last_update2017-06-30 17:11:48
depth1
children0
last_payout2017-07-07 17:11:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length685
author_reputation38,975,615,169,260
root_title"Hack like a pro: shell `pipe`"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id6,785,933
net_rshares1,160,697,434
author_curate_reward""
vote details (1)