October 3rd, 2019

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 $

Tricks of the shell

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

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 class04 in your current working directory type

$ mkdir class04

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 class04

The directories . and ..

Still in the class04 directory, type

$ ls -a

As you can see, in the class04 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 class04 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 class04

to list the contents of your class04 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 class04/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 ~/class04

will list the contents of your class04 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
ls directory list content of directory
mkdir directory 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 class04 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 class04 directory.

$ cd ~/class04

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 class04 directory. Then, inside the class04 directory, type

$ mv science.bak backups/.

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

Removing files and directories

rm (remove file)

To delete (remove) a file, use the rm command. As an example, we are going to create a copy of the science.txt file then delete it.

Inside your class04 directory, type

$ cp science.txt tempfile.txt
$ ls
$ rm tempfile.txt
$ ls

rmdir (remove directory)

You can use the rmdir command to remove a directory

(make sure it is empty first)

Try to remove the backups directory

You will not be able to since UNIX will not let you remove a non-empty directory

Exercise

Create a directory called tempstuff using mkdir , then remove it using the rmdir command.

Summary

Command Meaning
cp file1 file2 copy file1 and call it file2
mv file1 file2 move or rename file1 to file2
rm file remove a file
rmdir directory remove a directory

Original material