Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fedora - Command line quick tips: Using pipes to connect tools

#1
Command line quick tips: Using pipes to connect tools

One of the most powerful concepts of Linux is carried on from its predecessor, UNIX. Your Fedora system has a bunch of useful, single-purpose utilities available for all sorts of simple operations. Like building blocks, you can attach them in creative and complex ways. Pipes are key to this concept.

Before you hear about pipes, though, it’s helpful to know the basic concept of input and output. Many utilities in your Fedora system can operate against files. But they can often take input not stored on a disk. You can think of input flowing freely into a process such as a utility as its standard input (also sometimes called stdin).

Similarly, a tool or process can display information to the screen by default. This is often because its default output is connected to the terminal. You can think of the free-flowing output of a process as its standard output (or stdout — go figure!).

Examples of standard input and output


Often when you run a tool, it outputs to the terminal. Take for instance this simple sequence command using the seq tool:

$ seq 1 6
1
2
3
4
5
6

The output, which is simply to count integers up from 1 to 6, one number per line, comes to the screen. But you could also send it to a file using the > character. The shell interpreter uses this character to mean “redirect standard output to a file whose name follows.” So as you can guess, this command puts the output into a file called six.txt:

$ seq 1 6 > six.txt

Notice nothing comes to the screen. You’ve sent the ouptut into a file instead. If you run the command cat six.txt you can verify that.

You probably remember the simple use of the grep command from a previous article. You could ask grep to search for a pattern in a file by simply declaring the file name. But that’s simply a convenience feature in grep. Technically it’s built to take standard input, and search that.

The shell uses the < character similarly to mean “redirect standard input from a file whose name follows.” So you could just as well search for the number 4 in the file six.txt this way:

$ grep 4 < six.txt
4

Of course the output here is, by default, the content of any line with a match. So grep finds the digit 4 in the file and outputs that line to standard output.

Introducing pipes


Now imagine: what if you took the standard output of one tool, and instead of sending it to the terminal, you sent it into another tool’s standard input? This is the essence of the pipe.

Your shell uses the vertical bar character | to represent a pipe between two commands. You can find it on most keyboard above the backslash \ character. It’s used like this:

$ command1 | command2

For most simple utilities, you wouldn’t use an output filename option on command1, nor an input file option on command2. (You might use other options, though.) Instead of using files, you’re sending the output of command1 directly into command2. You can use as many pipes in a row as needed, creating complex pipelines of several commands in a row.

This (relatively useless) example combines the commands above:

$ seq 1 6 | grep 4
4

What happened here? The seq command outputs the integers 1 through 6, one line at a time. The grep command processes that output line by line, searching for a match on the digit 4, and outputs any matching line.

Here’s a slightly more useful example. Let’s say you want to find out if TCP port 22, the ssh port, is open on your system. You could find this out using the ss command* by looking through its copious output. Or you could figure out its filter language and use that. Or you could use pipes. For example, pipe it through grep looking for the ssh port label:

$ ss -tl | grep ssh
LISTEN 0 128 0.0.0.0:ssh 0.0.0.0:* LISTEN 0 128 [::]:ssh [::]:*

* Those readers familiar with the venerable netstat command may note it is mostly obsolete, as stated in its man page.

That’s a lot easier than reading through many lines of output. And of course, you can combine redirectors and pipes, for instance:

$ ss -tl | grep ssh > ssh-listening.txt

This is barely scratching the surface of pipes. Let your imagination run wild. Have fun piping!




https://www.sickgaming.net/blog/2019/08/...ect-tools/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  Fedora - Fedora CoreOS out of preview xSicKxBot 0 8 01-18-2020, 09:40 AM
Last Post: xSicKxBot
  Fedora - Develop GUI apps using Flutter on Fedora xSicKxBot 0 16 01-16-2020, 03:08 PM
Last Post: xSicKxBot
  Fedora - How to setup a DNS server with bind xSicKxBot 0 36 01-14-2020, 09:50 AM
Last Post: xSicKxBot
  Fedora - How to setup multiple monitors in sway xSicKxBot 0 24 01-09-2020, 08:29 AM
Last Post: xSicKxBot
  Fedora - Most read articles in 2019 not from 2019 xSicKxBot 0 23 01-07-2020, 10:37 AM
Last Post: xSicKxBot
  Fedora - Tracking Translations with Transtats xSicKxBot 0 30 01-04-2020, 06:23 AM
Last Post: xSicKxBot
  Fedora - Top articles of 2019: Editors’ choice xSicKxBot 0 30 12-31-2019, 06:22 AM
Last Post: xSicKxBot
  Fedora - Top articles of 2019: For desktop users xSicKxBot 0 33 12-28-2019, 03:31 AM
Last Post: xSicKxBot
  Fedora - Best of 2019: Fedora for developers xSicKxBot 0 41 12-26-2019, 02:34 AM
Last Post: xSicKxBot
  Fedora - Best of 2019: Fedora for system administrators xSicKxBot 0 36 12-24-2019, 05:22 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

[-]
Welcome
You have to register before you can post on our site.

Username:


Password:





[-]
Statistics
» Members: 11,997
» Latest member: TaeIsKing23
» Forum threads: 11,107
» Forum posts: 11,496

Full Statistics

[-]
Online Staff
There are no staff members currently online.

[-]
Discord

[-]
Latest Threads
AppleInsider - New Apple iPhone 11 ads s...
Last Post: xSicKxBot
Today 04:03 AM
» Replies: 0
» Views: 34
Microsoft - Satya Nadella shares his tho...
Last Post: xSicKxBot
Today 04:03 AM
» Replies: 0
» Views: 4
News - Guardians for Australia
Last Post: xSicKxBot
Today 04:03 AM
» Replies: 0
» Views: 31
Steam - Dota 2 Update – January 16th, 20...
Last Post: xSicKxBot
Today 04:03 AM
» Replies: 0
» Views: 4
News - Rumour: Prolific Leaker Says We’l...
Last Post: xSicKxBot
Today 04:03 AM
» Replies: 0
» Views: 4
Xbox Wire - Paperbound Brawlers: A Seven...
Last Post: xSicKxBot
Today 04:03 AM
» Replies: 0
» Views: 2
News - Daily Deal – Beholder 2, 60% Off ...
Last Post: xSicKxBot
Today 04:03 AM
» Replies: 0
» Views: 2
News - GDC 2020 offers practical tips on...
Last Post: xSicKxBot
Today 04:03 AM
» Replies: 0
» Views: 3
News - Patton Oswalt's MODOK Marvel Seri...
Last Post: xSicKxBot
Today 04:02 AM
» Replies: 0
» Views: 2
News - Deals: Switch Lite’s Price Drops ...
Last Post: xSicKxBot
Yesterday 09:00 PM
» Replies: 0
» Views: 61

[-]
Twitter

[-]
Recent Posts
AppleInsider - New Apple iPhone 11 ads s...
New Apple iPhone 11 ...xSicKxBot — 04:03 AM
Microsoft - Satya Nadella shares his tho...
Satya Nadella shares...xSicKxBot — 04:03 AM
News - Guardians for Australia
Guardians for Austra...xSicKxBot — 04:03 AM
Steam - Dota 2 Update – January 16th, 20...
Dota 2 Update – Janu...xSicKxBot — 04:03 AM
News - Rumour: Prolific Leaker Says We’l...
Rumour: Prolific Lea...xSicKxBot — 04:03 AM

[-]
Top Poster
no avatar Congratulations to xSicKxBot, our current top poster for the last day with 13 posts!

Copyright © SickGaming.net 2012-2019