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
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;
Directories may contain other directories.
Command: cat
General Syntax
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
# 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,
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.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY,
TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
# 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.
# cat -e test
In the below output, we could see TAB space is filled up with ‘^I‘ character.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY,
TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
# cat -T test
In the below example we have three files test, test1 and test2.
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.
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.
existing contents of test3 will be overwritten by contents of test,test1 and test2 files.
Command: pwd
Syntax
pwd [OPTION]
4. Command: ls
Syntax
ls [options] [names]
"ls" stands for list. It is used to list information about files and directories.
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.
4. Command: mv
Syntax
UNIVERSITY OF ENGINEERING AND TECHNOLOGY,
TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Examples
mv Chapter1 garbage
This command renames the file "Chapter1" to the new name "garbage".
5. Command: rm and rm -r
Examples:
rm Chapter1.bad
This command deletes the file named "Chapter1.bad"assuming you have permission to
delete this file).
This command deletes the files named "Chapter1", "Chapter2", and "Chapter3".
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
Syntax:
cp [options] file1 file2
cp [options] files directory
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
Syntax
mkdir [options] directory name
Description
The "mkdir" command is used to create new directories (sub-directories).
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
8. Command: rmdir
Syntax
rmdir [options] directories
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
Command: exit
Exit a shell
UNIVERSITY OF ENGINEERING AND TECHNOLOGY,
TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Lab Task