OS Lab03
OS Lab03
Operating systems
LAB MANUAL 3
Date:
Name:
Reg#: Group:
Marks: Signature:
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Operating systems
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.
File and Directories
A directory is a collection of files and/or other directories
o Because a directory can contain other directories, we get a directory
hierarchy
The ‘top level’ of the hierarchy is the root directory
Files and directories can be named by a path
o Shows programs how to find their way to the file
o The root directory is referred to as /
o Other directories are referred to by name, and their names are separated by
slashes (/)
If a path refers to a directory it can end in /
Usually an extra slash at the end of a path makes no difference
Linux Files and Directories
Examples of Absolute Paths
An absolute path starts at the root of the directory hierarchy, and names
directories under it:
/etc/hostname
o Meaning the file called hostname in the directory etc in the root directory
We can use ls to list files in a specific directory by specifying the absolute path:
$ ls /usr/share/doc/
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
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Operating systems
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
Operating systems
$ 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
Operating systems
The. directory is most commonly used on its own, to mean “the current directory”
Hidden Files
The special . and .. directories don’t show up when you do ls
o They are hidden files
Simple rule: files whose names start with . are considered ‘hidden’
Make ls display all files, even the hidden ones, by giving it the -a (all) option:
$ ls -a
. .. .bashrc .profile report.doc
Hidden files are often used for configuration files
o Usually found in a user’s home directory
You can still read hidden files — they just don’t get listed by ls by default
Operating systems
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
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Operating systems
Operating systems
Operating systems
Operating systems
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.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Operating systems
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
a. Use the hostname command, with no options, to print the hostname of the
machine you are using.
b. 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.
c. 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)
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Operating systems