CST8207: Linux Operating Systems I

Lab Worksheet 2 – Chapters 1 – 3

Using Standard Linux Commands I


This is Lab Worksheet 2 - not an Assignment

This Lab Worksheet contains some practical examples that will prepare you to complete your Assignments.
You do not have to hand in this Lab Worksheet. Make sure you complete the separate Assignments on time. Quizzes and tests may refer to work done in this Lab Worksheet; save your answers.

Before you get started - REMEMBER TO READ ALL THE WORDS

You must have an account on the Course Linux Server to do this lab. Log in to the server and use the shell. Review the Class Notes related to this worksheet as you work through it. Leave your work on the Linux server. Do not delete any work from the Linux server until the term is over and your marks are complete!

Commands introduced and used in this Lab

1Command: man

The man (Manual) command takes the name of a command as a parameter, e.g. “man pwd” or “man ls”. It displays the first page of a help file and pauses, waiting for you to type “q” to quit reading or “h” for more options. The most common thing to type is a blank (space), which displays the next page of the help file.

  1. Read the man page for the pwd command and give its full NAME (one-line description) here:

    ____________________________________________________________________

Use the man command to read up on each of the commands you use in this course, including the man command itself ("man man"). The cd command is built-in to the shell and does not have its own man page - see the man page for the bash shell for details on all built-in shell commands, or type: help cd

The answers to the following questions are under DESCRIPTION inside “man man”:

  1. What does bold text mean in the SYNOPSIS section of a man page?

    __________________________________________________________________

  2. What does italic text mean in the SYNOPSIS section of a man page?

    __________________________________________________________________

  3. What do square brackets [] mean in the SYNOPSIS section of a man page?

    __________________________________________________________________

  4. What does the pipe symbol | (SHIFT-\) mean in the SYNOPSIS section of a man page?

    __________________________________________________________________

  5. What do three dots (ellipsis) ... mean in the SYNOPSIS section of a man page?

    __________________________________________________________________

2Commands: cd and pwd

Set your shell prompt: Before doing this lab, set your bash shell prompt to show your login name, the computer name, and the basename of your current working directory using this command that sets the PS1 variable that contains the prompt (type this exactly and use single quotes and two blanks, one near the end):

The user string in the shell prompt will be your own userid, which is why it is shown in the italic font in this Lab. Text in italic font is not to be typed literally. The host string will be the hostname of the computer; it is also shown in italic font in this Lab. The shell will replace the characters \W (upper-case W!) by the basename of your current directory. You will have to set your prompt every time you log in.

The cd (Change Directory) command allows you to navigate through the Linux directory hierarchy structure by changing your shell's current working directory. The syntax for cd is:

Typing cd with no directoryname argument will take you to your personal HOME directory (which is not the same thing as the directory called /home - be careful!). Providing a single directoryname parameter will change your shell's current working directory to the given directory. While you are working with the cd command, watch the shell prompt; it will change to display the basename of the current working directory after each cd command. Your HOME directory is indicated in the shell prompt by a tilde character: ~ This tilde character indicates you are in your own personal HOME directory (not the system directory called /home - be careful to distinguish between your HOME and the system directory).


  1. At the command prompt type cd without any parameters. Record here the directory basename shown at the right end of the bash shell prompt: [user@host ______________ ]$

  2. Type pwd at the prompt and record the output here: ________________________________

  3. cd / This will change the current directory to the top-level “ROOT” directory.

    What directory basename is shown in the bash prompt after this command?

[user@host ______________ ]$

  1. Give the output of the pwd command now: _______________________________________

  2. cd /etc What directory basename is shown in the bash prompt after this command?

[user@host ______________ ]$

  1. Give the output of the pwd command now: ______________________________________

  2. cd .. (Two periods.) This command will “go up” one directory level (to the ROOT).

    What directory basename is shown in the bash prompt after this command?

[user@host ______________ ]$

  1. Give the output of the pwd command now: ______________________________________

  2. cd home/user Replace the text user with the userid that you are logged in with now.
    What directory
    basename is shown in the bash prompt after this command?

[user@host ______________ ]$

  1. What is full absolute path of the relative path directory argument of the command from (i) above?

Answer: _____________________________________________________________

  1. Give the output of the pwd command now: ______________________________________

  2. cd /usr/local/bin What is the basename in the bash prompt after this command?

[user@host _______________ ]$

  1. Give the output of the pwd command now: ______________________________________

  2. cd ../../sbin What is the basename in the bash prompt after this command?

[user@host _______________ ]$

  1. Give the output of the pwd command now: ______________________________________

  2. cd ../local/bin What is the basename in the bash prompt after this command?

[user@host _______________ ]$

  1. Give the output of the pwd command now: ______________________________________

  2. cd ../../bin What is the basename in the bash prompt after this command?

[user@host _______________ ]$

  1. What is the full absolute path of the relative path directory argument of the command from (r) above?

Answer: _____________________________________________________________

  1. What is the output of the pwd command now: ____________________________________

  2. Describe the effect of executing a cd command without any arguments; explain what happens:

    Answer: _________________________________________________________________________

3Command: ls

The ls, or List Structure (list directory contents) command lists the names and/or properties of pathnames. Use it to see the names and attributes of directories and files and directories inside directories. The syntax is:

Read the man page for ls to discover many useful options that allow you to display the contents of a directory in many formats. Two common options are -a to show all files (including hidden files that start with a leading period) and -l (lower-case letter L) to get a long listing including most file attributes, such as file owner, file modify date, and file permissions. Single option letters can be typed separately or bundled together after a single dash in most Linux commands, as follows:

Perform the following commands and observe how the output of ls changes:

  1. ls /bin/ls

  2. ls -l /bin/ls # The option -l is lower-case letter L, not the digit 1

  3. ls -lis /bin/ls

  4. ls /home/user # Replace the text user with your current login userid

  5. ls -a /home/user # Replace user with your current login userid

  6. ls -al /home/user # Replace user with your current login userid

  7. ls -ld /home/user # Replace user with your current login userid

  8. Look up the meaning of the -d option to ls in the manual page for ls. Explain what it does:

    Answer: ________________________________________________________________________

  9. Look up the meaning of the -i option to ls in the manual page for ls. Explain what it does:

    Answer: ________________________________________________________________________

Shell file name completion: Without using the [Enter] key, type just the six characters “ls /ho“ and then press the [Tab] key. The shell will fill in the rest of the “/home” name for you. Let the shell do your typing for you! Also try using the TAB key on this pathname: ls -ld /lo[Tab]

After typing all the above commands, press the 'up arrow' and then 'down arrow' keys to scroll up and down in the list of commands you have typed. Note how you can re-execute any command by scrolling to it with the arrow keys and pushing the [Enter] key anywhere in the command line to execute it again.

Sending long output into the pagination commands less or more

Often, a directory listing might be longer than a single screen and may scroll off the top of the window you are using. You can view any long output one screen at a time using a pagination command such as “less” or “more”. To send the output of ls into the input of less” or “more”, separate the commands using the “pipe” symbol “|” (found above the backslash on most keyboards). Try these three command lines:

  1. ls -al /usr/bin # This will produce thousands of lines of output on your screen!

  2. ls -al /usr/bin | less # This paginates the huge output one screen at a time.

  3. ls -al /usr/bin | more # This paginates the huge output one screen at a time.


Use the [Spacebar] (space) to jump to the next screen of information and the letter b to go backward one screen, just as you did using the man command. You can use q to quit the command and the letter h to bring up a screen of other useful commands. The man command uses less to paginate manual pages. The command “more” is an older version of “less” with fewer features - type h to get help as well.

4Command: mkdir

The mkdir (Make Directory) command allows you to create one or more new, empty directories (folders), provided the names aren't already being used. The syntax for the mkdir command is:

Perform the following commands shown in bold type. Commands will produce no output if they succeed.

[user@host ~]$ cd

[user@host ~]$ rm -rf lab2.4 # remove this directory and everything inside it

(The above command will make a “clean slate” if you choose to restart this section from the start.)

[user@host ~]$ mkdir lab2.4 # create a new, empty sub-directory

[user@host ~]$ cd lab2.4 # make lab2.4 the current directory

[user@host lab2.4]$ mkdir dir1 dir2 # create two new, empty sub-directories

[user@host lab2.4]$ ls

  1. Give the output of the last command, above: _____________________________________

[user@host lab2.4]$ cd dir1 # make dir1 the current working directory

[user@host dir1]$ ls -a

  1. Give the output of the last command, above: _____________________________________

[user@host dir1]$ mkdir subdir # create a new, empty sub-directory

[user@host dir1]$ ls -a

  1. Give the output of the last command, above: _____________________________________

[user@host dir1]$ cd .. # two periods: go up one directory level

[user@host lab2.4]$ mkdir parent/child # fails to create a new directory

  1. Record the error message: __________________________________________________

  2. Explain why the above command failed and did not execute as expected:

    __________________________________________________________________________________

[user@host lab2.4]$ mkdir –p parent/child # look up -p in the man page for mkdir

  1. The above command succeeds with no errors. What does the -p option to the mkdir command do?

    __________________________________________________________________________________

5Command: rmdir

The rmdir (Remove Directory) command allows you to remove one or more directories, but only if each directory is empty (contains no files or other sub-directories). The syntax for the rmdir command is:

Perform the following commands shown in bold type. Commands will produce no output if they succeed.

[user@host ]$ cd

[user@host ~]$ rm -rf lab2.5 # remove this directory and everything under it

[user@host ~]$ mkdir lab2.5 # create a new, empty sub-directory

[user@host ~]$ cd lab2.5 # make lab2.5 the current directory

[user@host lab2.5]$ mkdir dir1 dir2 test # create three new, empty directories

[user@host lab2.5]$ ls -l # option -l is lower-case letter L, not the digit 1

  1. Give the 4-line output of the last command, above: _________________________________

    ___________________________________________________________________

    ___________________________________________________________________

    ___________________________________________________________________

[user@host lab2.5]$ rmdir test

[user@host lab2.5]$ ls

  1. Give the two-word output of the last command, above: _______________________________

[user@host lab2.5]$ mkdir –p dir1/subdir parent/child

[user@host lab2.5]$ cd dir1

[user@host dir1]$ rmdir dir2 # this fails with an error message

  1. Record the error message: __________________________________________________

  2. Why did the command generate this error message? Explain why the command failed:

    __________________________________________________________________________________

[user@host dir1]$ rmdir ../dir2

[user@host dir1]$ cd ../dir2 # this fails with an error message

  1. Record the error message: __________________________________________________

[user@host dir1]$ cd .. # two dots means go up one directory level

[user@host lab2.5]$ rmdir dir1/subdir

[user@host lab2.5]$ rmdir dir1

[user@host lab2.5]$ ls -l

  1. Give the 2-line output of the last command, above: __________________________________

    ____________________________________________________________________

[user@host lab2.5]$ rmdir parent/child parent

  1. Why doesn't the above command produce an error message about the non-empty directory parent?

    _________________________________________________________________________________

6Review exercise: cd, mkdir, rmdir

Enter the 13 commands that are shown in bold below and note which commands produce errors. (There will be some errors, this is intentional.) In the following questions, record the errors along with the number of the command line that generated each. Note the use of leading tilde characters below, indicating to the shell that this pathname starts in your HOME directory (not the directory called /home). In this case, the leading tilde on the pathname is shell short-hand for /home/user, where user is your login userid.

  1. rm -rf ~/lab2.6 # Note the use of the tilde character!

  2. cd

  3. mkdir ~/lab2.6 # Note the use of the tilde character!

  4. cd lab2.6

  5. mkdir ./hockey

  6. mkdir soccer football

  7. rmdir ~/lab2.6 # Note the use of the tilde character!

  8. rmdir hockey

  9. mkdir ~/lab2.6/course # Note the use of the tilde character!

  10. cd ..

  11. cd hockey

  12. cd lab2.6/football

  13. rmdir ~/lab2.6/course # Note the use of the tilde character!

Answer these questions below based only on commands 1 to 12, above (ignore the first rm command):

  1. Record exactly each error message along with the command number that generated the message:

    ___________________________________________________________________

    ___________________________________________________________________

    ___________________________________________________________________

  2. What is the absolute path of the shell's current working directory after the last command, above?

    ___________________________________________________________________

  3. What command could you use to verify your previous answer ? ________________________

  4. List by basename only all the directories that you successfully deleted: ___________________

    ___________________________________________________________________

    ___________________________________________________________________

  5. List by absolute pathname every directory you successfully created (including ones you removed):

    ___________________________________________________________________

    ___________________________________________________________________

    ___________________________________________________________________

    ___________________________________________________________________

    ___________________________________________________________________

  6. List every directory and sub-directory remaining under and including lab2.6 using a relative path relative to your HOME directory (the relative pathnames must each start in your HOME directory):

    ___________________________________________________________________

    ___________________________________________________________________

    ___________________________________________________________________

    ___________________________________________________________________

7Command: passwd (change your password)

The passwd (Password) command changes user account passwords. The root super-user can change any user account password; ordinary users can only change their own passwords by not supplying any userid argument.

The command may verify that any password you choose is a secure password - i.e. that it is not a simple known dictionary word and that it is long enough to be secure. A good, secure password should be no less than 6 alpha-numeric characters in length, and contain at least one special/numeric character within it. Note: None of the characters you type for your password will echo on your screen, for security. You will be typing blind.

8Command: find (find pathnames)

The find command recursively walks the directory tree structure, starting at an optional directory, and finds (and usually prints) pathnames, based on many optional expression criteria. See the man page for the many options and expressions. Four common expressions are given below: (a) to find all pathnames under a starting directory, (b) find pathnames containing a particular basename pattern under a starting directory, (c) find pathnames owned by a particular userid, or (d) find pathnames modified within some number of days:

  1. find [starting_directories...] -print

  2. find [starting_directories...] -name 'basename' -print

  3. find [starting_directories...] -user 'userid' -print

  4. find [starting_directories...] -mtime -days -print

Note that the expression -name pattern is the basename, found in any directory, starting from each of the the starting_dirctories. The basename patterns can include quoted shell-GLOB-style path metacharacters such as “*” and “?”. Note the unusual use of full-words following single-dashes as expressions in this command! (Almost all other commands use double dashes for word-style options.) Modern versions of the find command don’t need the -print expression; it’s the default. Examples (try them!):


  1. What command line recursively finds and displays only pathnames owned by userid idallen under the system directory /var/games ? (You should see at least one file.)

    _________________________________________________________________

  2. What does the find expression "-ls" do? (See "man find".) (Optional: Repeat (a) using -ls)

    ______________________________________________________________________________

Did you READ ALL THE WORDS in this Lab?

Page 7 of 7

©2010-2013 Algonquin College

Shawn Unger, Ian Allen, Todd Kelley

worksheet02.odt

Version 16 2019-01-05, 22:14