Playing with pipes in Linux: practical examples

pipes (welded copper pipes)

The pipes or pipes they are one of the wonders of the Unix world that Linux has inherited. With them you can do a multitude of useful things in the terminal to link commands. Something you couldn't do if they didn't exist. But they still cause some confusion for some users with little experience or who have just reached the world * nix from another operating system such as Microsoft Windows.

Therefore, with this tutorial we are going to play with them by showing some practical examples that can help you in your day to day when working on the command line. You will see how they are quite simple to use and can contribute a lot. So I encourage you to keep reading and seeing the examples ...

  • "Dispense" the output of a command. In this way, you can use more or less to be able to navigate through the information output of any command. For example, the output of a list of files and directories, or that of processes that respond to the name "office":
ls -al | more

ps aux | grep office | less

  • Count the number of lines that has the output of a command or a file. For example, see the lines that an example.txt file has or the number of processes running (remember to subtract 1, because the first line is the header) and even the number of files or directories:
cat ejemplo.txt | wc -l
ps aux | wc -l
ls | wc -l

  • Locate a specific line or word, for example the IP starting with 192.168 of the active network interfaces:
 
ifconfig | grep 192.168
  • Locate specific values, for example the permissions of the files and directories, and show the PIDs of the corresponding processes with systemd:
 
ls -lR | grep rwx
ps aux -ef | grep systemd | awk '{ print $2 }'
  • Order the lines of a file in alphabetical order:
cat ejemplo.txt | sort 
  • See the first or last 10 lines of a file, but only those that contain a specific word:
head /var/log/syslog | grep WARNING
tail -f /var/log/syslog | grep error

The content of the article adheres to our principles of editorial ethics. To report an error click here!.

3 comments, leave yours

Leave a Comment

Your email address will not be published.

*

*

  1. Responsible for the data: AB Internet Networks 2008 SL
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   01101001b said

    Thanks for the article! I fully share the "wonders of the world Unix that Linux has inherited." To this day, I come across pipes that someone needed to write to handle a particular situation that are so great that one is flabbergasted wondering "does this work?" and the truth, yes, it works. They really are wonderful.

    1.    Isaac said

      Thank you for reading us!

  2.   Alexander Pinato said

    Excellent explanation. Thanks for sharing.

bool (true)