CodingNic

Permissions, Redirection and Piping

Piping

Permissions, Redirection and Piping 15 min read

Piping

Objectives

By the end of this chapter, you should be able to:

  • Explain what head, tail, sort, uniq, wc, and grep do
  • Define what piping is
  • Chain commands together with | to solve problems no single command handles alone

๐Ÿ’ก Why this matters: Redirection connects a command to a file. Piping connects a command directly to another command โ€” that’s the difference between typing one command at a time and building a small pipeline, which is most of what real command-line work actually looks like.

A Few Commands Worth Knowing First

  • head โ€” prints the first lines of a file (-n picks how many)
  • tail โ€” prints the last lines of a file (-n picks how many)
  • sort โ€” sorts a file’s lines
  • uniq โ€” removes duplicate lines, but only if they’re already sorted and adjacent
  • wc โ€” counts words, lines, characters, and bytes

Combining Files with cat

Given first.txt:

text
First
Second
Third

and second.txt:

text
Fourth
Fifth
Sixth

cat first.txt second.txt concatenates them โ€” prints both, one after the other, as if they were a single file.

Piping

What if you want to concatenate two files and then count the words? Or concatenate and then sort? You need a way to feed one command’s output straight into another โ€” that’s what a pipe (|) does. Think of it as a hose connecting the output of the command on the left to the input of the command on the right:

bash
cat first.txt second.txt | sort

Pipes chain as many times as you want:

bash
cat first.txt second.txt | sort | head -n 2

Try reasoning through this one before running it:

bash
cat first.txt second.txt | sort | tail -n 3 | head -n 1

Step by step: concatenate the two files, sort the result, keep the last three lines, then keep just the first of those three. Net effect: the third-from-last line of the combined, sorted output โ€” without ever knowing how many lines the file has.

Searching with grep

grep searches text for a pattern, and it’s most useful piped in after something else. cat first.txt | grep First prints First โ€” the one line that matched. No match means no output at all; multiple matches means every one gets printed. Try:

bash
cat first.txt second.txt | grep Nope
cat first.txt second.txt | grep th

Putting It Together

Say petnames.txt contains:

text
Lassie
Moxie
Whiskey
Fido
Lassie
Moxie

Plenty of duplicates. Running uniq petnames.txt directly doesn’t remove them, because uniq only catches duplicates that are already next to each other โ€” the file needs sorting first:

bash
sort petnames.txt | uniq
text
Fido
Lassie
Moxie
Whiskey

Add redirection on top and you can send that straight to a new file instead of just printing it:

bash
sort petnames.txt | uniq > petnames_sorted.txt

Quick Reference

Command Does
head -n N First N lines
tail -n N Last N lines
sort Sorts lines alphabetically
uniq Removes adjacent duplicate lines (input must be sorted)
wc Counts lines, words, characters
grep pattern Prints only lines matching pattern

Try It

Pipes run against real files, so try these yourself:

  1. Create two short text files and use cat and | to combine and sort them in one line.
  2. Use wc piped after something else to count how many lines contain a specific word.
  3. Recreate the petnames.txt example with your own list of duplicated words, then sort, dedupe, and redirect the result to a new file โ€” all in one line.

Recap

  • | connects the output of one command to the input of the next, and you can chain several together.
  • head/tail grab lines from one end of a file, sort/uniq clean up ordering and duplicates, wc counts, and grep filters by pattern.
  • Piping and redirection combine freely โ€” a pipeline can end by printing to your screen or writing to a file.

Next module: environment variables, the PATH, and how to find and manage running processes.