Unix Power Tools
Unix has many power tools to automate a lot of boring things. In this chapter, we’ll look at some of those tools.
Wildcards
Wildcard is pattern to specify match filenames.
The most common pattern is *, which matches any characters.
For example, the following program lists all the files with .c extension.
$ ls *.cThe other commony used wildcard pattern is [0-9], which matches any character in that range.
[0-9] - match any letter from 0 to 9
[0-9a-f] - match any letter from 0 to 9 and a to f
[abc] - match any one of a, b and cRegular expressions
Regular Expressions are used to search for and replace various patterns in text.
TODO:
Sort
Sorts the input.
TODO:
Uniq
Find the unique elements in a sorted input.
find - Finding files
The find is used to find files a directory that match given conditions.
USAGE:
find path expressionsLet’s look at some examples.
Find all the c files in the current directory.
$ find . -name '*.c'The name uses a wildcard pattern, a pattern in which *
Find all subdirectories in the current directory tree.
$ find . -type dFind all files in /usr/bin which are larger than 10MB.
$ find /usr/bin -size +10MFind all files in current directory which are modified in the last 2 days.
$ find . -mtime -2Find all files in current directory which are modified in the last 2 hours (120 minutes).
$ find . -mmin -120Find all the c files in the current directory and run wc command for each of them.
$ find . -name '*.c' -exec wc {} \;When using -exec, write the command and use {} where you want the matched file to substitued and end it with \;.
You can also do the same using xargs.
$ find . -name '*.c' | xargs wcGrep
TODO:
Awk
Awk is a mini language for manipulating columns of data.
TODO:
Problems
- Print all not empty lines in a file
- Print a file with line numbers
- Print only even lines in a file
Class Notes
See class notes on Unix Power Tools for the examples covered in the class.