Unix: Directories and paths

by Gisle Hannemyr

This chapter introduces the basic Unix commands to create and list directories, and navigate between them. It also teaches you what a path is in the Unix file system.

Table of contents

Listing files and directories (ls)

When you log in, in on a Unix system, you are automatically placed “in” your home directory. Your home directory has the same name as your Unix user-name. The home directory is where you will have write permission so you can save personal files and create sub-directories.

To find out what is already in your home directory, type:

$ ls

Unix let you navigate the directory structure (and further down you will learn how). The directory a particular terminal place you “in” at any given time is called that terminals working directory.

The command ls lists the contents of your current working directory. The screen shot below show the result of doing this on a newly created account on a Unix system I happen to administrate:

xterm ls

There may be no files visible in your home directory, in which case the list will be empty. Alternatively, there may already be some files and directories that was set up automatically by the system when the account was created.

The command ls does not necessarily list all the files in the directory. It only list those whose name does not begin with a dot (.) Files beginning with a dot are known as hidden files and usually contain various program configuration information. They are hidden because you should not change them unless you know what you are doing.

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

$ ls -a

As you can see from the screen shot below, ls -a lists files that are normally hidden.

xterm ls -a

The command ls is an example of a command that can take options: In Unix, options are usually written prefixed with at least one hyphen (e.g. -a). Options modify the behaviour of the command. The online manual pages introduced in the previous chapter will tell you which options a particular command can take, and how each option modifies the behaviour of the command.

Making directories (mkdir)

You should now make a sub-directory in your home directory to hold the files and directories you will manipulate as perform the exercises I suggest you try to enhance your learning experience.

To make a sub-directory called exercises in your current working directory type:

$ mkdir exercises 

To see the new directory listed on the terminal, type:

$ ls

The newly created directory exercises should now be listed.

Changing to a different directory (cd)

The command cd directory is used when you want to change the working directory. If it can be found, directory becomes the new working directory. The working directory may be thought of as the directory you are “in”, i.e. your current position in the file-system tree.

To change the working directory to be the directory you have just created, type:

$ cd exercises 

Use the ls command to list the contents (which should be empty).

tipTyping cd with no argument will always take you to your home directory. This command can be very handy if you get lost and no longer know where you are in the file-system.
 

Exercise

Make another directory inside the exercises directory. Call it: repository

Path-names and the working directory (pwd)

Path-names enable you to work out where you are in relation to the root of the file system. The full path from the root to a particular location in the file system is known as the absolute path.

To find out the absolute path of your home directory, type cd to get back to your home-directory and then type pwd. Below is an example of how the result of this sequence of a command for a user named “john”:

$ cd
$ pwd
/home/john

This means that the home directory of the user “john” is also named john, and it is located on a directory named home, which is in the top-level root directory.

The root directory is just written as a slash (/).

file system tree

In the diagram above, notice that that the home directory of the user named “john” (bright yellow) contains three sub-directories (bin, play and work). In the sub-directory work there a file named “report.odt”.

The absolute path to the file report.odt (yellow) is /home/john/work/report.odt.

tipThe above file system layout is only an example. The file structure may be different on the machine you're using while reading this ebook. There are no absolute rules about how to lay out the file structure on a Unix system. A Unix system, in particular on a large Unix server that is shared by many users, may use a file structure very different from the example above.

Exercise

Make use of the three commands cd, ls and pwd to explore the file system on your machine. Draw a diagram similar to the one above and colour the path from root to your home directory yellow. You do not have to list every file and directory. Aim for a complexity similar to the sample diagram above.

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
pwd display the path of the current directory