ou can chain commands together by sending the
standard input. For those commands, you must read the
output of one command as input to another. This is
manual pages, but the most common way to indicate that a
known as piping, and is an alternative way to use
command should read standard input is to use a single dash
standard output. Instead of displaying on-screen or saving
(-) as an argument.
to a file, the standard output from one command becomes
the standard input for another command. The second
Piping is similar to redirecting output to a temporary file
command runs using the output from the first command,
and then applying a second command to that file, but it
and produces its own standard output, which you can then
cuts out the middleman. For example:
display, redirect to a file, or pipe to another command.
[ferro:˜] user% tail /var/log/system.log >
This technique gets its name from a special character called
the vertical pipe (|). On Apple keyboards, you create this
[ferro:˜] user% grep 'ferro.local' log.tmp
character by holding down the shift key and typing a
Dec 2 16:40:06 ferro root: setting hostname
backslash (\). The syntax for a pipe command looks like this:
[ferro:˜] user% rm log.tmp
first command and arguments | second command
You can accomplish the same result with a single command
You can link together as many commands as you want to
using pipes. Most commands that work with text, such as
head, tail, grep, and others, automatically accept
tail /var/log/system.log | grep "ferro.local"
standard input if there is no filename given to the second
CHAIN TEXT COMMANDS TOGETHER
в– The shell executes the first
В¤ Type a space, a vertical вЂ№ Type a second command
CHAIN TEXT COMMANDS
TOGETHER pipe character, and another to run on the output of the command and pipes the
вЃ„ Type a Unix shell space. first command, and press output to the second
Return. command as input.
command that displays
WORK WITH TEXT
You can chain together commands using the backtick ( `), a left-leaning
single quote character found in the upper-left corner of your keyboard.
You use the backtick to take the output of one command and provide it
as an argument to another command. You write the second command
normally, but where you want to insert the output of the first command,
you enclose it within a pair of backticks. For example, to see the size
and permissions of the executable file for the less command, you type
ls -l `which less`.
This input executes the which command, which returns a full pathname
to the less command, and then runs the ls -l command with that
pathname as an argument.
To move all files containing the word obsolete to a subdirectory, type
mv -i `grep -l 'obsolete' *.txt` old-files/
This input executes the grep -l command, which displays a list of files
containing the search pattern, and that list is used as a set of arguments
to the mv command.
вЂ№ Type grep and the second вЂ№ Type less and press
MATCH TWO PATTERNS PIPE OUTPUT THROUGH
WITH GREP THE LESS COMMAND
pattern, and press Return. Return.
вЃ„ Type grep, the first вЃ„ Type a Unix shell
в– The grep commands в– The output of the
pattern, and the file you command and a space.
display lines of text matching command is piped to the
want to search.
В¤ Type a vertical pipe
both patterns. less command for paging.
В¤ Type a space, the vertical character and a space.
pipe character, and a space.
UNIX FOR MAC
COMPARE TEXT FILES
lines or range of lines. The letter tells whether an addition
ou can compare two files by using the diff
was made, a, a line was deleted, d, or a line was changed, c.
command, which shows you the differences between
The versions from each file then appear, preceded by a less-
the files. You can compare three files by using the
than symbol for lines in the first file, and a greater-than
diff3 command. You use the diff command if you want
symbol for lines in the second file.
to compare two versions of the same file to see whether
the file has changed. The syntax for the diff command is:
You can also use the diff command to compare non-text
binary files; however, the diff command does not print
diff first-file second-file
the actual differences between files, because these are
likely to be non-printable characters. Instead, diff returns
The output from the diff command consists of those lines
you to the prompt if the files are identical, or prints a
that are not the same in both files. If the files are identical,
message saying that the binary files differ from each other.
the diff command prints nothing; you only see the
command line prompt again.
The diff command only compares two files. If you need to
compare three files, you can use the diff3 command.
If there are differences, the diff command reports them in
Note that there is not a diff4 command.
the following manner: The first line indicates the line
numbers within each file, with a single letter between the
COMPARE TEXT FILES
в– The shell indicates that в– A less-than symbol
вЂ№ Type the name of a
COMPARE TWO TEXT FILES
different file and press lines 3 and 4 have changed indicates a line change
вЃ„ Type diff and a space. Return. between the first and second in the first file.
В¤ Type the name of one file files.
в– The shell displays the в– A greater-than symbol
and a space.
differences between the files. indicates a line change
in the second file.
WORK WITH TEXT
The patch application uses a diff file to update an original
If you use output redirection to save the
file with the changes made to the diff file. This is useful if
output of the diff command into a file,
you are compiling new applications from updated source
the result is a diff file. You can use a diff
code. You can also use the patch application to update a
file to record changes between text files.
configuration file or any other text file. The syntax for the
For example, you may want to update a
patch command is
Web page by saving a copy of the HTML
text document and then changing a few patch original-file diff-file
lines. You can use the diff command
The original file, without the patches applied, is saved as
to determine what changes you made,
and save the results in a diff file.
You can use the output of the diff command with the ed
command, a command line tool that lets you edit text. To
make the output of the diff command into an instruction
list, or ed script, that the ed command understands, use the
-e option with the diff command.
В¤ Type three filenames
Note: If the files differ, the shell
COMPARE TWO BINARY FILES COMPARE THREE FILES
displays a message. If the files are separated by spaces, and
вЃ„ Type diff and a space. вЃ„ Type diff3 and a space.
identical, no message appears. press Return.
В¤ Type the name of one file,
в– The shell displays the
a space, and the name of
differences between the files.
another file, and press
UNIX FOR MAC
COUNT CHARACTERS, LINES,
AND WORDS IN TEXT
The wc command can also count lines, words, and
ou can measure the number of lines, words, and
characters from standard output. You can use the wc
letters in a file by using the wc command. The wc
command to count the results of other commands, such as
command, short for word count, also counts the
the grep command.
number of lines and characters, or bytes, in a file. The
output of the wc command looks like this:
For example, to count how many lines in a file contain a
specific pattern, you can use this command:
lines words characters filename
grep 'pattern' filename | wc -l
The lines value is the number of lines in the text. The
words value is the number of words; a word, for this
Keep in mind that this counts the number of lines that the
purpose, is any group of characters that does not contain a
grep command returns, and not the number of times that
blank line or space. The characters value includes printed
the pattern appears in the file. A single line may have the
letters, numbers, and symbols, as well as blank lines and
pattern repeated many times; thus, this is not an accurate
way to count the number of times a word, or pattern,
appears in a text file.
You can tell the wc command to print only the lines, words,
or characters by using the -l, -w, and -c options,
To count the number of files in a directory, you can pipe the
respectively. If you do not specify any of these options, the
output of the ls command to the wc -l command.
wc command lists all three values.
COUNT CHARACTERS, LINES, AND WORDS IN TEXT
в– The shell displays the вЂ№ Type a vertical pipe
COUNT WORDS AND COUNT THE NUMBER OF
LINES IN TEXT FILES IN A DIRECTORY
number of lines, words, and character and wc -l, then
вЃ„ Type wc and a space. вЃ„ Type ls and a space.
characters, respectively. press Return.
в– The shell displays the
В¤ Type the name of a file В¤ Type the pathname of a
number of lines of output
and press Return. directory and a space.
from the ls command.
WORK WITH TEXT
ARRANGE TEXT IN COLUMNS
The maximum width of the lines in the input file determines
ou can create neatly arranged columns of text by
the width of each column. If, for example, your Terminal
using the column command. It is easier to read long
window is 80 characters across and the maximum width of all
lists if they appear in multiple columns, as this allows
lines within the file is 21, then you can have three columns.