Lab Tasks in LAB 07
Lab Tasks in LAB 07
Lab No. 4
4.1.0- Lab objective
This lab will give overview of Linux shells. You will get insight of ‘bash’ shell.
4.1.1- Background
Shells
A shell provides an interface between the user and the operating
system kernel
Either a command interpreter or a graphical user interface
Traditional Unix shells are command-line interfaces (CLIs)
Usually started automatically when you log in or open a terminal
Page - 30 -
Operating Systems
Page - 31 -
Operating Systems
The options usually come first, but for most commands they do not
need to
There is a special option ‘--’ which indicates the end of the options
o Nothing after the double hyphen is treated as an option,
even if it starts with -
Page - 32 -
Operating Systems
$ ls -la
4.2.6 Setting Shell Variables
Shell variables can be used to store temporary values
Set a shell variable’s value as follows:
$ files="notes.txt report.txt"
o The double quotes are needed because the value contains a
space
o Easiest to put them in all the time
Print out the value of a shell variable with the echo command:
$ echo $files
o The dollar ($) tells the shell to insert the variable’s value into
the command line
Use the set command (with no arguments) to list all the shell
variables
Page - 33 -
Operating Systems
Page - 34 -
Operating Systems
Page - 35 -
Operating Systems
Page - 36 -
Operating Systems
Page - 37 -
Operating Systems
Page - 38 -
Operating Systems
Page - 39 -
Operating Systems
4.3- Exercises
Q1
a. Use the df command to display the amount of used and
available space on your hard drive.
b. Check the man page for df, and use it to find an option to the
command which will display the free space in a more human-
friendly form. Try both the single-letter and long-style options.
c. Run the shell, bash, and see what happens. Remember that you
were already running it to start with. Try leaving the shell you
have started with the exit command.
Q2
a. Try ls with the -a and -A options. What is the difference between
them?
b. Write a for loop which goes through all the files in a directory and
prints out their names with echo. If you write the whole thing on
one line, then it will be easy to repeat it using the command line
history.
c. Change the loop so that it goes through the names of the
people in the room (which needn’t be the names of files) and
print greetings to them.
d. Of course, a simpler way to print a list of filenames is echo *. Why
might this be useful, when we usually use the ls command?
Page - 40 -
Operating Systems
Q3
a. Use the find command to list all the files and directories under
your home directory. Try the -type d and -type f criteria to show
just files and just directories.
b. Use ‘locate’ to find files whose name contains the string
‘bashbug’. Try the same search with find, looking over all files on
the system. You’ll need to use the * wildcard at the end of the
pattern to match files with extensions.
c. Find out what the find criterion -iname does.
Page - 41 -
Operating Systems
Lab No. 5
5.1.0- Lab objective
This lab will give overview of Basic File and Directory Management utilities.
Page - 42 -
Operating Systems
Page - 43 -
Operating Systems
Page - 44 -
Operating Systems
5.5.1 Examples of cp
Copy /etc/smb.conf to the current directory:
$ cp /etc/smb.conf .
Create an identical copy of a directory called work, and call it
work-backup:
$ cp -a work work-backup
Copy all the GIF and JPEG images in the current directory into
images:
$ cp *.gif *.jpeg images/
Page - 45 -
Operating Systems
Page - 46 -
Operating Systems
Page - 47 -
Operating Systems
Page - 48 -
Operating Systems
5.10 Exercises
Q1
a. Use cd to go to your home directory, and create a new directory there
called dog.
b. Create another directory within that one called cat, and another within
that called mouse.
c. Remove all three directories. You can either remove them one at a
time, or all at once.
d. If you can delete directories with rm -r, what is the point of using rmdir
for empty directories?
e. Try creating the dog/cat/mouse directory structure with a single
command.
Q2
a. Copy the file /etc/passwd to your home directory, and then use cat to
see what’s in it.
b. Rename it to users using the mv command.
c. Make a directory called programs and copy everything from /bin into
it.
d. Delete all the files in the programs directory.
e. Delete the empty programs directory and the users file.
Q3
a. The touch command can be used to create new empty files. Try that
now, picking a name for the new
file:
$ touch baked-beans
b. Get details about the file using the ls command:
$ ls -l baked-beans
c. Wait for a minute, and then try the previous two steps again, and see
what changes. What happens
Page - 49 -
Operating Systems
Page - 50 -
Operating Systems
Lab No. 6
6.1.0- Lab objective
This lab will give overview of Processing Text Streams using Text Processing
Filters
Page - 51 -
Operating Systems
Page - 52 -
Operating Systems
o Default is –clw
Examples: display word count for essay.txt:
$ wc -w essay.txt
Display the total number of lines in several text files:
$ wc -l *.txt
6.5 Sorting Lines of Text with sort
The sort filter reads lines of text and prints them sorted into order
For example, to sort a list of words into dictionary order:
$ sort words > sorted-words
The -f option makes the sorting case-insensitive
The -n option sorts numerically, rather than lexicographically
6.6 Removing Duplicate Lines with uniq
Use uniq to find unique lines in a file
Removes consecutive duplicate lines
Usually give it sorted input, to remove all duplicates
o Example: find out how many unique words are in a dictionary:
$ sort /usr/dict/words | uniq | wc –w
sort has a -u option to do this, without using a separate program:
$ sort -u /usr/dict/words | wc -w
sort | uniq can do more than sort -u, though:
uniq -c counts how many times each line appeared
uniq -u prints only unique lines
uniq -d prints only duplicated lines
6.7 Selecting Parts of Lines with cut
Used to select columns or fields from each line of input
Select a range of
o Characters, with
o Fields, with –f
o Field separator specified with -d (defaults to tab)
Page - 53 -
Operating Systems
Page - 54 -
Operating Systems
Page - 55 -
Operating Systems
Page - 56 -
Operating Systems
6.16 Exercises
Q1
a. Type in the example on the cut slide to display a list of users logged in.
(Try just who on its own first to see what is happening.)
b. Arrange for the list of usernames in who’s output to be sorted, and
remove any duplicates.
c. Try the command last to display a record of login sessions, and then try
reversing it with tac. Which is more useful? What if you pipe the output into
less?
d. Use sed to correct the misspelling ‘enviroment’ to ‘environment’. Use it
on a test file, containing a few lines of text, to check it. Does it work if the
misspelling occurs more than once on the same line?
e. Use nl to number the lines in the output of the previous question.
Q2
a. Try making an empty file and using tail -f to monitor it. Then add lines to
it from a different terminal using a command like this:
$ echo "testing" >>filename
b. Once you have written some lines into your file, use tr to display it with
all occurances of the letters A–F changed to the numbers 0–5.
c. Try looking at the binary for the ls command (/bin/ls) with less. You can
use the -f option to force it to display the file, even though it isn’t text.
d. Try viewing the same binary with od. Try it in its default mode, as well as
with the options shown on the slide for outputting in hexadecimal.
Q3
a. Use the split command to split the binary of the ls command into 1Kb
chunks. You might want to create a directory especially for the split files,
so that it can all be easily deleted later.
Page - 57 -
Operating Systems
b. Put your split ls command back together again, and run it to make sure
it still works. You will have to make sure you are running the new copy of it,
for example ./my_ls, and make sure that the program is marked as
‘executable’ to run it, with the following command:
$ chmod a+rx my_ls
Page - 58 -