Lab No. 2

After completing this lab, students will be able to:

  • Inspect and navigate the linux filesystem
  • Create, remove and alter files and directories

The File System

In Unix, files are organized in a hierarchical structure that resembles a tree, typically represented upside down (in the same fashion as a family tree):


You can see that in this structure, all the elements are “connected” by a path.

A directory is a “branch” within the file system tree. As such it can contain other branches (subdirectories) or leaves (files).

The top directory in a Linux file system is known as the root directory, and it is represented by /. Notice how the root directory contains other directories and files and how some of these directories contain either directories or files.


If you have not logged into you need do so in order to start the Lab exercises.

File system navigation

There are several commands that help you to navigate the file system. Unix systems have the concept of the working directory (also referred as current directory), which determines the directory within the tree that your session is currently associated with. The pwd command prints your current directory.

pwd: print current/working directory

  1. Execute the pwd command. What is the output of this command?

When you login into Unix systems, your current directory is set to your home directory by default. Home directories for most users are typically located in /home (However, on Mac OS, home directories are located in /Users.)

The ls command list the contents of directories. Without any arguments, it lists the contents of the current directory. You can also list the contents of any other directory if you provide it as an argument (and if you have read access to that directory):

ls: list directory contents

  1. Execute the ls command. What is the output of this command? If the output is too long, ask the lab instructor for help.
  2. Execute the command touch fileone. Execute the command ls -l. Notice how the -l option causes ls to display the output in long format (several fields about each file are presented in the form of columns). Who is the owner of the file fileone? (hint: The textbook has an explanation of the meaning of the columns in page 16). Attach the output of the ls -l command to your report.
  3. Execute the ls -la command. Compare with the output of the previous command. What is the effect of adding the -a option?
  4. Execute the ls -l /usr/bin. This will produce a long list of files contained in the /usr/bin directory. Execute ls -lt /usr/bin. What is the effect of adding the -t option? Execute ls -ltr /usr/bin. What is the purpose of the -r option?
  5. What is the oldest file in the /usr/bin directory?
  6. What is the newest file in the /tmp/planets directory?

A pathname is the trail through the directory hierarchy to a file or directory. A pathname can be absolute when it traces a path from the root directory (/), or it can be relative when it traces a path from the working directory. Relative pathnames utilize a special notation: . which refers to the working directory, and .. which refers to a parent directory. There tilde character (~) is a special character that the shell can use to expand to home user directories. The - character is another handy special character that you can use to “jump back” to the previous working directory.

You can change your current directory with the cd command, which takes as an argument the pathname of the desired directory. If no argument is provided, then cd will change the working directory to the user’s home directory.

cd: change current directory

  1. Execute the command cd /var/log. What is your current working directory?
  2. There is a directory named mail inside the /var/log directory. What is the absolute path to that directory? Given your working directory, what is the relative path to that directory? What is the relative path to the /var directory? Make sure you test your answers by running the cd command.
  3. Execute cd ~. What is your working directory?
  4. Execute cd ~jmora. What is your working directory?
  5. Execute cd -. How does the - charater modify the behavior of the cd command?
  6. Absolute and relative pathnames can be mixed. Consider the following path:/usr/include/curl/./././///../boost/random/../math. Simplify that path so it does not use relative references (hint: try it with the cd command).
  7. The following diagram corresponds to a portion of the filesystem in blue. Fill out the empty elements. To find the correct answers, you will need to navigate through the file system using the cd command and use the ls directory to list files.

Creating directories and files

The mkdir utility creates new directories. It requires as arguments the pathnames of the directories to be created. The following example shows the commands required to generate the directory structure shown below:

[jmora@blue log]$ cd
[jmora@blue ~]$ ls
lab02  public_html
[jmora@blue ~]$ mkdir vehicles
[jmora@blue ~]$ ls
lab02  public_html  vehicles
[jmora@blue ~]$ mkdir -p vehicles/cars/sedan
[jmora@blue ~]$ ls vehicles/cars
[jmora@blue ~]$ mkdir -p vehicles/cars/pickup vehicles/cars/suv
[jmora@blue ~]$ ls vehicles/cars
pickup  sedan  suv

mkdir: make directories

  1. What is the purpose of the -p option? Suppose you just created the directory structure from the previous image, and that your working directory is your home directory. Will the command mkdir vehicles/planes/jet succeed?
  2. Provide a series of mkdir commands to complete the following structure (remember that vehicles is a subdirectory of your home directory):
├── bicycles
│   └── mountain
├── cars
│   ├── pickup
│   ├── sedan
│   │   └── compact
│   └── suv
└── jet
    ├── cargo
    └── fighter

To remove directories you can use the rmdir command. Just as mkdir, it requires the pathnames of the directories to be removed. However, rmdir has the limitation that it only works with empty directories. To remove directories that are not emptu, you can use the rm -r command.

rmdir and rm -r: remove directories

  1. Provide a list of commands to remove the vehicles/cars/sedan directory.

Moving and copying files

The mv command can be used for two purposes: rename a file/directory, or move the file/directory to another path.

mv: move or rename files

  1. Make your home the working directory. Run the following command: tar -xf /tmp/vehicles.tar. This will create three files with names of vehicles in your working directory.
  2. Provide a list of commands that will result in moving the files to the corresponding directory within the vehicles directory.
  3. The three files should contain the Wikipedia link that corresponds to the name of the file. However, the file named prius is incorrect. Provide a single command that will make the file name reflect the content of the file (e.g. the filename should be fseries) and will place the file under the correct directory (~/vehicles/cars/pickup)

The cp command is used to copy files or directories. In the following example, a file is created by redirecting the output of the echo command into a file called original. The cat command reads that file and prints that on the screen, this is done as a verification step. original is then copied into a file called clone. Notice how the contents of clone are the same as the contents of original.

[jmora@blue ~]$ echo "This text is the content" > original
[jmora@blue ~]$ ls
lab02  original  public_html
[jmora@blue ~]$ cat original
This text is the content
[jmora@blue ~]$ cp original clone
[jmora@blue ~]$ ls
clone  lab02  original  public_html
[jmora@blue ~]$ cat clone
This text is the content

In order to copy directories, you need to provide the -r option to the cp command. This option stands for recursive, and basically it means that it will traverse the whole tree under the directory that you want to copy.

cp: copy files or directories

  1. What command would you use to copy the file /tmp/airbus into the appropriate subdirectory within vehicles?
  2. We want to make a backup of the vehicles directory into a directory called vehicles_backup. What command will serve this purpose?
  3. Using a text editor, create two files in your home directory named file_1 and file_2. Make sure that the content of these files is not the same. Execute the command cp -n file_1 file_2 and after this inspect the contents of the two files. Execute the command again but this time use the -i option. Based on your observations (you can always look at the man pages, of course), what is the purpose of these two flags?