CST8207: Linux Operating Systems I

Lab Worksheet 3 – Chapters 1 – 5

Using Standard Linux Commands II


This is Lab Worksheet 3 - 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

Log in to the Course Linux Server to do all commands in this lab. Set your bash PS1 shell prompt to show your login name, computer name, and the basename of your current directory, just as you did in the previous Lab. Leave your finished work on the server; do not delete it when you are finished the worksheet.

1Command: man - online help for commands and more

The man command, short for Manual Pages, displays the manual page for the specified command. Man pages, as they are commonly referred to, contain all of the pertinent information on the basic command concepts, how to use the command, the command structure, basic options available for the command and how to use them, advanced options (if any), and related topics, in that order. The man command syntax is:

Examples: man ls ; man man ; man passwd ; man group ; man hosts

A text screen will show up with the information you requested - if it exists. You can then scroll up and down the man page using the up () and down () arrow keys and/or the [PgUp] and [PgDn] keys on your keyboard. You can also use the spacebar to scroll down one screen. Once you are done with the man page, simply type q for quit and you will exit the man page display. You can type q any time you want to exit the manual pages and you can type h or ? for a help screen listing all the other neat things you can do while looking at this manual page. The most common thing to type is a blank (space), which displays the next page of the help file.

When you don't know the specific command for an operation, you can search the man page titles based on a keyword. (You can only search the title lines.) For this you need to specify the -k (keyword) option:

Example: man -k games (lists all man page title lines that contain the word games)

Command: clear

You can clear the text on a terminal by using the clear command, or by typing ^L (CTRL-L) into the shell.

2Command: touch

The touch command updates the “last modified” time/date stamps on one or more existing files. It can also be used to create one or more new, empty files. See the manual page for more features.

Creating empty files and updating the modification time

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

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, as you did in the previous Lab.


[user@host ]$ cd

[user@host ~]$ rm -rf lab3.2 (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 lab3.2 (create a new, empty sub-directory)

[user@host ~]$ cd lab3.2 (make this the new current working directory)

[user@host lab3.2]$ touch clock (create a new, empty file)

[user@host lab3.2]$ ls -l clock (The option -l is lower-case letter L, not the digit 1)

    1. Record only the time/date stamp: ___________________________________

[user@host lab3.2]$ sleep 60 (Waits for 60 seconds. Read a book.)

[user@host lab3.2]$ touch clock (update the time stamp on the existing file)

[user@host lab3.2]$ ls -l clock (the -l option is a letter, not a digit)

    1. Record only the new time/date stamp: ________________________________

3Command: cp (copy)

The cp (Copy) command makes a copy of files or directories. The syntax for the cp command is:

where sources... is one or more files or directories and destination is either a file or a directory. If the destination is a directory, the file(s) will be copied into that directory using their same names. If you want to copy directories, you must use options such as -r or -a; otherwise, cp copies only source files.

[user@host ]$ cd

[user@host ~]$ rm -rf lab3.3 (remove this directory and everything inside it)

[user@host ~]$ mkdir lab3.3 (create a new, empty sub-directory)

[user@host ~]$ cd lab3.3 (make this the new current working directory)

[user@host lab3.3]$ touch a b c (create three new, empty files)

[user@host lab3.3]$ ls

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

[user@host lab3.3]$ mkdir mydir

[user@host lab3.3]$ ls -F (that is an UPPER CASE option letter)

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

[user@host lab3.3]$ cp a b c mydir

[user@host lab3.3]$ ls mydir

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

[user@host lab3.3]$ mkdir snack

[user@host lab3.3]$ touch snack/pie

[user@host lab3.3]$ cd snack

[user@host snack]$ touch apple

[user@host snack]$ cd ..

[user@host lab3.3]$ cp snack/pie snack/apple mydir

[user@host lab3.3]$ ls mydir

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

[user@host lab3.3]$ mkdir A B C (these are UPPER CASE directory names)

    1. What command line could you use to verify that the three directories have been created?

      ______________________________________________________________________

[user@host lab3.3]$ touch A/foo B/bar C/empty (create three files)

[user@host lab3.3]$ cp A B C mydir (try to copy A and B and C into mydir)

    1. Record one of the messages displayed on the screen: ___________________________________

[user@host lab3.3]$ ls mydir (confirm that no directories were copied)

    1. Why were the three source A,B,C directories not copied into destination directory mydir?

      ____________________________________________________________________________________

[user@host lab3.3]$ cp -r A B C mydir (the copy succeeds using this option!)

[user@host lab3.3]$ ls -R mydir (that is an UPPER CASE option letter)

    1. What command line could you use to see the index number and date of the new copy of file empty?

      _______________________________________________________________________

[user@host lab3.3]$ mkdir -p parent/child (remember what -p does?)

[user@host lab3.3]$ cp -r parent mydir (remember what -r does?)

    1. Give the absolute path of the new copy of directory child after the above copy command creates it:

      _______________________________________________________________________

4Command: mv (move or rename)

The mv (Move or Rename) command moves (renames) files or directories. The renaming is very fast because the file data is not copied during a move/rename; only the names change (unless the move has to move the data to a different disk partition). Renaming is not a costly operation. The syntax for the mv command is:

where sources... is one or more files or directories and destination is either a file or a directory. If the destination is a directory, the source files or directories will be moved (renamed) into that directory using their same names. If the destination is a file, only one source file is allowed to be moved (renamed). Examples:

Note that the destination must be an existing directory name if you are moving more than one thing, and you will get an error message if you try to move multiple things to a file name, e.g. mv file1 file2 file3

[user@host ]$ cd

[user@host ~]$ rm -rf lab3.4 (remove this directory and everything inside it)

[user@host ~]$ mkdir lab3.4

[user@host ~]$ cd lab3.4

[user@host lab3.4]$ touch A (create a single new, empty file)

[user@host lab3.4]$ ls -i (note the index number of A)

[user@host lab3.4]$ cp A foo (copy the file)

[user@host lab3.4]$ mv A bar (move the file)

[user@host lab3.4]$ ls -i (note the index numbers)

    1. Looking at the index numbers: did moving file A to bar copy any data? _________________________

[user@host lab3.4]$ touch green blue orange (create three new empty files)

[user@host lab3.4]$ mv green blue orange (try to move/rename the files)

    1. Record the error message: ____________________________________________________

[user@host lab3.4]$ mkdir colours

[user@host lab3.4]$ mv green blue orange colours

    1. Give the absolute path of the file blue: ___________________________________________

[user@host lab3.4]$ mkdir fans players arena (three new directories)

[user@host lab3.4]$ touch fans/me players/you (two new files inside two directories)

[user@host lab3.4]$ mv fans players arena (move two directories into a third)

    1. Give the new absolute path of the file named you after the above mv command has moved it:

      _______________________________________________________________________

5Command: rm (remove or delete files, recursively remove directories)

The rm (Remove or Delete) command removes (deletes) files. If the -r option is specified, it recursively deletes directories and all their contents. Unlike DOS, Windows, or OSX, a file or directory that is deleted with the rm command is gone (is not saved in a Recycle Bin) and not easily recovered. The syntax for the rm command is:

Another useful option to rm is -f (force) that turns off any interactive prompts and most error messages. We have been using “rm -rf” to completely recursively remove lab directories at the start of each section.

Note: Most Unix/Linux shells let you type multiple commands on one line by separating them using the semi-colon character ';'. Type the four commands below, separated by three semi-colon characters:

[user@host ]$ cd ; rm -rf lab3.5 ; mkdir lab3.5 ; cd lab3.5

The above line goes to your HOME directory, removes the current lab3.5 directory (and everything inside it), then re-creates it and makes it your current working directory. You do this at the start of each section of your labs, so that you have a "clean" empty directory in which to work. Continue working:

[user@host lab3.5]$ mkdir sandbox sandbox/toybox (create two directories)

[user@host lab3.5]$ touch toy1 toy2 toy3 (create three empty files)

[user@host lab3.5]$ mv toy1 toy2 toy3 sandbox/toybox (move all 3 files)

[user@host lab3.5]$ ls sandbox/toybox (you should see three toy files)

[user@host lab3.5]$ rmdir sandbox (try to remove the non-empty directory)

    1. Record the error message: ____________________________________________________

[user@host lab3.5]$ rmdir -p sandbox (try again to remove the non-empty directory)

    1. Record the error message: ____________________________________________________

[user@host lab3.5]$ cp -a sandbox savebox (save a full copy of sandbox in savebox)

[user@host lab3.5]$ ls -R savebox (confirm that all sandbox has been copied to savebox)

[user@host lab3.5]$ rm -r sandbox (recursively delete sandbox and everything in it)

[user@host lab3.5]$ ls (confirm that sandbox is gone)

[user@host lab3.5]$ mv savebox sandbox (what does this do?)

    1. Give the absolute path of the file toy2 after the above commands are finished:

      _______________________________________________________________________

[user@host lab3.5]$ cp -a sandbox/toybox sandbox (recursive copy FAILS – why?)

    1. Explain why the above copy fails: ________________________________________________________

      ____________________________________________________________________________________

The -i option to rm will turn on “Interactive” mode, where you are prompted about every file being deleted:

[user@host lab3.5]$ cp -a sandbox/toybox . (note the DOT ending this command line)

[user@host lab3.5]$ ls toybox (you should see three toy files in here)

[user@host lab3.5]$ rm -ri toybox (answer yes to all the interactive questions)

6Command: cat (catenate or show contents)

cat (Catenate, or Show) opens one or more files and catenates (shows) their contents. You can use it on any size file, but files are not be paginated and large files will scroll off your screen. Unlike less, cat won't warn you if you're about to mess up your terminal by displaying a binary format file. The syntax for cat is:

Try these examples using cat and less:

[user@host ~]$ cat /etc/issue.net (this contains the network login banner)

[user@host ~]$ cat /etc/resolv.conf (this contains the system DNS server IP addresses)

[user@host ~]$ cat /etc/issue.net /etc/resolv.conf /etc/issue.net

[user@host ~]$ cat /etc/services (many lines scroll off screen!)

[user@host ~]$ less /etc/services (use less to show files one page at a time)

[user@host ~]$ more /etc/services (a pagination program similar to less)

    1. What option to cat shows non-printing characters? ____________________

    2. What option to cat will number the output lines? ______________________

    3. What option to cat will suppress repeated empty output lines? ____________________

    4. "cat" backwards is "tac". What does the tac command do? __________________________________

      Compare "cat /etc/resolv.conf" with "tac /etc/resolv.conf".

7Command: fgrep - find lines containing text strings inside file(s)

A family of related programs – grep, fgrep, and egrep – open files and look for text inside the files.

grep (Global Regular Expression Print) opens zero or more files and prints lines from those files that match a Regular Expression pattern. egrep searches for an extended Regular Expression . We will first learn to use the simpler fgrep program that searches for simple text strings, not patterns.


The fgrep command (Fixed grep) searches for simple text strings, not a pattern. Always use fgrep until you know how to use Regular Expression patterns. Using the -e option, you can search for lines containing any one of multiple text strings at the same time. The syntax for fgrep is:

Until you learn how to use Regular Expression patterns, always use the fgrep command that does not treat any characters specially. fgrep searches for exactly the text you enter. Try these examples using fgrep :


[user@host ~]$ fgrep 'abcd0001' /etc/passwd (use your own userid and not abcd0001)

[user@host ~]$ fgrep '$' /etc/crontab (must use fgrep due to special character)

[user@host ~]$ fgrep -e 'root:' -e 'games:' /etc/passwd (find either string)

[user@host ~]$ history | fgrep 'date' (look for the date command in your shell history)

[user@host ~]$ history | fgrep 'pwd' (look for the pwd command in your shell history)


You can chain together fgrep commands using pipes to find lines that match all the text strings. (More on pipes in a later worksheet.) Using pipes, only lines that contain all the text strings will display:


[user@host ~]$ man man | fgrep 'italic' (lines containing the text ‘italic)

[user@host ~]$ man man | fgrep 'italic' | fgrep 'replace' (lines with both strings)


All the text strings must be present in each line found when you chain fgrep commands together with pipes:


[user@host ~]$ fgrep 'the' /etc/fstab (should find and print at least one line)

[user@host ~]$ fgrep 'none' /etc/fstab (should find and print at least one line)

[user@host ~]$ fgrep 'the' /etc/fstab | fgrep 'none' (no output! Why?)[user@host ~]$ fgrep 'the' /etc/fstab | fgrep 'print' (outputs one line. Why?)


  1. Why is there no output from the third command pipeline, above, searching for ‘the’ and ‘none’ ?

________________________________________________________________________________________

________________________________________________________________________________________

________________________________________________________________________________________

________________________________________________________________________________________

  1. What does the -i option mean to fgrep?

    ________________________________________________________________________________________

  2. What does the -v option mean to fgrep?

    ________________________________________________________________________________________


  1. What does the -w option mean to fgrep?

    ________________________________________________________________________________________

8Review exercise: cd, mkdir, touch, mv, rm, cp, mkdir, find

Enter exactly the commands that are shown in bold below and note which commands produce errors. (There will be three errors; this is intentional.) Answer the questions following based only on these review commands. The tilde characters below have the same meaning as in the previous lab. (Go look!) Be precise in your typing!

  1. cd ; rm -rf ~/lab3.8

  2. mkdir ~/lab3.8

  3. cd ~/lab3.8

  4. mkdir ./orchard

  5. touch apple orange

  6. mv orange orchard/lemon

  7. rm orange

  8. touch lettuce tomato cucumber

  9. cp tomato lettuce garden

  10. mkdir jardin forest

  11. mv lettuce cucumber jardin

  12. rmdir garden

  13. touch lab3

  14. cd orchard

  15. cd ../../lab3.8/forest

  16. mv ../lab3 ../tomato

    1. Give the command number that generated the error followed by the full and exact error message:

      _______________________________________________________________________

      _______________________________________________________________________

      _______________________________________________________________________

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

      _______________________________________________________________________

    3. Give the absolute pathname of the one regular file lemon that is now in the directory named orchard:

      _______________________________________________________________________

    4. Give the relative path to the same lemon file from the forest directory:

      _______________________________________________________________________

    5. Give the relative path to the same lemon file from your own HOME directory:

      _______________________________________________________________________

    6. Give the relative path to the same lemon file from the directory called /home:

      _______________________________________________________________________

    7. Give the relative path to the same lemon file from the Linux ROOT directory:

      _______________________________________________________________________

    8. Give the relative path to the same lemon file from the directory called /root:

      _______________________________________________________________________

    9. List the basenames of directories that were successfully created (at any time) during the review exercise:

      _______________________________________________________________________

      _______________________________________________________________________

    10. List the absolute pathnames of all directories that were successfully deleted during the review exercise:

      _______________________________________________________________________

    11. List the absolute pathnames of the five regular files still remaining anywhere under the directory lab3.8. Do not include the names of any directories or sub-directories – list only the five absolute regular file names located anywhere under the review directory lab3.8:

      _______________________________________________________________________

      _______________________________________________________________________

      _______________________________________________________________________

      _______________________________________________________________________

      _______________________________________________________________________

    12. What command line recursively finds and displays all pathnames under ~/lab3.8 ?

      _______________________________________________________________________

    13. What does the find expression "-ls" do? (See "man find".)

      ______________________________________________________________________________

    14. What does the find expression "-type f" do? (See "man find".)

      ______________________________________________________________________________

    15. What does the find expression "-size 100M" do? (See "man find".)

      ______________________________________________________________________________

    16. What does the find expression "-size +100M" do (note the plus sign)? (Hint: Search the man page for the string "numeric arguments" which explains how numbers can be specified to find.)

      ______________________________________________________________________________

    17. 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.)

      _______________________________________________________________________

    18. What command line recursively finds and displays only pathnames ending in "log" in the system directory /etc (you will see many Permission denied messages in the output, as well as pathnames)?

      _______________________________________________________________________

    19. What command line recursively finds and displays only pathnames for things bigger than 500 Kilobytes in the system directory /etc (you will see many Permission denied messages in the output)?

      _______________________________________________________________________

    20. Are you keeping a reference list of the commands used in every lab, along with the options used and what they mean? Future labs and tests will expect you to remember these command names and options.

      ______________________________________________________________________________


9Using shell command line history (similar to DOS doskey)

Purpose

Command Line Example

Show all your command history

$ history | less

Re-execute any previous command

$ !n (the n is the number shown by history )

Re-execute any previous command

Use the Up and Down arrow keys, then push [Enter]

Re-execute the last command

$ !!

Re-execute last command starting with foo

$ !foo

Show (print) command but do not execute

$ !foo:p

Re-use first argument from previous command

$ echo !^ ; wc !^ ; rm !^

Re-use all arguments from previous command

$ echo !* ; wc !* ; rm !*

Using the built-in shell history command (saved in ~/.bash_history )

  1. Describe what kind of output the above history command pipeline generates:

_________________________________________________________________________________

_________________________________________________________________________________

  1. Describe what the double-exclamation command !! would do, but don't actually do it:

_________________________________________________________________________________

  1. Describe what typing exclamation-two !2 would do, but don't actually do it:

_________________________________________________________________________________

  1. If the history command shows you a command, number 120, that you would like to re-execute, what would you type into the shell to do that?

_______________________________________________________________________

Page 8 of 8

©2010 -2013 Algonquin College

Shawn Unger, Ian Allen, Todd Kelley

worksheet03.odt

Version 12 2017-01-30, 04:34