Useful commands

Introduction

Unix has become very large and it is often quite difficult to find a command to do what you want to do.  This guide lists some useful/popular commands, grouped by category, that are available on the School's system with a short explanation of each.  Obviously, to get more information you should use the man command.

Processes

ps

The ps program gives you information about active processes.  Without arguments it lists processes started by you from the same terminal as ps was launched.  To list all of your processes type ps -u username.  To list all processes running on the machine type ps -a.

top

This gives you a list about the top CPU processes sorted in order of raw CPU usage.  While running top you can use the following commands.

  • type u and then type a username to list only that user's processes.
  • type o and then type size, res, cpu or time
  • type k and then a PID to kill that process
  • type r and then a PID and a nice value to renice that process

Text Processing

awk

Awk is a very sophisticated program for pattern matching and processing text files.  Awk scans through each line of a file and if the pattern matches then the associated program is run.  As a simple example consider a payroll file which has on each line the name of the employee, their rate of pay, and the number of hours they worked.  e.g.
John 4.34 10
Bob 4.34 5
Mary 3.70 15
Mark 4.34 0
Fred 3.70 12.5

If that data was in a file called payroll and we ran the following command
awk '$3 > 0 {print $1, $2*$3}' payroll
then we would get this output
John 43.4
Bob 21.7
Mary 55.5
Fred 46.25
In this case the pattern to be matched was that field 3 ($3) was greater than 0.  If it was, then the program (the bit in {}) was run which printed out the first field and then the product of fields 2 and 3.  Awk is far to big to describe in full here.  Read man awk for a more extensive introduction

sort

Sort takes a file, or the standard input, and sorts the lines into alphabetical order.  Sort also has a few useful options.

  • the -u flag removes any duplicates from the file
  • the -n flag sorts in numerical order
  • the -r flag reverses the sorted output
  • the -c flag checks if the file is sorted and 
  • the -M flag sorts in month order

wc

This program performs a word count on the given file or standard input.  By default it will display lines, words, and characters.

fold

Reformats a text file so that a maximum line length is not exceeded.  E.g. fold -w 10 myfile would output myfile in lines of 10 characters.

Files

diff

Diff compares two files.  In its most basic form diff will compare two files line by line and report any differences. e.g. diff file1 file2.  If you want the comparison to be case insensitive then use diff -i file1 file2.  Using diff -w will ignore any white space in the file.

file

The file command attempts to determine what the type of a file is.  E.g. typing file this.ps (where this.ps is a Postscript file will produce) this.ps:     PostScript document.

split

The split utility splits a file into pieces.  It has the following options

  • -l n splits into sections of n lines
  • -b n splits the file into n byte sections
  • -b n k splits the file into sections of n kilobytes (i.e. n*1024 bytes)
  • -b n m splits the file into sections of n megabytes (i.e. n*1048576 bytes)

head, tail

These programs print the first or last ten lines of a file.

Miscellaneous Utilities

cal

The cal program displays a calendar for the given month or year.  For example type cal 09 2000 for a September 2000 calendar or cal 2000 for the whole year.  Remember to use 4 digit year numbers.

script

The script program logs all activity (input, output and error) that appears in a terminal.  This is very useful if you want to log your interaction with a program into a file.

time

This programs times how long another program takes to run. 

at

Lets you run a command at a later date/time.  See man page for examples.

Postscript

psnup

Shrink Postscript pages to fit on a single physical page.

psselect

Allows you to extract specific pages from a Postscript document.

psbook

Changes the order of Postscript pages so that they can be printed as a book.

html2ps

Convert HTML pages to Postscript.  Use the -D option to generate more compatible Postscript

ps2ascii

Extracts the ASCII text from a Postscript file.

Graphics

Netpbm

Netpbm is a large collection of graphics utilities.  Use man pages for further info.

gifdiff

Compares two gif images to see if they look similar.

gif2tiff

Converts GIF images to tiff format.