Basic UNIX Tools

This page provides brief description and usage for several basic UNIX tools. We encourage you to use look up the man page for further detailed information.

Prepared by Douglas Chen for CSC209F '98

Tool List

Other day-to-day UNIX commands

cp, mv, rm, du, df, finger, who, more, less, cat

Few examples


awk

Awk is a powerful pattern scanning and processing language which has similar functionalities as sed and grep. Because awk script can be written in very few lines, often it is written, used, and then discarded. The awk language is similar to the C language and hence quicker to learn if you know C but it also provides a easier way to familiarize people who are new to C.

Note that the awk language cannot be fully explained in just few examples. For those people who are interested in more aspects of awk, books on this topic can be found in any computer bookstore.

Here are some examples:

  % awk '{ print $NF : $0}'
  CSC209F is an interesting course.
  [Ctrl-d]
  5: CSC209F is an interesting course.
The above example takes data from stdin (adding a file name at the end of the command line allows awk to read data from the file specified) and prints out number of fields (NF), a colon, and the entire line ($0).
  % awk '/Ann/' name.list
  MarryAnne
  Ann
  DeAnne
This example is the same as "grep Ann name.list" which lists all occurrence of the pattern Ann in the file name.list.
  % finger
  Login    Name                     Host               TTY      Idle LoginTime
  ange     Angela Glinos            cheetah            console    4d Fri 09:26
  at209che Douglas I-Hsi Chen       eddie              pts/46        Tue 09:37
  jdd      John DiMarco             mammoth            pts/41      4 Tue 09:33

  % finger | awk '{ print $1, $3}' 
  ange cheetah
  at209che eddie
  jdd mammoth
This example shows how to print the first field ($1) and the third field ($3) of each line piped from finger. The default input field separator (FS) is a white space. You can change its value to a comma (,) for example by using the -F option (i.e. finger | awk -F, '{print $1, $3}' or finger | awk 'BEGIN{FS=","} {print $1, $3}')

See also: sed, grep

chmod

Chmod modifies the file permission associated with a file. The default file permission on CDF when a file is created is 600 (or -rw-------). Note that 6 in 600 is the sum of r (4) and w (2). The execution permission x has value of 1. Therefore the permission 741 is equivalent to -rwxr----x.

Here's how you can change the permission to something else.

  (original permission, -rw-------)

  % chmod 644 myfile           (now change to -rw-r--r--)
  % chmod a=r myfile           (now change to -r--r--r--)
  % chmod ug+w myfile          (now change to -r--rw-rw-)
See also: umask

compress, uncompress, zcat

Compress reduces the size of the given file using Lempel-Ziv algorithm while uncompress reverse the effect and expands the compressed file back to its original form. The default extension .Z is used for compressed files. Here's an example on how compress and uncompress are used.

Zcat is equivalent to 'uncompress -c'

  % compress myfile            (myfile becomes myfile.Z)
  % uncompress myfile.Z        (myfile.Z becomes myfile)
  % uncompress -c myfile.Z     (dump content of myfile.Z to standard output,
                                myfile.Z does NOT become myfile)
  % zcat myfile.Z              (same as above)
  % zcat myfile.Z > myfile     (redirect stdout to a file)
See also: gzip/gunzip

date

Date prints current date and time to standard output. Users can customize the output format instead of the default setting.
  % date '+DATE: %m/%d/%y%nTIME: %H:%M:%S'

  (result)
   DATE: 09/28/98
   TIME: 14:45:05

diff

Diff tells you the differences between files.
  % diff file1 file2           (basic use of diff)
  % diff -i file1 file2        (tell diff to ignore case)
Try options: -c, -e, -f, -h, -n

See also: cmp

grep

Grep is perhaps the most useful tool you will encounter in software development. It is a pattern matching tool allowing users to specify a pattern using regular expression. Commonly used commands are as follows.
  Note: story.txt is the text file to be searched

  % grep green story.txt       (show all lines that contain 'green' in them)
  % grep -i green story.txt    (ignore case for the word 'green')
  % grep -v green story.txt    (show all lines that does not have 'green')
  % grep ^green story.txt      (show all lines that begins with 'green')
Try options: -A, -B, -e, -f See also: sed

gzip, gunzip, zcat

Gzip reduces the size of the named files using LZ77 algorithm. In most cases, the resulting files are smaller than ones produced by compress. It is a common practice to tar and then gzip files for distribution purposes.

Here are some examples:

  % gzip myfile                (produces gzip'ed myfile.gz)
  % gunzip myfile.gz           (restore myfile.gz to myfile)
  % gzip -d myfile.gz          (same as above)
  % gzip -cd myfile.gz         (dump content of myfile.gz to stdout,
                                user can then pipe stdout to more/less)
  % gzip -cd myfile.tar.gz | tar xf -
                               (gunzip and untar the file in one command)
See also: tar/untar

head, tail

Head outputs the first part of files while tail outputs the last part of files. By default the first (last) 10 lines are printed by head (tail).
  % head myfile                (output first 10 lines of myfile)
  % head -c 1k                 (output first 1-kilobyte of myfile,
                                subs k with b (byte) or m (megabyte))
  % tail -5 myfile             (output last 5 lines of myfile)
  % head -15 myfile | tail -10 (output line 5 to line 15 of myfile)
Try: tail -f myfile

lpr, lpq, lprm

These are the printing utilities available for use in most of UNIX environment. Lpr uses a spooling daemon to print files when the printer becomes available. Lpq shows current status of the printer while lprm allows users to remove a particular printing job waiting to be processed. You can send a PostScript file directly to a printer with lpr if it understands the language!

Note that the -P option is available to all three programs and can be used to select a printer other than the default one or specified in the PRINTER environment variable. If you don't like to use the -P option every time, modify the PRINTER variable!

Here are some examples:

  % lpr story.txt finale.ps  (prints out story.txt and finale.ps)
  % lpr -Preuse *            (prints all files and redirect to printer 'reuse')
  % lpq                      (show status of default printer)
  % lpq -Preduce             (show status of printer 'reduce')
  % lprm 1023                (remove printing job 1023 from printing queue)
Try lpr options: -T, -U, -F, -R, -i

ls

Ls lists contents of directories.
  % ls -a          (list all files including .*)
  % ls -d          (list directory entries instead of contents)
  % ls -i          (list inode of each file)
  % ls -l          (list files in long format)
  % ls .*          (list files begin with .)
Try options: -ld, -u, -g, -r, -t

man

Man displays the manual page for commands specified. Try the following options.
  % man man                    (show man's own manual page)
  % man -k jump                (keyword search on 'jump')
Try options: -f, -M

pquota

Reports printing quota allocated to the user. Simply type 'pquota -v' to see the status of your printing quota.

See also: lpr, lpq, lprm, quota

ps

This command is used to see the status of current processes. Try 'ps', 'ps -a', 'ps -x' and see the man page for more information.

pwd

Pwd prints the name of current (present) working directory.
  % pwd
  /home/u2/csc209h/a209abcd

quota

Quota displays your file system quota and usage information. Use 'quota username' to see info about a specified user.

See also: pquota

sed

Sed is the stream editor. It is one of the most powerful and useful tools like grep for shell scripting, etc. It evolves around regular expressions, therefore, it is important to gain some basic understand about the topic before using sed.

Here are some examples:

  % sed 's/John/Johnson/g' report.txt  (change all occurrence of John to Johnson
                                        in report.txt)
  % sed 'y/abc/ABC/' report.txt        (replace all occurrence of a to A, b to
                                        B and c to C)
  % sed '1,10 d' report.txt            (delete the first 10 lines)
See also: sed's man page

sort

Sort sorts input in alphabetical order line by line. Try out the following options. Simply type 'sort filename' to have the result dumped to stdout.

    sort -n             Sort in numeric order
    sort -nr            Sort in reverse numeric order
    sort +      Sort starting at +1-st field
    sort -r             Reverse normal order
    sort -u             Discard duplicates (display unique lines)
See also: grep

tar

Tar is the tape archive program which helps user to backup files. There are numerous options provided by the program but ordinary users only needs few.
  (at one directory above directory 209, do the following)

  % tar cf 209.tar 209        (create a tar file 209.tar which contains
                               everything in the directory 209)

  (with newer version of tar, you can gzip the tar file in a single command)

  % tar czf 209.tar.gz 209

  (now, untar/restore the tar file back to the file system)

  % tar xf 209.tar

  -OR-

  % cat 209.tar | tar xf -    (tar takes input from stdin)

  -OR-

  % tar xzf 209.tar.gz
See the man page for more information.

wc

Wc is the word counting program. It can reports number of characters, words and lines in a given file or files.
  % wc myfile                  (output number of chars, words and lines)
  % wc -c myfile               (output number of characters only)
  % wc -l myfile               (output number of lines only)
  % wc -w myfile               (output number of words only)

Few examples

Who is online

This example prints login names of people who are currently online. All records are sorted and are unique.
  % finger | grep -v ^Login | awk '{ print $1}' | sort -u
  a209witt
  a209zhou
  a2501pyk
  a270cost
  a270razv
  a324anis
Next example prints the names of people who are currently online. All records are sorted and are unique.
  % finger | grep -v ^Login | cut -b10-34 | sort -u
  Angela Glinos            
  Peng Yi                  
  ordinary user            
  xiaoyu gao               
  J. T. C. Chiu            
  Lok Wilson