Operating System (Lab Manual) : Department of Computer Science COMSATS University Islamabad, Vehari

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Operating System (Lab Manual)

Department of Computer Science


COMSATS University Islamabad, Vehari
EXPERIMENT – 1

AIM: To study the Execution of various file/directory handling commands.


THEORY:

The Linux file system is the structure in which all the information on your computer is stored.
Files are organized within a hierarchy of directories. Each directory can contain files, as well
as other directories.

If you were to map out the files and directories in Linux, it would look like an upside-down
tree. At the top is the root directory, which is represented by a single slash ( / ). Below that is
a set of common directories in the Linux system, such as bin, dev, home, lib , and tmp , to
name a few. Each of those directories, as well as directories added to the root, can contain
subdirectories.

Some of the Linux directories that may interest you include the following:
• /bin - Contains common Linux user commands, such as ls, sort , date , and chmod .
/boot - Has the bootable Linux kernel and boot loader configuration files
(GRUB).
• /dev - Contains files representing access points to devices on your systems. These
include terminal devices ( tty* ), floppy disks ( fd* ), hard disks ( hd* or sc* ), RAM (
ram* ), and CD-ROM ( cd* ). (Applications normally access these devices directly
through the device files, but end users rarely access them directly.)
/etc - Contains administrative configuration files.
• /home - Contains directories assigned to each user with a login account.
• /media - Provides a location for mounting devices, such as remote file systems and
removable media (with directory names of cdrom , floppy , and so on). In Fedora and
RHEL, many removable media are mounted automatically in this directory when the
media is inserted (CD or DVD) or connected (USB pen drives or cameras ).
• /proc - Provides a mechanism for the kernel to send information to processes.
• /root - Represents the root user's home directory.
• /sbin - Contains administrative commands and daemon processes.
• /sys - A /proc -like file system, added with the Linux 2.6 kernel and intended to
contain files for getting hardware status and reflecting the system's device tree as it is
seen by the kernel. It pulls many of its functions from /proc .
• /tmp - Contains temporary files used by applications.
• /usr - Contains user documentation, games , graphical files (X11), libraries (lib), and a
variety of other user and administrative commands and files.
• /var - Contains directories of data used by various applications. In particular, this is
where you would place files that you share as an FTP server ( /var/ftp ) or a Web
server ( /var/www ). It also contains all system log files ( /var/log ). In time, FTP,
HTTP, and similar services will move to the /srv directory to adhere to the Linux
Standards Base ( www.freestandards.org/spec ).

COMMAND:
1: Date Command:
This command is used to display the current data and time.

Syntax:
$date

Linux Syntax to Format Date for Display on Screen

The syntax is as follows for the GNU/date and BSD/date command:


$date + FORMAT
$date + "%FORMAT"
$date + "%FORMAT%FORMAT"
$date + "%FORMAT-%FORMAT"

An operand with a leading plus (+) sign signals a user-defined format string which specifies
the format in which to display the date and time. The following examples are tested on
GNU/Linux, Apple OS X Unix, and FreeBSD unix operating system.

Display date in mm-dd-yy format


Open a terminal and type the following date command:
$ date +"%m-%d-%y"

Date Format Options:

a = Abbreviated weekday.
A= Full weekday.
b= Abbreviated month.
B= Full month.
c= Current day and time.
C= Display the century as a decimal number.
d= Day of the month. D= Day in „mm/dd/yy‟
format h= Abbreviated month day.
H= Display the hour.
L=Day of the year.
m=Month of the year.
M=Minute.
P=Display AM or PM
S=Seconds
T=HH:MM:SS format
u=Week of the year. y=Display
the year in 2 digit.
Y=Display the full year.
Z=Time zone .

To turn on 4 digit year display:


$date +"%m-%d-%Y"

Just display date as mm/dd/yy format:


$date +"%D"

2. Calendar Command:

This command is used to display the calendar of the year or the particular month of calendar
year.

Syntax:
a) $cal
b) $cal <year>
c) $cal <month> <year>

Here the first syntax gives the calendar of current month and command b shoes entire
calendar for given year & the third Syntax gives the calendar of reserved month of that year.

Print the previous month


To print the previous month, the current month, and the next month all on one row, enter:
Syntax:
$cal -3

3. Echo Command:
echo is one of the most commonly and widely used built-in command for Linux bash and C
shells, that typically used in scripting language and batch files to display a line of text/string
on standard output or a file. This command is used to print the arguments on the screen.

Syntax:
$echo <text>
a) Input a line of text and display on standard output Syntax:
$echo <string>
b) Declare a variable and echo its value. For example, Declare a variable of x and assign
its value=10 Syntax:
x=10
$echo the value of variable x= $x
c) Using option ‘\b‘– backspace with backslash interpreter ‘-e‘ which removes all the
spaces in between.
Note: The ‘-e‘ option in Linux acts as interpretation of escaped characters that are
back slashed.
Syntax:
$ echo -e “we \b are \b learning \b Linux \b commands”

d) Using option ‘\n‘ – New line with backspace interpreter ‘-e‘ treats new line from
where it is used.
Syntax:
$ echo -e “we \n are \n learning \n Linux \n commands”

e) Using option ‘\t‘ – horizontal tab with backspace interpreter ‘-e‘ to have horizontal
tab spaces.
Syntax:
$ echo -e “we \t are \t learning \t Linux \t commands”

f) How about using option new Line ‘\n‘ and horizontal tab ‘\t‘ simultaneously. Syntax:
$ echo -e “we \n \t are \n \t learning \n \t Linux \n \t commands”

g) Using option ‘\v‘ – vertical tab with backspace interpreter ‘-e‘ to have vertical tab
spaces.
Syntax:
$ echo -e “we \v are \v learning \v Linux \v commands”

Exercise

Question: How about using option new Line and vertical tab simultaneously.

Syntax:

h) Print all the files/folder using echo command (ls command alternative).
Syntax:
$ echo *
4. Banner Command:

Banner command in Linux is used to print the ASCII character string in large letter to
standard output. It is used to display the arguments in „#‟ symbol.

Syntax:
$banner <arguments> Example
1: Printing “1234567890” in large letters. Syntax:
$banner 1234567890
Example 2: Printing “GeeksforGeeks” in large letters. There are two things:
• First, all the letter will be displayed in Capital letters in standard output.
• Second, only “GEEKSFORGE” will be printed as banner has a default capacity of 10
characters in a word. After that, you have to give space which is shown in further
examples.
Syntax:
$banner GeeksforGeeks

5.’who’ Command:

It is used to display who are the users connected to our computer currently.

Example 1: The “who” command displays the following information for each user currently
logged in to the system if no option is provided.

i) Login name of the users


j) Terminal line numbers
k) Login time of the users in to system
l) Remote host name of the user

Syntax:
$who
$who <options>

Example 2: To display the output with headers.

Syntax:
$who -H

Example 3: To show the last booting date or time or when the system was lastely rebooted.

Syntax:
$who -b

6. ’who am i’ Command:

Display the details of the current working directory.


Syntax:
$who am i

7. ’CLEAR’ Command:

It is used to clear the screen.

Syntax:
$clear

8. ’MAN’ Command:
It help us to know about the particular command and its options & working. It is like “help‟
command in windows.

Syntax:
$man <command name>

9. LIST Command:

It is used to list all the contents in the current working directory.

Syntax:
$ls
$ls – options <arguments>
If the command does not contain any argument means it is working in the Current directory.

Options:
a– used to list all the files including the hidden files.
c– list all the files column wise. d-
list all the directories. m- list the files
separated by commas.
p- list files include „/‟ to all the directories.
r- list the files in reverse alphabetical order.
f- list the files based on the list modification date.
x- list in column wise sorted order.

DIRECTORY RELATED COMMANDS:

Present Working Directory Command:

To print the complete path of the current working directory.

Syntax:
$pwd
CD Command:

To change or move the directory to the mentioned directory.

Syntax:
$cd <directory name>
Example 1. Change from current directory to /usr/local.
Syntax:
$ cd /usr/local
Example 2. Change from current directory to /usr/local/lib using absolute path.
Syntax:
$ cd /usr/local/lib
Example 3. Change from current working directory to /usr/local/lib using relative
path.
Syntax:
:/usr/local$ cd lib
Example 4. (a) Switch back to previous directory where you working earlier.
Syntax:
:/usr/local/lib$ cd –
(b) Change Current directory to parent directory.
Syntax:
:/usr/local/lib$ cd ..

FILE RELATED COMMANDS:

CREATE A FILE:
To create a new file in the current directory we use CAT command.

Syntax: $cat > Filename.


The > symbol is redirector, we use cat command.
Type desired text and press CTRL+D (hold down Ctrl Key and type‘d‘ ) to exit. The text will
be written in test2 file.

DISPLAY A FILE:

To display the content of file mentioned we use CAT command without „>‟ operator.

Syntax: $cat filename.

TO VIEW MULTIPLE FILES


To display the content of multiple files mentioned Syntax:
$cat file1 file2
COPYING CONTENTS:

To copy the content of one file with another. If file does not exist, a new file is created and if
the file exists with some data then it is overwritten.
Syntax:
$cat filename source>> destination filename

USE CAT COMMAND WITH MORE & LESS OPTIONS


If file having large number of content that won’t fit in output terminal and screen scrolls up
very fast, we can use parameters more and less with cat command as show above.

Syntax:
$cat file1 | more
$cat file2 | less

DISPLAY LINE NUMBERS IN FILE


With -n option you could see the line numbers of a file song.txt in the output terminal.
Syntax:
$cat -n filename

DISPLAY $ AT THE END OF FILE


In the below, you can see with -e option that ‘$‘ is shows at the end of line and also in space
showing ‘$‘ if there is any gap between paragraphs. This options is useful to squeeze multiple
lines in a single line.
Syntax:
$cat -e filename

DISPLAY TAB SEPARATED LINES IN FILE


In the below output, we could see TAB space is filled up with ‘^I‘ character.

Syntax:
$cat -T test

COPY THE CONTENTS OF ONE FILE TO ANOTHER FILE.


We can redirect standard contents of a file into a new file or else existing file with ‘>‘
(greater than) symbol. Careful, existing contents of file1 will be overwritten by contents of
test file.
Syntax:
$cat [filename-whose-contents-is-to-be-copied] > [destination-filename]

APPENDING STANDARD OUTPUT WITH REDIRECTION OPERATOR


Appends in existing file with ‘>>‘ (double greater than) symbol. Here, contents of test file
will be appended at the end of test1 file.
Syntax:
$cat test >> test1

REDIRECTING MULTIPLE FILES CONTENTS IN A SINGLE FILE


This will create a file called test3 and all output will be redirected in a newly created file.
Syntax:
$cat test test1 test2 > test3
SORTING CONTENTS OF MULTIPLE FILES IN A SINGLE FILE
This will create a file test4 and output of cat command is piped to sort and result will be
redirected in a newly created file.

Syntax:
$cat test test1 test2 test3 | sort > test4

To sort the contents in alphabetical order in reverse order.


Syntax:
$sort –r <filename>

MOVE Command :
To completely move the contents from source file to destination file and to remove the
source file.
Syntax:
$mv <source filename> <destination filename>

REMOVE Command :
To permanently remove the file we use this command.
Syntax:
$rm <filename>

WORD Command :
To list the content count of no of lines, words, characters.
Syntax:
$wc<filename>

Options :
-c – to display no of characters.
-l – to display only the lines.
-w – to display the no of words.

Home Assignment 1:

1. Which command is used to display the top of the file?


2. Which command is used to remove a directory?
3. Which of the following commands is used to display the directory attributes rather
than its contents?
4. Which command is used to concatenate all files beginning with the string 'emp' and
followed by a non-numeric characters?
5. Which command is used to delete all files in the current directory and all its
subdirectories?

Deadline: 14 February, 2019 (3pm)

You might also like