[go: up one dir, main page]

0% found this document useful (0 votes)
19 views4 pages

CO222 Lab 00 - Introducing UNIX - 2

Uploaded by

e21200
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views4 pages

CO222 Lab 00 - Introducing UNIX - 2

Uploaded by

e21200
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab 00 - Introducing UNIX

Department of Computer Engineering, University of Peradeniya


December 02, 2024

1 UNIX and Linux


All labs for this course are usually conducted on Linux. Linux is a member of the UNIX family of operating
systems. UNIX was first developed in the ’70s, and has a long history. Today versions of UNIX run on
everything from supercomputers to smartphones.
Linux is a modern UNIX that has several nice GUIs. The lab system at the Department of Computer
Engineering run the XFCE desktop which is similar to Microsoft Windows.

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.

2.2 Getting Help


To get help on a command use the command man (manual.) To show the manpage for date for example do
man date. You can use the arrow keys and and page up/down to scroll through the text, and q to quit.

3 Files and Directories


All of the data you save to disk such as your programs are written to files. Files are organised into directories
("folders".) When you open a new shell you will start in your home directory, which is where you save your
personal data. Type the command ls (list) to see the files in your current directory.

Exercise 3: Try the command ls -l. What additional details do you see in the output?

3.1 Command Options and Arguments


The -l in the above command is called an option. Each UNIX command may have many options that
changes what the command does in various ways.

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.

Exercise 5: List the following directories using ls

1. /home/e21xxx (replace "xxx" with your login.)

2. / ("root directory")
3. List the /dev directory with the -l option. Is there anything different in the output compared to other
directories?

3.2 Navigating the Directory Hierarchy


Directories in UNIX are organised in a hierarchical tree structure. The top of the hierarchy called the root
directory is denoted by / (forward slash. )1
You may use the pwd (present working directory) command to find out in which directory you are. The
output shows you the absolute path starting from the directory root. The cd (change directory) command
is used to switch to another directory.

Exercise 6: Switch to the following directories using cd. Verify that you are in the right directory using
pwd, after each one.

1. /home/e21yyy, where e21yyy is the login of your neighbour.

2. What directory does cd ∼ (tilde) take you to?

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?

3.3 Relative and Absolute Paths


There are two ways to refer to a particular file or directory in UNIX. You have already seen how to give an
absolute path starting from the root/. You can also refer to a file or directory using a relative path. If you
do ls -a (list all) in any directory you will see two "special directories" denoted by . and, ...
The directory . denotes the directory itself, while .. indicates the current directory’s parent - that is,
the directory one level up. You can use . and .. to specify directories relative to your current directory. For
example, if you do cd ../e21yyy (where e21yyy is your friends’s login), you will end up in the absolute path
/home/e21yyy

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 /.

1 Unlike Windows there are no "drives" such as C: in UNIX.

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 10: Write commands to list the following patterns

1. All exectuables beginning with the letter x under /usr/bin.


2. All files under /dev that begin with tty and end with a digit.

4.1 Copying, Moving and, Deleting Files


The cp, mv and rm commands can be used to copy, move and delete files, respectively. Both cp and mv
require two arguments - the source and target.

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.

5 Users and Groups


UNIX is a multiuser system designed to run on large machines on which multiple users could work at the
same time. Each user on a system has a login name and belongs to one or more groups. Groups determine
what you are allowed to do
on the system, for example, what files you are allowed to access. being in the audio group gives you
permission to play and record sound.

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.

5.1 Ownership and Permissions


Each file and directory on UNIX records the uid and gid of the owner. The owner of a file or directory can
do anything, like modify, delete or rename it.

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.

You might also like