HB Computer Security | Aide Memoire
  • Introduction
  • Core Technical Skills
    • Core Skills
      • Linux
        • Getting to Know Linux
          • Using the Shell
            • Shells, Terminals, and Virtual Consoles
            • Choose your Shell
            • Shell Commands
              • Non-PATH Commands
              • Command History
                • Command Line Editing
                  • Keystrokes for Navigating Command Lines
                  • Keystrokes for Editing Command Lines
                  • Keystrokes for Cutting and Pasting Text from within Command Lines
                • Command Line Recall
                  • Keystrokes for Command Line Recall
              • Connecting and Expanding Commands
                • Piping Between Commands
                • Sequential Commands
                • Expanding Commands
            • Shell Variables
              • Common Shell Variables
            • Aliases
            • Create your Own Shell Environment
              • Modification Ideas
          • Navigating the Linux File System (LFS)
            • Filesystem Commands
            • Listing Files and Directories
            • File Permissions and Ownership
              • Modifying Permissions with chmod
              • Modifying Default Permissions with umask
              • Change File Ownership with chown
            • Copying, Moving, and Removing Files
            • Finding Files
              • locate
              • find
              • grep
            • Downloading Files
              • axel
              • wget
              • curl
                • User-Agent: Googlebot
          • Working with Text Files
            • Using vim and vi to Edit Text Files
              • Starting with vi
              • Adding Text
              • Moving Around in the Text
            • Text Manipulation
        • System Administration
          • Installing Linux
            • Installing from Live Media
            • Installing in the Enterprise
            • Partitioning Hard Disks
              • Tips for Creating Partitions
          • Account Administration
            • The root Account
              • Becoming root with su
              • sudo
                • The /etc/sudoers File
                • Granting sudo privileges
                  • visudo Guidance
                • Useful sudo Hints
            • Other Administrative Accounts
            • Standard User Accounts
              • Risks of userdel: Orphaned Files
          • Graphical Remote Administration
            • Cockpit
              • Installation Guide
            • Remote Desktop Protocol with xrdp
              • Installation and Configuration
            • Remote Desktop with vnc
              • Installation and Configuration
              • Running VNC as a System Service
          • Managing Running Processes
            • Listing Processes
              • ps
              • top
              • htop
            • Backgrounding and Foregrounding
              • Starting a Background Process
              • Using Foreground and Background Commands
            • Killing and Recining Processes
              • kill and killall
          • Managing Software
            • Managing Software from the Desktop
            • Going Beyond the Limitations of Software Center
              • Debian Packages
                • Advanced Package Tool (apt)
                • Repositories
                • dpkg
        • Shell Scripting
          • Variables
            • Command Substitution
            • Arguments
          • Reading User Input
          • if, else, and elif
          • BOOLEAN Logic
          • Loops
            • for Loops
            • while Loops
          • Functions
          • Local Vs Global Variables
          • Summary
        • Securing Linux
      • Windows
        • Security Hardening
Powered by GitBook
On this page

Was this helpful?

  1. Core Technical Skills
  2. Core Skills
  3. Linux
  4. Getting to Know Linux
  5. Using the Shell
  6. Shell Commands
  7. Connecting and Expanding Commands

Piping Between Commands

The pipe metacharacter, |, serves as a conduit, channeling the output of one command directly into the awaiting input of another. This seamless relay enables a succession of commands to collaboratively process data. Confused? Don't be - it's actually really easy.

Let's see an example that stitches together commands using pipes:

cat /etc/passwd | sort | less

This command:

  1. Prints the contents of the /etc/passwd file to the terminal

  2. Pipes the output to the sort command, which arranges the entries alphabetically based on the usernames leading each line

  3. Pipes the output to the less command, making it easier to browse through and navigate the output

Pipes perfectly embody the foundational ethos of UNIX, from which Linux evolved. At its heart, UNIX was envisaged as an operating system of interconnected building blocks. The principle was to cleverly chain these utilities together to handle a multitude of tasks.

Casting our minds back to the days before graphical word processors (sadly yes, I can remember those...), users penned plain-text files peppered with macros for formatting cues. To catch a preview of the document's actual appearance, they'd conjure up commands similar to:

gunzip < /usr/share/man/man1/grep.1.gz | nroff -c -man | less

Let's look more closely at what is happening here:

  1. The contents of the grep man page (grep.1.gz) are handed over to gunzip for decompression

  2. nroff steps in to format the page using the manual macro (hence, -man)

  3. For a tidy presentation, we send it through less

Given it's all in plain text, there's a bunch of flexibility here. You could sort the content, adjust parts, or even integrate text from another document.

A few points to zone in on...

  1. man pages are like user manuals for computer commands, explaining what they do and how to use them

  2. We asked gunzip to unpack the grep.1.gz file using a method called redirection. Essentially, we fed the file mentioned after the < symbol into gunzip. We'll dive deeper into this concept a bit later

  3. Note that we used two pipes (|) - practically speaking, you can chain together as many commands using pipes as you want, or at least until you hit system limits like memory or CPU constraints. Just beware of complexity...

The standout point? Instead of relying on a single, hefty program, we achieve our goal by seamlessly chaining commands together, with piping and redirection being the stars of the show.

PreviousConnecting and Expanding CommandsNextSequential Commands

Last updated 1 year ago

Was this helpful?