LINUX Tutorial One

1.1 Listing files and directories

ls (list)

When you first login to a linux terminal, your current working directory is usually your home directory. However, in our case we log into the docker container as root (the admin user), and start at the the topmost directory, denoted as /).

To find out what is in this directory, type

# ls

The ls command lists the contents of your current working directory (ls is short for list).

There may be no files visible in the current directory, in which case the LINUX prompt will be returned. Alternatively, there may already be some files in this directory (the topmost root directory will always contain system-wide files and directories.

ls does not, in fact, cause all the files in the current directory to be listed, but only those whose names do not begin with a dot (.) Files beginning with a dot are known as hidden files and usually contain important program configuration information. They are hidden because you should not change them unless you are very familiar with LINUX!!!

To list all files in your home directory including those whose names begin with a dot, type

# ls -a

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. (Online manual pages are described more in Tutorial Four)

1.2 Making Directories

mkdir (make directory)

We will now make a subdirectory in the current directory. To make a subdirectory called linuxstuff in your current working directory type

# mkdir linuxstuff

To see the directory you have just created, type

# ls

1.3 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 linuxstuff

Type ls to see the contents (which should be empty)

Exercise 1a

Make another directory inside the linuxstuff directory called backups

1.4 The directories . and ..

Still in the linuxstuff directory, type

# ls -a

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

In LINUX, (.) means the current directory, so typing

# cd .

means stay where you are (i.e., in the linuxstuff directory).

This may not seem very useful at first, but using (.) as the name of the current directory will save a lot of typing, as we shall see later in the tutorial.

The parent of the current directory is (..), so typing

# cd ..

will take you one directory up the hierarchy (i.e., the parent directory of the current one). Try it now.

Note: typing cd with no argument always returns you to your home directory (/root in our case). Typing cd / will return you to the topmost directory (for us, this is very useful as it will return us to the original directory we start in when we run a shell in a new container).

1.5 Pathnames

pwd (print working directory)

Pathnames enable you to work out where you are in relation to the whole file-system. For example, to find out the absolute pathname of your home-directory, type cd to get back to your home-directory and then type

# pwd

The full pathname should read -

/root

Exercise 1b

Use the commands ls, pwd and cd to explore the file system.

(Remember, if you get lost, type cd / to return to the directory we started in)

1.6 More about home directories and pathnames

Understanding pathnames

First type cd / to get back to the topmost directory, then type

# ls linuxstuff

to list the conents of your linuxstuff directory.

Now type

# ls backups

You will get a message like this -

ls: cannot access backups: No such file or directory

The reason is, backups is not in your current working directory. To use a command on a file (or directory) not in the current working directory (the directory you are currently in), you must either cd to the correct directory, or specify its full pathname. To list the contents of your backups directory, you must type

# ls linuxstuff/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 ~

will list the contents of your home directory (/root), no matter where you currently are in the file system.

What do you think

# ls ~/..

would list?

Summary

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

Last Modified: January 2019