Saturday, April 15, 2006

A few essential Unix utilities

There are a few essential unix commands every developer should know. I have used these utilities on a number of occassions and found them to be quite useful. There are dozens, even hundreds of utilities available in unix but these four provide the most often used functionality needed to operate in a linux environment besides the obvious basics (ls, cd, mkdir, etc). I will just cover the basic uses of these commands, providing the most frequent usage. Note, some of these usages will only work on some versions of the command. For example, there are some differences on the way the details work between Linux and Solaris.

find - find the files your looking for, searches recursively
grep - search for strings and get their corresponding lines
awk - search for strings and permutate output (allows many more functionalties)
sed - stream editor, can be used for search and replace

Usage: find [searchfrom] -name [name of file]
Usage: find [searchfrom] -atime [-n/+n/n]

Example:
find . -name filename // find all files named filename in current working directory and down
find directory1 -name file* // find all files named file* in directory1 and down
find . -atime -3 // find all files accessed less then three days ago from current working directory and down
find . -atime +3 // find all files accessed less then three days ago from current working directory and down

An important difference between find and grep, grep does not search recursively unless you specify -r. Such as:
grep -rI [pattern] filename

Awk is a great way to parse through log files, flat files to extract tabular information like below; this will print the second word from each row that contains the word SearchString.
awk '/SearchString/ { print $2 }' < input_file

Sed allows you to search and replace from the commandline like so:
sed 's/search/replace/' file1 > file2

The above four can be combined with various other unix commands through a pipeline for some very powerful usages. But, thats a topic for another blog entry.

Pascal Aschwanden

0 Comments:

Post a Comment

<< Home