Os Lab 02
Os Lab 02
TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Operating Systems
Experiment 2
Implementation of LINUX Commands -I
(Linux)
Shell a program that takes the commands you type and translates them into instructions to the
operating system.
File: Under most operating systems (including Linux), there is the concept of a file, which is
just a bundle of information given a name (called a filename). Examples of files might be
your history term paper, an e-mail message, or an actual program that can be executed.
Essentially, anything saved on disk is saved in an individual file.
Directory: With the concept of files comes the concept of directories. A directory is a
collection of files. It can be thought of as a “folder'' that contains many different files.
Directories are given names, with which you can identify them. Furthermore, directories are
maintained in a tree-like structure; that is, directories may contain other directories.
Current working directory: At any moment, commands that you enter are assumed to be
relative to your current working directory. You can think of your working directory as the
directory in which you are currently ``located''. When you first log in, your working directory
is set to your home directory--/home/me.
Command: cat
General Syntax
# cat file1/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
narad:x:500:500::/home/narad:/bin/bash
In below example, it will display contents of test and test1 file in terminal.
Hello everybody
Hi world,
We will create a file called test2 file with below command using “>” overwrite operator.
Awaits input from user, type desired text and press CTRL+D (hold down Ctrl Key and type
‘d‘) to exit. The text will be written in test2 file. You can see content of file with following
cat command.
Note: if you write again in this file using “cat > test2” command then previous content will be
overwritten. To append at the end of previously written content use “>>” append operator.
# cat test2
If file having large number of contents 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. press “q”
to quit scrolling.
With -n option you could see the line numbers of a file song.txt in the output terminal.
# cat -n song.txt
In the below, you can see with -e option that ‘$‘is shown at the end of line and also in space
showing ‘$‘for gap between paragraphs. This options are useful to squeeze multiple lines in
a single line.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY,
TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
# cat -e test
In the below output, we could see TAB space is filled up with ‘^I‘ character.
# cat -T test
In the below example we have three files test, test1 and test2 and able to view the contents of
those file as shown above. We need to separate each file with ; (semi colon).
We can redirect standard output of a file into a new file else existing file with ‘>‘ (greater
than) symbol. Careful, existing contents of test1 will be overwritten by contents of test file.
Appends in existing file with ‘>>‘ (double greater than) symbol. Here, contents of test file
will be appended at the end of test1 file.
When you use the redirect with standard input ‘<‘ (less than symbol), it use file name test2
as a input for a command and output will be shown in a terminal.
This will create a file called test3 and all output will be redirected in a newly created file.
2. Command: pwd
Description: "pwd" stands for print working directory. It displays your current position in the
LINIX file system.
Examples:
pwd
It is simply used to report your current working directory.
4. Command: ls
Description: "ls" stands for list. It is used to list information about files and directories.
Examples:
ls
This is the basic "ls" command, with no options. It provides a very basic listing of the files in
your current working directory. Filenames beginning with a decimal are considered hidden
files, and they are not shown.
ls -al
This command provides a long listing of information about all files in the current directory.
This is probably the most used version of the ls command.
ls -a
This command provides a listing of information about all hidden files and directories in the
current directory whose name begins with a dot.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY,
TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
4. Command: mv
Examples:
mv Chapter1 garbage
This command renames the file "Chapter1" to the new name "garbage".
5. Command: rm and rm -r
Description:
Examples:
rm Chapter1.bad
This command deletes the file named "Chapter1.bad"assuming you have permission to delete
this file).
This rm-i command prompt you before deleting any of the three files specified. The -i option
stands for inquire. You must answer y (for yes) for each file you want to delete. This can be a
safer way to delete files.
rm *.html
This command deletes all files in the current directory whose filename ends with the characters
".html".
rm index*
This command deletes all files in the current directory whose filename begins with the
character’s "index".
rm -r new-novel
This command deletes the directory named "new-novel”. This directory, and all of its contents,
are erased from the disk, including any sub-directories and files.
6. Command: cp
Description: The "cp" command is used to copy files and directories. Note that when using
the cp command, you must always specify both the source and destination of the file(s) to be
copied.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY,
TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Examples:
cp -r file1 Lab2
This command copies your file “file1” to a directory named “Lab2”.
7. Command: mkdir
Examples:
mkdir tmp
This command creates a new directory named "tmp" in your current directory. (This example
assumes that you have the proper permissions to create a new sub-directory in your current
working directory.)
This command creates three new sub-directories (memos, letters, and e-mail) in the current
directory.
mkdir –p parent/child/customer/acme
This command creates a new directory named parent/child/customer/acme, and creates any
intermediate directories that are needed.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY,
TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
8. Command: rmdir
Description: The "rmdir" command is used to remove directories. (Warning - be very careful
when removing directories!)
Examples:
rmdir Lab2
This command deletes the directory named "Lab2" (assuming you have permission to delete
this directory).
~ files with this symbol are backup files. We can recover them.
Command: clear
Clear the Window
Command: exit
Exit a shell
UNIVERSITY OF ENGINEERING AND TECHNOLOGY,
TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Lab Task