Guide to unix tools

Prepared by Prasanna Vasudevan and Rahul Agarwal, CS107 TAs

Here are a few unix commands useful to developers.

diff

There are many situations when you want to compare the differences between two files. For instance, you may want to quickly compare your assignment's output to a reference implementation. The utility diff is perfect for this.

The command is:

diff -c file1 file2

The output puts a (+) in front of lines that are added in file 2, a (-) in front lines that were deleted, and a (!) in front of the lines that were changed.

grep

Another valuable utility is grep, which can search for a specified pattern in files. This is can be useful, for instance what if you were looking for all the places where you call a particular function. The syntax for using grep is:

grep pattern filename

You may choose to specify multiple filenames if you want, or you may choose to omit it. If you omit a filename, grep will read from the terminal's standard input.

For example, if we were looking for all the lines where the binky function is called , we could use grep to search the source file as follows:

grep "binky()" program.c

What really gives grep its power is that it can match not only exact strings, but also general patterns. For example, a . in a grep pattern matches any character, and following it with a * means the character can be repeated any number of times. Consider the case where binky() actually takes a number of arguments, so matching on binky()will not suffice. We can instead search

grep "binky(.*)" program.c

These patterns are called regular expressions" and they can get very complex (a full treatment of this would be another discussion entirely). For the most part, you will not need to know regular expressions in depth.

Two important flags that you can use with grep are -i and -v. The -i flag makes the search case-insensitive and the -v flag negates the search, so grep will return everything that doesn't match the pattern.

I/O Redirection

Frequently situations arise where you would like to write the output of a shell command to a file. For example you might want to write to output of a diff to a file to make it easier to read. The Unix terminal provides a simple way to do this. The syntax is as follows:

command > filename

For example, to write the results of a diff to a file differences.txt , the command would be:

diff -c file1.txt file2.txt > differences.txt

To append to the end of an existing file, the syntax is:

command >> filename

If want to read from a file into standard input, rather than writing from output:

command < filename

You can also redirect the output of one command to the input of another input. The result of this is the same as writing the output if the first command to some temporary file, and then reading the input into the second command. This is called “piping” the output of the first command into the second, and is accomplished as follows:

command1 | command2

For example, say for some reason we wanted to sort the results of a grep operation alphabetically. The sort command takes input from the terminal and sorts it lexicographically, so we could accomplish this whole task as follows:

grep "foo" | sort

If we wanted to write this result to a file, we could execute the command:

grep "foo" | sort > sorted_results.txt