OS2 - Week 8 - Week9
OS2 - Week 8 - Week9
-Week 4 – Lab 1
Part 1: Listing Files and Directories
In these tasks 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.
Now open the terminal application and complete the following tasks
cd
ls
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:
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:
ls –a
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)
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:
ls -l /etc/hosts
So, what does all of this extra output mean? The following table provides a brief
breakdowال8غةخn 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--r-- This represents the permissions of the file. Permissions are discussed in a later lab.
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.
189 The size of the file in bytes
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:
ls -R /etc/udev
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.
Warning: Be careful of the -R option. Some directories are very, very large!
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:
ls -d /etc/s*
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. 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:
ls -d /etc/????
7. 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":
ls –d /etc/[abcd]*
Now open the terminal application and complete the following tasks
8. 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:
ls
cp /etc/hosts hosts
ls
9. 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:
rm hosts
ls
cp –v /etc/hosts hosts
ls
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.
10. Enter the following commands to copy the /etc/hosts file, using the period (.) character to
indicate the current directory as the target:
rm hosts
ls
cp –v /etc/hosts .
ls
The period . character is a handy way to say "the current directory". It can be used with all
Linux commands, not just the cp command.
11. Enter the following commands to copy from the source directory and preserve file attributes by
using the -p option:
rm hosts
ls
cd /etc
ls -l hosts
cp –p hosts /home/sysadmin
cd
ls –l hosts
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 (Dec 12 16:14) in the example
provided above. Your output may vary.
12. Type the following commands to copy using a different target name:
rm hosts
cp -p /etc/hosts ~
cp hosts newname
ls –l hosts newname
rm hosts newname
The first copy with the -p option preserved the original timestamp. Recall that the tilde ~
represents your home directory (/home/sysadmin).
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.
13. 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:
mkdir Myetc
cp –R /etc/udev Myetc
ls –l Myetc
ls –lR Myetc
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.
15. 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:
touch premove
ls
mv premove postmove
ls
rm postmove
Week 4 – Lab 2
Part 1: Finding Files and Directories
In these tasks, we will explore how to search for a file on the system. This is useful to know in
situations when you can't find a file on the system, either one that you created or one that was
created by someone else.
Now open the terminal application and complete the following tasks
1. An easy way to search for a file is to use the locate command. For example, you can find the
location of the crontab file by executing the following command:
locate crontab
2. Note that the output from the previous example includes files that have "crontab*" as part of
their name. To find files that are just named "crontab", use the following command:
locate -b "\crontab
Note: The locate command makes use of a database that is traditionally updated once per day
(normally in the middle of the night). This database contains a list of all files that were on the
system when the database was last updated.
As a result, any files that you created today will not normally be searchable with the locate
command. If you have access to the system as the root user (the system administrator account),
you can manually update this file by running the updatedb command. Regular users can not
update the database file.
Another possible solution to searching for "newer" files is to make use of the find command.
This command searches the live filesystem, rather than a static database. The find command
isn't part of the Linux Essentials objectives for this lab, so it is only mentioned here. Execute
3. You may just want to find where a command (or its man pages) is located. This can be
accomplished with the whereis command :
whereis passwd
The whereis command only searches for commands and man pages, not just any file.
Recall from earlier that there is more than one passwd man page on the system. This is why you
see multiple file names and man pages (the files that end in .gz are man pages) when you
execute the previous command.
4. Search for files beginning in your home directory containing the name “bash.”
5. Find files that were modified (or created) less than 5 minutes ago in the specified directory by
using the following commands:
The Music directory was displayed with the second find command because the directory was
modified, the result of a file being added to the directory.
6. Execute the following command to find files in the /usr directory that are larger than 2MB in
size:
8. To verify that the output displays directories, use the -ls option. The find command uses the -
print option by default which displays just file names. The -ls option provides file details:
Recall that the "d" character before the permissions "rwxr-x-r-x" indicates that the file is
actually a directory.
The use of the more or less commands allows for the user to the view data a "page" or a line at a
time. These "pager" commands also permit other forms of navigation and searching that will be
demonstrated in this section.
Note: examples are given using both the more and less commands. Most of the time, the
commands work the same, however the less command is more advanced and has more features.
The more command is still important to know because some Linux distributions don't have the less
command, but all Linux distributions have the more command.
Now open the terminal application and complete the following tasks
1. The /etc/passwd is likely too large to be displayed on the screen without scrolling the screen.
To see a demonstration of this, use the cat command to display the entire contents of the
/etc/passwd file:
cat /etc/passwd
2. Use the more command to display the entire contents of the /etc/passwd file:
more /etc/passwd
Note: The "--More--(92%)" indicates you are "in" the more command and 92% through the
current data.
3. While you are in the more command, you can view the help screen by pressing the h key:
<SPACE>
In the next example, you will learn how to search a document using either the more or less
commands.
Searching for a pattern within both the more and less commands is done by typing the slash, /,
followed by the pattern to find. If a match is found, the screen should scroll to the first match.
To move forward to the next match, press the n key. With the less command you can also
move backwards to previous matches by pressing the N (capital n)key.
5. Use the less command to display the entire contents of the /etc/passwd file. Then search for
the word bin, use n to move forward, and N to move backwards. Finally, quit the less pager by
typing the letter q:
less /etc/passwd
/bin
n n n N N N q
Unlike the more command which automatically exits when you reach the end of a file, you must
press a quit key such as q to quit the less program.
6. You can use the head command to display the top part of a file. By default, the head command
will display the first ten lines of the file:
head /etc/passwd
tail /etc/passwd
8. Use the head command to display the first two lines of the /etc/passwd file:
head -2 /etc/passwd
9. Execute the following command line to pipe the output of the ls command to the tail
command, displaying the last five file names in the /etc directory:
ls /etc | tail -5
You have also seen where head and tail commands are different: the head command starts
counting its lines to output from the top of the data, whereas the tail command counts the
number of lines to output from the bottom of the data. There are some additional differences
between these two commands as demonstrated in the next few tasks.
10. Another way to specify how many lines to output with the head command is to use the option -
n -#, where # is number of lines counted from the bottom of the output to exclude. Notice the
minus symbol (-) in front of the #. For example, if the /etc/passwd contains 24 lines and the
following command will display lines 1-4, excluding the last twenty lines:
Standard error (stderr) occurs when an error occurs during the execution of a command; it has a
file descriptor of 2. Error messages are also sent to the terminal window by default.
In this lab, you will use characters that redirect the output from standard output ( stdout) and
standard error (stderr) to a file or to another command instead of the terminal screen.
Standard input, stdin, usually is provided by you to a command by typing on the keyboard; it has a
file descriptor of 0. However, by redirecting standard input, files can also be used as stdin.
Now open the terminal application and complete the following tasks
The second command redirects the output; instead of sending it to the terminal, the output is
sent to a file called mymessage.
2. When you use the > symbol to redirect stdout, the contents of the file are first destroyed. Type
the following commands to see a demonstration:
cat mymessage
echo Greetings > mymessage
cat mymessage
Notice that using one redirection symbol overwrites an existing file. This is called "clobbering"
a file.
3. You can avoid clobbering a file by using >> instead of > . By using >> you append to a file.
Execute the following commands to see a demonstration of this:
cat mymessage
echo "How are you?" >> mymessage
cat mymessage
Notice that by using >> all existing data is preserved and the new data is appended at the end
of the file.
4. The find command is a good command to demonstrate how stderr works. This command
searches the filesystem for files based on criteria such as filename. Run the following command
and observe the output:
Notice the error message indicating you do not have permission to access
certain files/directories. This is because as a regular user, you don't have
right to "look inside" some directories. These types of error messages are
sent to stderr, not stdout.
The find command will be covered in greater detail later. The command is just being used now
to demonstrate the difference between stdout and stderr.
Note: the previous example demonstrates why knowing redirection is important. If you want to
"ignore" the errors that the find command displays, you can redirect those messages into a file
and look at them later, making it easier to focus on the rest of the output of the command.
6. You can also redirect stdout and stderr into two separate files.
Notice that a space is permitted but not required after the > redirection
symbol.
7. To redirect both standard output (stdout) and standard error (stderr) to one file, first redirect
stdout to a file and then redirect stderr to that same file by using the notation 2>&1.
The 2>&1 part of the command means send the stderr (channel 2) to the same place where
stdout (channel 1) is going.
ls -l /etc | more
You will need to press the spacebar to continue or you can also press CTRL-c to escape this
listing.
The cut command is useful for extracting fields from files that are either delimited by a
character, like the colon (:) in /etc/passwd, or that have a fixed width. It will be used in the
next few examples as it typically provides a great deal of output that we can use to demonstrate
using the | character.