Os Lab 2
Os Lab 2
LAB #02
1- Lab objective
In this lab, you will explore the Linux file system, including the basic concepts of
files and directories and their organization in a hierarchical tree structure.
2- Background
Current Directory
Your shell has a current directory — the directory in which you are currently
working
Commands like ls use the current directory if none is specified
Use the pwd (print working directory) command to see what your current
directory is:
$ pwd
/home/fred
Change the current directory with cd:
$ cd /mnt/cdrom
$ pwd
/mnt/cdrom
Use cd without specifying a path to get back to your home directory
Making and Deleting Directories
The mkdir command makes new, empty, directories
For example, to make a directory for storing company accounts:
$ mkdir Accounts
To delete an empty directory, use rmdir:
$ rmdir OldAccounts
Use rm with the -r (recursive) option to delete directories and all the files
they contain:
$ rm -r OldAccounts
Be careful — rm can be a dangerous tool if misused
Relative Paths
Paths don’t have to start from the root directory
o A path which doesn’t start with / is a relative path
o It is relative to some other directory, usually the current directory
For example, the following sets of directory changes both end up in the
same directory:
$ cd /usr/share/doc
$ cd /
$ cd usr
$ cd share/doc
Relative paths specify files inside directories in the same way as
absolute ones
Special Dot Directories
Every directory contains two special filenames which help making relative
paths:
o The directory .. points to the parent directory
ls .. will list the files in the parent directory
o For example, if we start from /home/fred:
$ cd ..
$ pwd
/home
$ cd ..
$ pwd
/
The special directory . points to the directory it is in
o So ./foo is the same file as foo
Running Programs
Programs under Linux are files, stored in directories like /bin and /usr/bin
o Run them from the shell, simply by typing their name
Many programs take options, which are added after their name and
prefixed with -
For example, the -l option to ls gives more information, including the size of
files and the date
they were last modified:
$ ls -l
drwxrwxr-x 2 fred users 4096 Mar 01 10:57 Accounts
-rw-rw-r-- 1 fred users 345 Mar 01 10:57 notes.txt
-rw-r--r-- 1 fred users 3255 Mar 01 10:57 report.txt
Many programs accept filenames after the options
o Specify multiple files by separating them with spaces
Specifying Multiple Files
Most programs can be given a list of files
o For example, to delete several files at once:
$ rm oldnotes.txt tmp.txt stuff.doc
o To make several directories in one go:
$ mkdir Accounts Reports
The original use of cat was to join multiple files together
o For example, to list two files, one after another:
$ cat notes.txt morenotes.txt
If a filename contains spaces, or characters which are interpreted by
the shell (such as *), put
single quotes around them:
$ rm ’Beatles - Strawberry Fields.mp3’
$ cat ’* important notes.txt *’
Text Editors
Text editors are for editing plain text files
o Don’t provide advanced formatting like word processors
o Extremely important — manipulating text is Unix’s raison d’être
The most popular editors are Emacs and Vim, both of which are very
sophisticated, but take
time to learn
Simpler editors include Nano, Pico, Kedit and Gnotepad
Some programs run a text editor for you
o They use the $EDITOR variable to decide which editor to use
o Usually it is set to vi, but it can be changed
o Another example of the component philosophy
4- Exercises
Q1
a. Use the pwd command to find out what directory you are in.
b. If you are not in your home directory (/home/USERNAME) then use cd
without any arguments to go there, and do pwd again.
c. Use cd to visit the root directory, and list the files there. You should see home
among the list.
d. Change into the directory called home and again list the files present.
There should be one directory for each user, including the user you are
logged in as (you can use whoami to check that).
e. Change into your home directory to confirm that you have gotten back
to where you started.
Q2
a. Create a text file in your home directory called shakespeare,
containing the following text:
Shall I compare thee to a summer’s day?
Thou art more lovely and more temperate
b. Rename it to sonnet-18.txt.
c. Make a new directory in your home directory, called poetry.
d. Move the poem file into the new directory.
e. Try to find a graphical directory-browsing program, and find your
home directory with it. You should also be able to use it to explore
some of the system directories.
f. Find a text editor program and use it to display and edit the sonnet.
Q3
a. From your home directory, list the files in the directory /usr/share.
b. Change to that directory, and use pwd to check that you are in the
right place. List the files in the current directory again, and then list the
files in the directory called doc.
c. Next list the files in the parent directory, and the directory above that.
d. Try the following command, and make sure you understand the result:
$ echo ˜
e. Use cat to display the contents of a text file which resides in your
home directory (create one if you haven’t already), using the ˜/ syntax to
refer to it. It shouldn’t matter what your current directory is when you
run the command.
Q4
Use the hostname command, with no options, to print the hostname of the
machine you are using.
a. Use man to display some documentation on the hostname command. Find
out how to make it print the IP address of the machine instead of the
hostname. You will need to scroll down the manpage to the ‘Options’
section.
b. Use the locate command to find files whose name contains the text
‘hostname’. Which of the filenames printed contain the actual
hostname program itself? Try running it by entering the program’s
absolute path to check that you really have found it.
Q5
a. The * wildcard on its own is expanded by the shell to a list
of all the files in the current directory. Use the echo
command to see the result (but make sure you are in a
directory with a few files or directories first)
b. Use quoting to make echo print out an actual * symbol.
c. Augment the poetry directory you created earlier with
another file, sonnet- 29.txt:
When in disgrace with Fortune
and men’s eyes, I all alone
beweep my outcast state,
d. Use the cat command to display both of the poems, using a
wildcard.
e. Finally, use the rm command to delete the poetry
directory and the poems in it.