An Assortment of Unix Commands

Open an xterm window for this lesson.

For each of the commands, try typing them yourself in the demonstration window. Remember that you must click with the mouse to direct keyboard input to that window.

date

Reports the time and date.

cal

Prints a calendar of the current month.

      cal 2 2000
prints a calendar for February, 2000, a leap year. Do not abbreviate the year, as in "92", since it will give you the year 92 AD, instead of the year 1992 AD.

who

Lists who is currently logged on to the computer.

clear

Clears the window.

finger <name>

Replace "<name>" with the name of any user. It can be a user name, first name, or last name. Prints information about that user, or user(s) in case of more than one match. In particular this is the way to find out a user name. Try

        finger detar

wc <filename>

For this command you substitute any file name for "<filename>". Try

        wc no_use
Counts lines, words, and characters in the file. In Unix words are separated by "white space" characters, meaning spaces, tabs or an end-of-line.

head <filename>

Lists the top few lines of the file. Try

      head .cshrc.

tail <filename

Lists the last few lines of the file. Try

      tail .cshrc

grep <string> <filename>

Prints all lines in the file <filename> that have the string of characters <string> in them. Example:

	grep the no_use
searches the file called "no_use" for all lines containing the string "the". In this case the match is case-sensitive, so it would fail on a line containing only "The". To make it case-insensitive, use
        grep -i the no_use
Many Unix commands take a variety of options, and the standard way to specify options is with a dash "-" followed by one or more characters.

For example the -v option inverts the process. So to exclude lines that contain the string ``the'' and get everything else, do

	grep -v the no_use

sort <filename>

Sorts the lines in a file <filename>, according to the first "word".

        sort no_use
There are many options. Some common ones are:

-n Treat the first "word" on each line as a number and
  sort in numeric order. Thus .12 comes before 0.05
  in an ordinary alphabetic sort, but after in a
  numeric sort.
-r Sort in reverse order.
-nr Sort in reverse numeric order.
-kn Sort on column n with the above attributes.

Example: The command to sort numerically on the fifth value on each line of the file no_use is

        sort -n -k5 no_use