Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

CSEN3113 – OS Lab Assignments-Day1

Day1  Lessons
Self Assessment with General Purpose Utilities in UNIX-like Systems
1. What is a directory?
A directory contains no data, but keeps some details of the files and subdirectories that it
contains. A directory contains the filename and not the file’s contents. If we have 20 files
in a directory, there will be 20 entries in the directory. Each entry has two components 
the filename and unique identification number (inode).

2. Significance of HOME variable: The Home Directory.


When you log on to the system, Linux automatically places you in a directory called the
home directory. It is created by the system when a user account is opened. If you log in
using the login name gr3, you’ll land up in a directory having the path name /home/gr3.

$ echo home = $HOME

3. Operations on directories:
a. Check the current directory.  pwd
b. Change the current directory.  cd
c. Create new directory (s).  mkdir command is followed by names of the
directories to be created.
$ mkdir asgn asgn/a asgn/a/h asgn/a/g asgn/b asgn/c asgn/c/d asgn/c/d/e
d. Remove directory (s).--> removes directory
$ rmdir asgn asgn/a asgn/a/h asgn/a/g asgn/b asgn/c asgn/c/d asgn/c/d/e
4. Absolute pathnames, Relative pathnames. 

Command: oslab/a/g$ cat f1 will work only if f1 exists in your current directory, i.e., g.
If you are placed in : oslab and want to access f1 in : oslab/a/g,

you have to use : oslab$ cat a/g/f1 (two levels down). This fully qualified pathname is called absolute
pathname.
Relative pathnames: : From oslab/c/d/e$ if I want to move up to oslab  cd ../../..

Page 1 Aug-21
CSEN3113 – OS Lab Assignments-Day1

5. What is a command?
It is an instruction given to the OS, interpreted by SHELL.

Commands → Internal and external commands.


Since ls is a program or file having an independent existence in /usr/bin directory, its considered as external command.

Most commands are external in nature, but there are some which are not really found anywhere, and some which are
normally not executed even if they are in one of the directories specified by PATH.

We are talking about internal commands:


:oslab$ type echo
echo is a shell builtin

echo isn’t an external command, because, when we type echo, the shell won’t look in its PATH to locate
it (the command file is there in /bin). The shell will execute it from its own set of built-in
commands that are not stored as separate files. These built in commands are internal commands.

External commands: cp, cd, mkdir, rm, ls, ps etc.

6. What is ls Command?
ls → listing of files and directories, i.e., the file names under current directory...
:oslab$ ls → too little information is coming
:oslab/b$ ls -alh
total 20K
→ The word "total" is followed by the number of file system blocks that the directory's files occupy. → using the
-h option together with -l this will have the output in k,M,G for a better understanding.
drwxrwxr-x 2 user user 4.0K Jul 5 16:15 .
drwxrwxr-x 5 user user 4.0K Jul 4 15:32 ..
-rw-rw-r-- 1 user user 3.2K Jul 5 16:15 f1
-rw-rw-r-- 1 user user 3.2K Jul 5 16:15 f2
-rw-rw-r-- 1 user user 3.2K Jul 5 16:15 f3

Meaning of each entry under total 20K:


directory identifier d or - is directly followed by a set of nine letters and hyphens. They disclose the item's security
permissions. The first three permissions apply to the file's owner, and the next set pertains to members of the owner's
group. The final three characters identify access permissions for all other users.
A user or group is allowed to read an item if its permissions contain the letter "r." The letter "w" indicates that someone can
modify the file by writing to it. An "x" means that users have permission to run the file if it's an executable program.
Hyphens appear when permissions have not been granted.
The next item in each ls -l listing is the number of links. A file typically has one link, but there are more links if it has aliases.
The count starts at two for folders because UNIX considers the parent and current directories to be links. This figure
includes the number of subdirectories in a folder as well.
The LUNIX ls -l command's listings also indicate the owner and group assigned to a file. You can use this data in conjunction

Page 2 Aug-21
CSEN3113 – OS Lab Assignments-Day1

with the permission flags to determine what access rights you have. The file size follows the group name; it is
measured in bytes. Regardless of how many files it contains, the size of a directory is normally 4096 bytes.
A calendar date appears after the file size. It may also include the year or a specific time. This is the date when the item was
last modified;
:oslab$ ls -lt → Filenames sorted by last modification time
:oslab$ ls -lu→ Filenames sorted by last access time
:oslab$ ls -li→ Filenames sorted by inode number
:oslab$ ls –x  output in multiple columns
:oslab$ ls –Fx  Identifying Directories and executables (-F)
:oslab$ ls –axF  Showing Hidden files also (-a)
:oslab$ ls -lR → Recursive file list

7. How to switch Directories?  change directory command by oslab$cd a/g/f1.txt

8. How to display the user currently working on the system? This means whats going on around
you  Who are you? Commands like  whoami, who am i., id. Question may also be asked 
Finding out what other users are logged in to the system. Who else is there? Commands  users,
who, who -Hu, w.

9. How to display the name and version of your operating system?  The uname command
displays certain features of the os running on your machine. :~/Desktop/oslab$ uname
:oslab$ uname –r  the current release
:oslab$ uname –n  shows host name or the complete domain name
Knowing your terminal: Linux treats terminals as files.
:oslab$ tty
/dev/pts/10  a file named 10 is resident in pts directory.

10. How to display the calendar of a month or year?


:oslab$ cal
:oslab$ cal 07 2019
:oslab$ cal 2012 | more

11. How to display the current system date and time in a variety of formats?
oslab$ date  displaying system date
oslab$ date +%m  only the month
oslab$ date +%h  month name
oslab$ date +”%h %m”  combine July 07 in one command
Other formats:
d  day of the month (1 to 31)
y  the last two digits of the year
H, M and S  hour, minute and second
D  The date in the format mm/dd/yy
T  The time in the format hh:mm:ss

Page 3 Aug-21
CSEN3113 – OS Lab Assignments-Day1

12. How to use echo to display a message on the terminal?


oslab$ read = 60
oslab$ echo read =$read
oslab$ echo logname= $LOGNAME, home= $HOME
oslab$ gedit f1 
Hello CSEN2253-Gr1
echo logname= $LOGNAME, home= $HOME
oslab$ls –l f1  check execute permission is there or not
oslab$chmod u+x f1
oslab$ls –l f1  check creation of executable
oslab$./f1  run executable
(if students are interested, show shell script displaying command line arguments)

Handling Ordinary Files:


The File System:
1. The File. What’s in a (File) name?  On most unix systems today, a filename can consists of up to
255 characters!! Files may or may not have extensions, and can consist of any ASCII character except
the / and NULL character (ASCII value 0).
Example of valid filenames in UNIX: try ‘ .’ or ‘..’ or ‘…’, ‘.gr1’, ‘gr1.’, ‘^v^b^d-++bcd’, ‘-{}[]’, ‘@#$%*ab67’,
‘a.b.c.d.e’

2. Display, create, copy, move, delete, and rename file.


Create a file with cat:
oslab$cat f1  display the contents of a small file on the terminal
oslab$cat f1 f2  cat concatenates two files
oslab$cat > fun  using cat to create file
Hello CSEN2253, $LOGNAME, check if the file can read variables given in shell [ctrl -d]
oslab$ [prompt returns]
oslab$ cat fun  we call “cat” this file fun
cp: Copying a file
oslab$ cp f1 f2  the first is copied to the second
oslab$ cp f1 f2 fun c/d/e  copying f1, f2 and f3 to a specific directory
cp options
oslab$ cp –i f1 f2  interactive copying (-i)

3. wc: Counting Lines, Words and Characters of file(s).


oslab$cat f1
oslab$wc f1  wc without options to make a “word count” of the data in the file
9 20 199 f1  wc counts 9 lines, 20 words and 199 characters.
oslab$ wc –l f1  show number of lines
oslab$ wc –w f1  show number of words
oslab$ wc –c f1  show number of characters
oslab$ wc f1 f2 f3  with multiple filenames, wc produces a line for each file, as well as a total count.

Page 4 Aug-21
CSEN3113 – OS Lab Assignments-Day1

4. cmp: Comparing Two Files.


oslab$ cmp f1 f2
oslab$ cmp –l f1 f2

user@user-HP-Pro-3330-MT:~/D/oslab/c$ cat > f1


echo my home directory is $HOME [ctrl D]
user@user-HP-Pro-3330-MT:~/D/oslab/c$ cat > f2
echo my memo directory is $HOME [ctrl D]
user@user-HP-Pro-3330-MT:~/D/oslab/c$ chmod u+x f1
user@user-HP-Pro-3330-MT:~/D/oslab/c$ chmod u+x f2
user@user-HP-Pro-3330-MT:~/D/oslab/c$ ls -lah
user@user-HP-Pro-3330-MT:~/D/oslab/c$ cat f1
f2
user@user-HP-Pro-3330-MT:~/D/oslab/c$ cmp f1 f2
f1 f2 differ: byte 6, line 1
user@user-HP-Pro-3330-MT:~/D/oslab/c$ cat f1
echo my home directory is $HOME
user@user-HP-Pro-3330-MT:~/D/oslab/c$ cat f2
echo my home Directory is $HOME
user@user-HP-Pro-3330-MT:~/D/oslab/c$ cmp f1 f2
f1 f2 differ: byte 14, line 1

5. comm: What is Common between two files? Compare two sorted files line-by-line.

user@user-HP-Pro-3330-MT:~/Desktop/oslab/c$ gedit f3 user@user-HP-Pro-3330-MT:~/Desktop/oslab /c$ gedit f4


Eggs White Sugar
Brown Sugar Brown Sugar
Salt Salt
White Sugar Chocolate Chips
Bread Milk
Chocolate Chips Bread
Milk Eggs

user@user-HP-Pro-3330-MT:~/Desktop/oslab/c$ comm f3 f4
Eggs
comm: file 1 is not in sorted order

user@user-HP-Pro-3330-MT:~/Desktop/oslab/c$ sort f3 > f31


user@user-HP-Pro-3330-MT:~/Desktop/oslab/c$ sort f4 > f41
user@user-HP-Pro-3330-MT:~/Desktop/oslab/c$ comm f31 f41
Check the output....

Page 5 Aug-21
CSEN3113 – OS Lab Assignments-Day1

How to display the user currently working on the system?


Simulation of multitasking and multiuser environment with non-GUI terminals and our own GUI
terminal. Usage of Ctrl-Alt-F1, F2, F3, F4, F5, F6 keys.... use the (login, pswd) combinations like (gr1,
gr1), (gr2, gr2), (gr3, gr3), (gr4,gr4), (gr5,gr5), (gr6, gr6) → come out from /tty(s) using Ctrl-Alt-F7 →
Check with $who -Hu and $w commands:
user@user-HP-Pro-3330-MT:~/Desktop/oslab$ who -Hu
NAME LINE TIME IDLE PID COMMENT
user tty4 2018-07-10 09:08 . 2604
user tty5 2018-07-10 09:08 . 2687
user tty2 2018-07-10 09:07 00:01 2437
user tty3 2018-07-10 09:08 . 2520
user tty6 2018-07-10 09:08 . 2774
user tty1 2018-07-10 09:07 00:01 2389
user :0 2018-07-10 09:04 ? 1600 (:0)
user pts/7 2018-07-10 09:05 . 2148 (:0)

user@user-HP-Pro-3330-MT:~/Desktop/oslab$ w
09:16:39 up 18 min, 8 users, load average: 0.00, 0.06, 0.10
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
user tty4 09:08 8:23 0.10s 0.06s -bash
user tty5 09:08 8:07 0.09s 0.06s -bash
user tty2 09:07 9:03 0.10s 0.06s -bash
user tty3 09:08 8:31 0.12s 0.06s -bash
user tty6 09:08 7:59 0.09s 0.06s -bash
user tty1 09:07 9:19 0.08s 0.06s -bash
user :0 :0 09:04 ?xdm? 17.70s 0.10s init --user
user pts/7 :0 09:05 7.00s 0.03s 0.00s w

Page 6 Aug-21

You might also like