CO222 Lab 00 - Introducing UNIX - 2
CO222 Lab 00 - Introducing UNIX - 2
2 The Shell
Although you can do common tasks using the GUI, the real power of UNIX lies in the command-line or
shell. To access the shell click on "Terminal Emulator" under the "Accessories" menu. You should see a
empty window with prompt similar to e21xxx@ce.
The prompt indicates that the shell is waiting for you to enter a command. You can try the command
date. Once you press "enter" the shell will execute the command and show you its output. For example,
type the command date to output the current date and time.
Because you will be not working on this exercise from the lab, but, at your home, first login to a Linux
server at the Department. <Click here> to learn how to log in to a department server.
Exercise 1: Is the time shown by date on your server correct? If not can you guess why it isn’t correct?
2.1 Auto-completion
A very useful feature of the shell is auto-completion. If you type the first part of a command, file name or
directory path and press the tab key, the shell will automatically complete it for you. Try it by typing dat
and pressing tab.
Exercise 2: What happens if you type da and press tab? Try pressing tab twice.
Exercise 3: Try the command ls -l. What additional details do you see in the output?
1
Exercise 4: Do man ls to find out what other options the ls command has. What option to sorts the
output of ls by file size?
We can get ls to list the contents of other directories by giving it an argument. For example, ls /bin
shows you the files in the directory /bin which contains the some UNIX command executables.
2. / ("root directory")
3. List the /dev directory with the -l option. Is there anything different in the output compared to other
directories?
Exercise 6: Switch to the following directories using cd. Verify that you are in the right directory using
pwd, after each one.
Use the mkdir command to create the following directories in your home (You should practice the habit of
creating a separate directory for each lab).
1. CO222
2. CO222/lab00
Is there an option for mkdir that you can use to create the hirerachy CO222 with a single command?
Exercise 7: Starting in you home directory, note down your working directory after each of the following
commands
1. cd ...
2. cd ../var/log
3. cd /.
2
4 Shell Patterns
Suppose you want to create three directories lab1, lab2 and lab3 in your home. You can use a single com-
mand mkdir lab1 lab2 lab3 to do so. Similarly, most UNIX commands can accept multiple arguments
such as files or directories.
Exercise 8: List the following files under /var/log using a single command
1. syslog
2. dmesg
3. kern.log
Sometimes we need to operate on a group of files sharing a common naming pattern, such as files end-
ing with the extension .log which that record various events in UNIX2 The command ls /var/log/*.log
will list all files ending in . log in the directory /var/log. The wildcard ∗ tells the shell to match any string
of characters in the file name.
Exercise 9: The command is /var/log /*.log does not list the file /var/log/syslog. Why?
The shell provides several other patterns. We can give a range of characters by enclosing them in brackets.
For example, the pattern [a-z] matches all lowercase letters.
Exercise 11:
1. Copy a file from /var/log to your home directory. (Hint: the target to cp should be the home
directory.)
2. What option to cp lets you copy a directory?
3. Try to remove a file under /var/log.
2 Unlike Windows, UNIX files may have an extension of any length, or no extension at all
3
Exercise 12:
1. Use the following commands to explore your login name and groups: whoami, groups
2. Note the numbers that UNIX uses internally to represent your login name and groups using the id
command. These are called your uid and gid respectively.
3. Use id to find out what groups the user root belongs to. root is the system administrators account
which has the rights to do anything on the system (do not confuse the root user with the root directory
/).
4. Use the getent command to list all the users and groups on the system.
Exercise 13:Do ls -1 in your home and note the uid and gid of the owner of each file, shown in columns
3 and 4 .
In the first column of 1s -1 output you will see three sets of of permission. A file can has three kinds
of permissions read write and execute denoted by r, w, x respectively. The r and w permissions define
who is allowed to read and modify the file. The x permission indicates that the file is executable. Each of
these set of permissions can be give to the file’s owner, those in the same group, and everyone else ("others".)
Exercise 14: Use the chmod command to experiment with changing permissions for owner, group and others.
Read the manpage for details. Create empty files called e21yyy_test1, e21yyy_test2 and e21yyy_test3
in temporary directory (to change the directory use cd /tmp command) for these exercises (yyy is your
registraion number).
1. Provide write permissions to a file in your directory to your neighbour by doing chmod g+w
e21yyy_test1 Verify that you can write to the file your neighbour gave you write access to.
2. Remove read permissions for owner by doing chmod u-r e21yyy_test2. What happens when you try
to open the file?
3. Set executable permissions on a text file e21yyy_test3. What happens if you do ./e21yyy_test3?
4. Create a directory and remove execute permissions on it. What is the effect?
5. Remove all the files and the directories that you have created (hint use rm command).
6 Submission
After you complete the exercises, prepare a document including your answer scripts with related screen-
shots and submit a .pdf file named E21XXXLab00.pdf via given link on FEeLS. Note: Here XXX is your
registration number. Also, inform to the respective instructor you have done with the lab sheet and get
marked.