Chapter 6 Lab - Working With Files and Directories
Chapter 6 Lab - Working With Files and Directories
6.1 Introduction
This is Lab 6: Listing Files and Directories. By performing this lab, students will learn how to
navigate and manage files and directories.
In this task you will explore the concepts of files and directories.
On a Linux OS, data is stored in files and files are stored in directories. You may be used to the
term folders to describe directories.
Directories are actually files, too; the data that they hold are the names of the files that have been
entered into the them, along with the inode number (a unique identifier number assigned to each
file) for where the data for that file exists on the disk.
As a Linux user, you will want to know how to manipulate these files and directories, including
how to list files in a directory, copy, delete and move files.
6.2.1 Step 1
The working directory is the directory that your terminal window is currently "in". This is also
called the current directory. This will be important for when you are running future commands as
they will behave differently based on the directory you are currently in.
The output of the pwd command (/home/sysadmin in the example above) is called the path. The
first slash represents the root directory, the top level of the directory structure.
In the output above, home is a directory under the root directory and sysadmin is a directory under
the home directory.
When you first open a terminal window, you will be placed in your home directory. This is a
directory where you have full access and other users normally have no access by default. To see the
path to your home directory, you can execute the following command to view the value of the HOME
variable:
6.2.2 Step 2
You can use the cd command with a path to a directory to change your current directory. Type the
following command to make the root directory your current working directory and verify with the
pwd command:
6.2.3 Step 3
To change back to your home directory, the cd command can be executed without a path. Change
back to your home directory and verify by typing the following commands:
Notice the change in the prompt. The tilde ~ character represents your home directory. This part of the
prompt will tell you what directory you are currently in.
6.2.4 Step 4
The cd command may be entered with a path to a directory specified as an argument. Execute the
cd command with the /home directory as an argument by typing the following:
When the path that is provided as an argument to the cd command starts with the forward slash /,
that path is referred to as an “absolute path”. Absolute paths are always complete paths from the
root directory to a sub-directory or file.
6.2.5 Step 5
Change back to your home directory, using the cd command with the tilde ~ as an argument:
When the path that is provided as an argument to cd command starts with a tilde ~ character, the
terminal will expand the character to the home directory of a user with an account on the system.
If either no other characters or a forward slash follows the tilde, then it will expand to the home
directory of the user currently active in the shell.
If a user name immediately follows the tilde character, then the shell will expand the tilde and user
name to the home directory of that user name. For example, ~bob would be expanded to
/home/bob.
Paths that start with a tilde are considered absolute paths because after the shell expands the tilde
path, an absolute path is formed.
6.2.6 Step 6
Use the echo command below to display some other examples of using the tilde as part of path:
6.2.7 Step 7
Attempt to change to the home directory of the root user by typing the following command:
Notice the error message; it indicates that the shell attempted to execute cd with /root as an argument
and it failed due to permission being denied. You will learn more about file and directory permissions in a
later lab.
6.2.8 Step 8
Using an absolute path, change to the /usr/bin directory and display the working directory by
using the following commands:
6.2.9 Step 9
Use an absolute path to change the /usr directory and display the working directory by issuing the
following commands:
6.2.10 Step 10
Use an absolute path the change to the /usr/share/doc directory and display the working
directory by issuing the following commands:
Absolute vs. Relative pathnames
Suppose you are in the /usr/share/doc directory and you want to go to the
/usr/share/doc/bash directory. Typing the command cd /usr/share/doc/bash results in a fair
amount of typing. In cases like this, you want to use relative pathnames.
With relative pathnames you provide "directions" of where you want to go from the current
directory. The following examples will illustrate using relative pathnames.
6.2.11 Step 11
Using a relative path, change to the /usr/share/doc/bash directory and display the working
directory by issuing the following commands:
6.2.12 Step 12
Use a relative path to change to the directory above the current directory:
6.2.13 Step 13
Use a relative path to change up one level from the current directory and then down into the dict
directory:
6.3 Listing Files and Directories
In this task, you will explore the how to list files and directories.
6.3.1 Step 1
In the output of the previous ls command the file names were placed in a light blue color. This is a
feature that many distributions of Linux automatically provide through a feature called an alias
(more on this feature in a later lab).
The color indicates what type the item is. The following table describes some of the more common
colors:
Color Type of File
Black or Regular file
White
Blue Directory file
Cyan Symbolic link file (a file that points to another file)
Green Executable file (AKA, a program)
6.3.2 Step 2
Not all files are displayed by default. There are files, called hidden files, that are not displayed by
default. To display all files, including hidden files, use the -a option to the ls command:
Hidden files begin with a period (a dot character). Typically these files and often directories are
hidden because they are not files you normally want to see.
For example, the .bashrc file shown in the example above contains configuration information for
the bash shell. This is a file that you normally don't need to view on a regular basis.
Two important "dot files" exist in every directory: . (which represents the current directory) and ..
(which represents the directory above the current directory).
6.3.3 Step 3
By itself, the ls command just provided the names of the files and directories within the specified
(or current) directory. Execute the following command to see how the -l option provides more
information about a file:
So, what does all of this extra output mean? The following table provides a brief breakdown of what each
part of the output of ls -l means:
- The first character, a - in the previous example, indicates what type of "file" this is. A -
character is for plain file while a d character would be for a directory.
rw-r-- This represents the permissions of the file. Permissions are discussed in a later lab.
r--
1 This represents something called a hard link count (discussed later).
root The user owner of the file.
root The group owner of the file.
150 The size of the file in bytes
Jan 22 The date/time when the file was last modified.
15:18
6.3.4 Step 4
Sometimes you want to see not only the contents of a directory, but also the contents of the
subdirectories. You can use the -R option to accomplish this:
The -R option stands for "recursive". All of the files in the /etc/udev directory will be displayed as
well as all of the files in each subdirectory, in this case the rules.d subdirectory.
6.3.5 Step 5
You can use file globbing (wildcards) to limit which files or directories you see. For example, the *
character can match "zero or more of any characters" in a filename. Execute the following
command to display only the files that begin with the letter s in the /etc directory:
Note that the -d option prevents files from subdirectories from being displayed. It should always be used
with the ls command when you are using file globbing.
6.3.6 Step 6
The ? character can be used to match exactly 1 character in a file name. Execute the following
command to display all of the files in the /etc directory that are exactly four characters long:
By using square brackets [ ] you can specify a single character to match from a set of characters.
Execute the following command to display all of the files in the /etc directory that begin with the
letters a, b, c or d:
In this task, you will copy, move, and remove files and directories.
6.4.1 Step 1
Make a copy of the /etc/hosts file and place it in the current directory. Then list the contents of
the current directory before and after the copy:
Notice how the second ls command displays a copy of the hosts file.
6.4.2 Step 2
Next you will remove the file, then copy it again, but have the system tell you what is being done.
This can be achieved using the -v or --verbose option. Enter the following commands:
Note that the rm command is used to delete a file. More information on this command will be
provided later in this lab.
Note that the -v switch displays the source and target when the cp command is executed.
6.4.3 Step 3
Enter the following commands to copy the /etc/hosts file, using the period . character to indicate
the current directory as the target:
6.4.4 Step 4
Enter the following commands to copy from the source directory and preserve file attributes by
using the -p option:
Notice that the date and permission modes were preserved. Note that the timestamp in the output
above is the same for both the original and the copy (Jan 22 15:18) in the example provided
above. Your output may vary.
6.4.5 Step 5
The second copy specified a different filename (newname) as the target. Because it was issued
without the -p option, the system used the current date and time for the target, thus, it did not
preserve the original timestamp found in the source file /etc/hosts.
Finally, note that you can remove more than one file at a time as shown in the last rm command.
6.4.6 Step 6
To copy all files in a directory use the -R option. For this task, you will copy the /etc/udev
directory and display the contents of the copied directory:
6.4.7 Step 7
Note that the rmdir command can also be used to delete directories, but only if the directory is
empty (if it contains no files).
Also note the -r option. This option removes directories and their contents recursively.
6.4.8 Step 8
Moving a file is analogous to a "cut and paste". The file is “cut” (removed) from the original
location and “pasted” to the specified destination. Move a file in the local directory by executing the
following commands: