A very brief Unix primer

This is a brief summary of some Unix concepts that will be important for this course.


Manual pages

Manual pages are one of the best resources to learn about Unix commands.

   man ls      displays the manual page for the ls command
   man man     displays the manual page for the man command
   man -k directory     displays a list of manual pages about "directory"
It is important to understand how to read the "synopsis" section of a man page.
   [ ]   are used to indicate optional agruments to the command

Paths and Filenames

Paths in Unix use forward-slashes (/) to separate directories:
   /export/home/r/rcapra/unc/inls760/lectures

There are several special notations you can use in paths:

   ~      is a shortcut for your home directory
   .      is the current working directory
   ..     is the parent directory of the current directory
   /      is the "root" level directory, the top of all directories
Examples:
   on ruby, these two paths refer to the same directory:
       ~rcapra/unc/inls760
       /export/home/r/rcapra/unc/inls760

   given the following directory structure:

                  inls760
                /    |    \
           lectures  p0    p1

   with present/current working directory = inls760
   the following are (slightly) convoluted examples illustrating paths:

       lectures/./../p0/..       refers to inls760
       p0/../p1/././../lectures  refers to lectures
       p0/../p1/././../p0/p1     is not a valid directory

Wildcards

Wildcard characters can be used to match multiple files. The asterisk character (*) will match any number of characters in a filename or directory name. Wildcards should be used with caution if performing potentially destructive commands.

Example:

   [rcapra@ruby lectures]$ ls
   lect1-bkup.ppt  lect1.pdf  lect1.ppt  lect1.ps  lect2.ppt
   [rcapra@ruby lectures]$ ls *.ppt
   lect1-bkup.ppt  lect1.ppt  lect2.ppt
   [rcapra@ruby lectures]$ ls lect1.*
   lect1.pdf  lect1.ppt  lect1.ps
   [rcapra@ruby lectures]$ ls *
   lect1-bkup.ppt  lect1.pdf  lect1.ppt  lect1.ps  lect2.ppt
   [rcapra@ruby lectures]$ 

Basic Unix commands

ls

Display the contents of a directory.

Short listing:

   [rcapra@ruby lectures]$ ls
   lect1-bkup.ppt  lect1.pdf  lect1.ppt  lect1.ps  lect2.ppt
Long listing, all files:
   [rcapra@ruby lectures]$ ls -al
   total 656
   drwx------    2 rcapra   users        4096 Jan 18 22:31 .
   drwx------    7 rcapra   users        4096 Jan 22 21:52 ..
   -rw-r--r--    1 rcapra   users      112128 Jan 16 17:24 lect1-bkup.ppt
   -rw-------    1 rcapra   users       58914 Jan 17 00:24 lect1.pdf
   -rw-r--r--    1 rcapra   users      112128 Jan 16 17:24 lect1.ppt
   -rw-r--r--    1 rcapra   users      238437 Jan 17 00:19 lect1.ps
   -rw-r--r--    1 rcapra   users      112128 Jan 16 17:23 lect2.ppt

cat

Concatenate files.

The cat command is often used to output the contents of a file to STDOUT (for more information on STDOUT, see below).

   [rcapra@ruby lectures]$ cat foo
   This is a test.
   This file has two lines.
   [rcapra@ruby lectures]$ 

less

Display the contents of a file page-by-page.

If you want to see the contents of a long file, you can use the "less" command to view it using the "less" pager.

When in less, you can use the space bar to page down, and the up arrow key to go up in the file.

When you are done viewing the file, press q.

   [rcapra@ruby lectures]$ less foo

cd

Change directory.

Example using the following directory structure:

                  inls760
                /    |    \
           lectures  p0    p1


   [rcapra@ruby lectures]$ cd ..
   [rcapra@ruby inls760]$ cd p0
   [rcapra@ruby p0]$ cd ../lectures
   [rcapra@ruby lectures]$ 

pwd

Diplay the full path of the present working directory.
   [rcapra@ruby lectures]$ pwd
   /export/home/r/rcapra/unc/inls760/lectures

cp

Copy file.

   [rcapra@ruby lectures]$ ls
   lect1-bkup.ppt  lect1.pdf  lect1.ppt  lect1.ps  lect2.ppt
   [rcapra@ruby lectures]$ cp lect1.ps foo
   [rcapra@ruby lectures]$ ls
   foo  lect1-bkup.ppt  lect1.pdf  lect1.ppt  lect1.ps  lect2.ppt

mv

Move file.

   [rcapra@ruby lectures]$ ls
   foo  lect1-bkup.ppt  lect1.pdf  lect1.ppt  lect1.ps  lect2.ppt
   [rcapra@ruby lectures]$ mv foo lect1-bkup.ps
   [rcapra@ruby lectures]$ ls
   lect1-bkup.ppt  lect1-bkup.ps  lect1.pdf  lect1.ppt  lect1.ps  lect2.ppt
   [rcapra@ruby lectures]$ mv lect1-bkup.ps ..
   [rcapra@ruby lectures]$ cd ..
   [rcapra@ruby inls760]$ ls
   lect1-bkup.ps  lectures  p0  p1

More commands in brief

rm -- removes a file
   rm foo.txt           removes (deletes) the file foo.txt

cat -- concatenate files, can also display contents of a file
   cat foo.txt         displays the contents of file foo.txt

mkdir -- make directory
   mkdir foo           creates a new subdirectory foo

rmdir -- removes an (empty) directory
   rmdir foo           removes the subdirectory foo (must be empty)

File permissions

Every file and directory in Unix has an associated set of permissions.

It is very important to understand these permissions and how to set them, or you may open your system and files up to threats.

Unix permissions involve three sets of users:

There are three main types of permission that can be granted to the three sets of users: When you look at a long format directory listing, you can see the permissions, owner, and group of the files and directories:
   [rcapra@ruby lectures]$ ls -al
   total 656
   drwx------    2 rcapra   users        4096 Jan 18 22:31 .
   drwx------    7 rcapra   users        4096 Jan 22 21:52 ..
   -rw-r--r--    1 rcapra   users      112128 Jan 16 17:24 lect1-bkup.ppt
   -rw-------    1 rcapra   users       58914 Jan 17 00:24 lect1.pdf
   -rw-r--r--    1 rcapra   users      112128 Jan 16 17:24 lect1.ppt
   -rw-r--r--    1 rcapra   users      238437 Jan 17 00:19 lect1.ps
   -rw-r--r--    1 rcapra   users      112128 Jan 16 17:23 lect2.ppt
For each item in the listing, the permissions are shown first, like this:
                 drwxrwxrwx
Split into sections, they are:
            d     rwx     rwx     rwx
            |      |       |       |
       directory  user   group   other
If a letter appears in the listing, then that permission is granted. For example, consider the file lect1.ps:
   -rw-r--r--    1 rcapra   users      238437 Jan 17 00:19 lect1.ps
This line indicates that: Note: reference for read, write, and execute below: An Introduction to Unix Permissions

read

For files, read permission means that you can view the contents of the file and that you can copy it.

For directories, read permission means that you can do an "ls" in the directory.

write

For files, write permission means that you can modify a file and save the changes.

For directories, write permission means that you can store files that you own in that directory and that you can change the contents of the directory (e.g. you can mv files in it).

execute

For files, execute permission means that you can try to "execute", or run, the file as a program or command.

For directories, execute permission means that you can use the directory in a path.

Using chmod to change permissions

The chmod command in Unix is used to set or change permissions for files and directories.

   [rcapra@ruby lectures]$ ls -l lect1.ps
   -rw-r--r--    1 rcapra   users      238437 Jan 17 00:19 lect1.ps
   [rcapra@ruby lectures]$ chmod go-r lect1.ps
   [rcapra@ruby lectures]$ ls -l lect1.ps
   -rw-------    1 rcapra   users      238437 Jan 17 00:19 lect1.ps
   [rcapra@ruby lectures]$ chmod g+w lect1.ps
   [rcapra@ruby lectures]$ ls -l lect1.ps
   -rw--w----    1 rcapra   users      238437 Jan 17 00:19 lect1.ps
   [rcapra@ruby lectures]$ ls -al
   total 656
   drwx------    2 rcapra   users        4096 Jan 23 12:41 .
   drwx------    7 rcapra   users        4096 Jan 22 22:29 ..
   -rw-r--r--    1 rcapra   users      112128 Jan 16 17:24 lect1-bkup.ppt
   -rw-------    1 rcapra   users       58914 Jan 17 00:24 lect1.pdf
   -rw-r--r--    1 rcapra   users      112128 Jan 16 17:24 lect1.ppt
   -rw--w----    1 rcapra   users      238437 Jan 17 00:19 lect1.ps
   -rw-r--r--    1 rcapra   users      112128 Jan 16 17:23 lect2.ppt
   [rcapra@ruby lectures]$ chmod go-rwx *
   [rcapra@ruby lectures]$ ls -al
   total 656
   drwx------    2 rcapra   users        4096 Jan 23 12:41 .
   drwx------    7 rcapra   users        4096 Jan 22 22:29 ..
   -rw-------    1 rcapra   users      112128 Jan 16 17:24 lect1-bkup.ppt
   -rw-------    1 rcapra   users       58914 Jan 17 00:24 lect1.pdf
   -rw-------    1 rcapra   users      112128 Jan 16 17:24 lect1.ppt
   -rw-------    1 rcapra   users      238437 Jan 17 00:19 lect1.ps
   -rw-------    1 rcapra   users      112128 Jan 16 17:23 lect2.ppt

Redirection and Pipes

Many Unix commands operate on input from STDIN and write their output to STDOUT. STDIN and STDOUT are "standard input" and "standard output" steams that are typically associated with keyboard input and terminal window output by default. As such, you are often using stdin and stdout when you issue Unix commands, although you may not have realized it.

As an example, try the following:

  1. At the Unix prompt, type "cat" and press return
  2. You should now be at the start of next line
  3. Type "this is a test" and press return
  4. The text should have been printed again on the next line
  5. Type CONTROL-D
  6. You should be back at the command prompt
You just used the cat command to receive input on stdin, which is by default the keyboard, and send the output to stdout, which is by default the screen. Control-D is the end-of-file character, which let cat know to exit and return you to the command prompt.

Redirection

Unix provides redirection operators that allow you to redirect, or change, where stdin comes from and where stdout goes to. Consider the following examples that use the Unix commands sort and uniq. sort will sort lines of a file. uniq will remove duplicate lines in a sorted file. Examples:
   [rcapra@ruby lectures]$ cat foo
   charlie
   abel
   baker
   charlie
   abel
   [rcapra@ruby lectures]$ sort foo
   abel
   abel
   baker
   charlie
   charlie
   [rcapra@ruby lectures]$ sort foo > bar
   [rcapra@ruby lectures]$ cat bar
   abel
   abel
   baker
   charlie
   charlie
   [rcapra@ruby lectures]$ sort < foo
   abel
   abel
   baker
   charlie
   charlie
   [rcapra@ruby lectures]$ sort < foo | uniq > bar
   [rcapra@ruby lectures]$ cat bar
   abel
   baker
   charlie
   [rcapra@ruby lectures]$