Pipes

General Idea for Pipes in UNIX:

Bad/Naive Approach: Using Files

Design Philosophy of Pipes:

Examples: Using pipes

# Count number of lines the who command returned
who | wc -l
# Search for all entries in world file for net-im packages
cat /var/lib/portage/world | grep "net-im/"
# Sort list of users
who | sort

Beginner Mistake: | v.s. >

Don’t confuse pipes and redirection!

Filters

Filters: Class of UNIX utilities that read from standard input, transform the file, and write to standard out.

Popular UNIX Filters

More on cat

cat copies its input to output unchanged (identity filter). When supplied a list of file names, it concatenates them onto stdout.

Some Options (Flags):

More on more

more is like cat, except it displays content page-by-page.

More on less

less lets you view the contents of a file similarly to more, except it doesn’t load the entire file at once.

More on head and tail

head displays the first few lines of a file.

Format: head [-n] [filename...]


tail displays the last few lines of a file.

Format: tail -number [rbc] [f] [filename]


Example: Using head and tail to get lines 4—10 of a file

tail -n +4 patch.sh | head -n 6

More on wc

wc counts the number of lines, characters, or words.

Options:

More on tee

Diagram demonstrating movement of data through tee

tee copies stdin to stdout for one or more files.

Format: tee [ -ai ] file-list

Example: Using tee to store the state of a pipe

ls | head -10 | tee first_10.txt | tail -5

More on cut

Note: Delimited data

cut prints selected parts of input lines.

Options:

Example: Using cut

$ cat /etc/passwd | cut -d: -f1
$ cat /etc/passwd | cut -d: -f1,7

More on paste

paste displays several text file “in parallel” on output.

Example: Using paste

Suppose we have the following files:

a.txt
---
1
2
b.txt
---
3
4
c.txt
---
5
6
$ paste a.txt b.txt c.txt
1       3       5
2       4       6

Example: Using paste to reverse a cut

(cut -f1 -d: < /etc/passwd) > 0_usernames.txt
(cut -f2 -d: < /etc/passwd) > 1_encrypted_password.txt
(cut -f3 -d: < /etc/passwd) > 2_uid.txt
(cut -f4 -d: < /etc/passwd) > 3_gid.txt
(cut -f5 -d: < /etc/passwd) > 4_fullname.txt
(cut -f6 -d: < /etc/passwd) > 5_homedir.txt
(cut -f7 -d: < /etc/passwd) > 6_loginshell.txt
paste -d: 0_usernames.txt 1_encrypted_password.txt 2_uid.txt 3_gid.txt 4_fullname.txt 5_homedir.txt 6_loginshell.txt

More on tr (translating strings)

tr copies stdin to stdout with substitution or deletion of selected characters.

Format: tr [-cds] [string1] [string2]

Examples:

# Replace all instances of s with z
tr s z
# Replaces all instances of s with z and o with x
tr so zx
# Replaces all lower-case characters with upper-case characters
tr a-z A-Z
# Deletes all a-c characters
tr –d a-c
# Change delimiter of /etc/passwd
tr ‘:’ ‘| /etc/passwd
# Change text from upper to lower case
cat lowercase.txt | tr '[A-Z]' '[a-z]' > uppercase.txt
# Change text from upper to lower case using named character classes
cat lowercase.txt | tr [:upper:] [:lower:]  > uppercase.txt
# Import DOS files
tr –d ’\r< dos_file.txt

Remember: tr translates strings character-by-character, it doesn’t substitute string-by-string.

More on sort

Format: sort [-dftnr] [-o filename] [filename(s)]

Examples:

sort -t: -nk2 /etc/passwd
sort -t: -nrk3 /etc/passwd

More on uniq (list unique items):

uniq removes or reports adjacent duplicate lines.

Format: uniq [-cduif] [input-file] [output-file]

More on find (apply expressions to files):

Format: find [pathlist] [expression]

Examples:

# Print all png files in my documents folder
find ~/Pictures -name '*.png'
# Delete all pdf files in my /tmp folder
find /tmp -name "*.pdf" -exec rm "{}" ";"
# Print all files in my videos folder larger than 500MiB
find ~/Videos -size +500M -print
# Print all config files modified in the last day
find ~/.config -mtime 1
# Count words of all config files modified in the last day
find ~/.config -mtime 1 -exec wc -w {} \;