October 4th, 2018

The shell

Interface between the user and the kernel

When a user logs in

  • the system checks the username and password
  • then starts another program called the shell
    • a command line interpreter (CLI).
  • it shows a prompt $
  • It interprets the commands the user types and executes them
  • The commands are programs
  • When they finish, the shell gives the user another prompt $

Filename Completion

Tricks of the shell

By typing part of the name of a command, filename or directory and pressing the [Tab] key, the shell will complete the rest of the name automatically.

If the shell finds more than one name beginning with those letters you have typed, it will beep, asking you to type a few more letters before pressing the tab key again.

History

More tricks of the shell

The shell keeps a list of the commands you have typed in

If you need to repeat a command,

  • use the cursor keys to move up and down in history
  • or type history for a list of previous commands

The Directory Structure

All the files are grouped together in the directory structure

  • The file-system is arranged in a hierarchical structure
    • like an inverted tree
  • The top of the hierarchy is called root
    • written as /

Commands can have options

ls is an example of a command which can take options: -a is an example of an option.

The options change the behaviour of the command.

There are online manual pages that tell you which options a particular command can take, and how each option modifies the behaviour of the command

How to know the options?

In the old times, people looked it in the “Manual”

Later people realized that you can have the manual in digital form, inside the computer

To see the manual page of ls, use the command man ls

Making Directories

mkdir (make directory)

We will now make a subdirectory in your home directory to hold the files you will be creating and using in the course of this tutorial. To make a subdirectory called unixstuff in your current working directory type

$ mkdir unixstuff

To see the directory you have just created, type

$ ls

Changing to a different directory 

cd (change directory)

The command cd directory means change the current working directory to ‘directory’. The current working directory may be thought of as the directory you are in, i.e. your current position in the file-system tree.

To change to the directory you have just made, type

$ cd unixstuff

The directories . and ..

Still in the unixstuff directory, type

$ ls -a

As you can see, in the unixstuff directory (and in all other directories), there are two special directories called . and ..

The current directory .

In UNIX, . means the current directory, so typing

$ cd .

means stay where you are (the unixstuff directory).

NOTE: there is a space between cd and the dot

This may not seem very useful at first, but using . as the name of the current directory will save a lot of typing

The parent directory ..

.. means the parent of the current directory, so typing

$ cd ..

will take you one directory up the hierarchy (back to your home directory). Try it now.

Note: typing cd with no argument always returns you to your home directory

This is very useful if you are lost

pwd (print working directory)

Pathnames enable you to work out where you are in relation to the whole file-system

To find out the absolute pathname of your directory, use

$ pwd

The full pathname will look something like this

/home/mbg_student

which means that

mbg_student (your home directory) is in the sub-directory home

which is in the top-level root directory called /

Understanding pathnames

First type cd to get back to your home-directory, then type

$ ls unixstuff

to list the contents of your unixstuff directory.

Relative pathnames

Now type

$ ls backups

You will get a message like this -

backups: No such file or directory

The reason is, backups is not in your current working directory.

Relative pathnames

To use a command on a file not in the current working directory, you must specify its full pathname

To list the contents of your backups directory, you must type

$ ls unixstuff/backups

~ (your home directory)

Home directories can also be referred to by the tilde ~ character

It can be used to specify paths starting at your home directory. So typing

$ ls ~/unixstuff

will list the contents of your unixstuff directory, no matter where you currently are in the file system.

Exercise

What do you think

$ ls ~

would list?

What do you think

$ ls ~/..

would list?

Summary

Command Meaning
ls list files and directories
ls -a list all files and directories
mkdir make a directory
cd directory change to named directory
cd change to home-directory
cd ~ change to home-directory
cd .. change to parent directory
pwd display the path of the current directory

Copying Files

cp (copy)

cp file1 file2 is the command which makes a copy of file1 in the current working directory and calls it file2

What we are going to do now, is to take a file stored in an open access area of the file system, and use the cp command to copy it to your unixstuff directory.

First, download the file science.txt into Downloads. Use 'File/Save As..' from the menu bar to save it.

cp (copy)

Change your current folder to your unixstuff directory.

$ cd ~/unixstuff

Then at the UNIX prompt, type,

$ cp ~/Downloads/science.txt .

Note: Don’t forget the dot . at the end. Remember, in UNIX, the dot means the current directory.

The above command means copy the file science.txt to the current directory, keeping the name the same.

Exercise

Create a backup of your science.txt file by copying it to a file called science.bak

Moving files

mv (move)

mv file1 file2 moves (or renames) file1 to file2

To move a file from one place to another, use the mv command. This has the effect of moving rather than copying the file, so you end up with only one file rather than two.

It can also be used to rename a file, by moving the file to the same directory, but giving it a different name.

We are now going to move the file science.bak to your backup directory.

mv (move)

First, change directories to your unixstuff directory. Then, inside the unixstuff directory, type

$ mv science.bak backups/.

Type ls and ls backups to see if it has worked.

Summary

Command Meaning
cp file1 file2 copy file1 and call it file2
mv file1 file2 move or rename file1 to file2

References