CS 114 - Fall 2004

Lecture 1 - Monday, September 27, 2004

Introduction to Unix

When you log in to a Unix machine (either through ssh, telnet, or by walking up the machine), you will be presented with a login prompt that looks like:

login:

The system is asking you for your username, the identifier by which you are known to the system. Be default on turing, your username is just your Cornell NetID. After entering your username, the system then asks you for your password:

login: username
Password:

When you type in your password, the console will not echo what you type back to you. This is to prevent others from learning your password by peering over your shoulder. If you enter your username and password successfully, you end up at the Unix prompt, which on turing looks like:

turing%

The prompt is where you interact with Unix. Typically, you issue commands, possibly with arguments. Some commands start programs, others manage the environment, and others give you information.

Everything under Unix lives in files: programs, data, etc. These files are distributed amongst different directories. Directories contain files, and may also contain other directories. Files thus form a tree-like hierarchy starting from a base directory called the root directory. This hierarchy is often called the filesystem. The filesystem may be on a local disk, or it may be across the network; we will not worry about exactly where the files are physically stored. Each user on the system gets a special directory, called their home directory, where they can put their files and create subdirectories, etc.

Here's a typical filesystem. Directories are indicated by names with a trailing /:

[typical filesystem]

In English, the root directory / contains 6 subdirectories bin/, home/, lib/, tmp/, usr/, and var/. Of those, home/ and usr/ have content indicated in the graph. If we look at home/, it contains two directories cs114/ and njn2/. The directory cs114/ contains two files, file1 and file2, and njn2/ contains a directory foo/ and a file file4.

Paths

A path is used to give the location of a file or directory one is interested in. There are two types of path:

Relative paths are always relative to a directory called the current working directory, or simply the current directory. When you login, your current directory is your home directory, /home/username/ on turing.

The following sequences of characters have special meaning in a path.

Commands

cd

You can change your current working directory by using the command cd at the prompt:

% cd path

changes the current working directory to path. (I will often indicate commands to be typed at the prompt by prefixing the line with a %; don't type the %.) The path argument can be either an absolute or relative path. Thus,

% cd /

changes to the root directory;

% cd /home/njn2/

changes to the home directory of njn2;

% cd ..

changes to the parent directory of the current working directory.

If you just type

% cd

by itself, you will change back to your home directory.

pwd

You can print your current directory using the command pwd, which stands for print working directory. For example:

% pwd
/home/njn2

indicates that the current directory is /home/njn2/.

ls

The command ls will display the contents of the current directory. Thus, if the current directory is /home/njn2/:

% ls
file3
foo

If you give a directory argument to ls it will list the contents of that directory:

% ls ~cs114
file1 file2


% ls ~cs114 ~njn2
/home/cs114:
file1 file2

/home/njn2:
file3 foo

Filesystem manipulation

So far we can navigate the filesystem and see what's there. Now, how do we manipulate the filesystem. The following commands are also useful for dealing with files and directories. Recall than whenever we talk about a file or a directory, it actually stands for a path to a file or directory.

Some commands have options, which affect the way they behave. Options are typically a letter preceded by a -. For example, if you give the option -i to rm, that is, if you write rm -i file, the system will prompt you for confirmation before deleting the file.

% rm -i file
rm: remove file (yes/no)?

Some commands understand many options. The command ls for instance, recognizes, among others, the option -F, which makes ls give you some indication of the type of each file.

% ls -F file3 dir/

It appends a / at the end of every directory. ls also takes the option -a, which makes ls display everything in the directory, including so-called hidden files. A file or directory is hidden if its name starts with a dot (.), for example .hidden. Hidden files aren't really special in any way, except that ls and other tools won't list them by default. Typically, programs needing configuration files will make those files hidden; otherwise, these files will clutter up your home directory.

man

How do you learn and remember the various options that each command understand? You can look at the online Unix documentation, available through the command man. If you type:

% man command

it will search for and display documentation (the manual) on command. This information is called the man page of the command. Among other things, you will get a description of the arguments the command expects and the options it understands. The command man itself understands different options; do:

% man man

to find out what those options are.