Preston University, Kohat
Islamabad Campus
Department of Computer Science
OPERATING SYSTEMS
Lab Report #______________
Submitted by
Name: ________________________________
Reg. No: _______________________________
BS Computer Science (BSCS)
Submitted to:
Instructor’s Name: ________________________
Lab Date: ___________________ Marks & Signature
Submission Date: ________________
9
LAB NO 03: INTRODUCTION TO BASIC UNIX COMMANDS
Duration 3 Hours
OS /Tool/Language Linux Operating Systems
Objective(s) To get familiar with the basic Unix/Linux shell commands.
Introduction to Basic Shell Commands
GENERAL COMMANDS
Command Function
date Used to display the current system date and time.
date +%D Displays date only
date +%T Displays time only
date +% Y Displays the year part of date
date +% H Displays the hour part of time
cal Calendar of the current month
cal {year} Displays calendar for all months of the specified year
cal {month} {year} Displays calendar for the specified month of the year
who Login details of all users such as their Terminal No, User name, etc.
whoami Used to display the login details of the user
Used to print the file name of the terminal connected to standard input.
tty
tty stands for TeleTYpewriter.
uname Displays the Operating System
uname –r Shows version number of the OS (kernel).
uname –n Displays domain name of the server
echo "txt" Displays the given text on the screen
echo $HOME Displays the user's home directory
bc Basic calculator. Press Ctrl+d to quit
lp {filename} Allows the user to spool a job along with others in a print queue.
history To display the commands used by the user since log on.
man Unix's version of Help files are called man pages.
man {cmdname} Manual for the given command. Press q to exit
exit Exit from a process. If shell is the only process then logs out
10
DIRECTORY COMMANDS
Command Function
pwd Path of the present working directory
A directory is created in the given name under the current
mkdir {dirname}
directory
mkdir
A number of sub-directories can be created under one stroke
{dirname1}{dirname2}
Change Directory. If the subdirstarts with / then path starts from
cd {sub dirname}
root (absolute) otherwise from current working directory.
cd To switch to the home directory.
cd / To switch to the root directory.
cd .. To move back to the parent directory
rmdir {sub dirname} Removes an empty sub-directory.
FILE COMMANDS
Command Function
To create a file with some contents. To end typing press Ctrl+d.
cat >{filename}
The >symbol means redirecting output to a file. (<for input)
cat {filename} Displays the file contents.
cat >>{filename} Used to append contents to a file
cp {srcfilename} Copy files to given location. If already exists, it will be
{desfilename} overwritten
cp –i {srcfilename}
Warns the user prior to overwriting the destination file
{desfilename}
cp –r src des Copies the entire directory, all its sub-directories and files.
mv old new To move/rename an existing file or directory.
mv f1 f2 f3 dir To move a group of files to a directory.
mv –v old new Display name of each file as it is moved.
rm {filename} Used to delete a file or group of files. –i option can also be used
rm * To delete all the files in the directory.
rm –r * Deletes all files and sub-directories
rm –f * To forcibly remove even write-protected files
ls Lists all files and subdirectories (blue colored) in sorted manner.
11
ls {filename/dirname } To check whether a file or directory exists.
ls name* Short-hand notation to list out filenames of a specific pattern.
ls –a Lists all files including hidden files (files beginning with.)
ls –x {dirname} To have specific listing of a directory.
ls -r To list files/directories in reverse order.
ls –R Recursive listing of directory tree.
Long listing showing file access rights (read/write/execute-rwx
ls –l
for user/group/others-ugo).
cmp
Used to compare two files. Displays nothing if files are identical.
{filename1}{filename2}
wc {filename} It produces a statistics of lines (l), words(w), and characters(c).
To sort file in following order
-r Reverse normal order
sort -r -n -nr
-n Sort in numeric order
{filename}
-nr Sort in reverse numeric order
$sort myfile
grep {word-to-lookup} To searches file for line that match a pattern.
{filename} $ grep fox myfile
To change file access permissions
u - User who owns the file
g - Group file owner
o - User classified as other
a - All other system user
+ Set permission - Remove permission r
chmod {u|g|o|a} {+|-}
{r|w|x} {filename} - Read permission
w - Write permission
x - Execute
permission (r=4, w=2, x=1)
chmod 740 file sets all rights for user, read only for groups and
no rights for others.
$ chmod u+wx,g+wx,o+x myscript
12
Selected examples
General Commands:
Command: man
Unix's version of Help files are called man pages. If there is a command name and you are not
sure how to use it, then Man Pages help you out with every step.
Syntax
$man command
Example:
$man pwd
The above command helps you with the complete information about the pwd command. Try it
yourself at your command prompt to get more detail.
You can get complete detail on man command itself using the following command −
$man man
Command: clear
“Clear” is used to clear the full screen of text, perhaps garbage on the terminal.
Examples:
clear
This command clears all text on the terminal.
Command: date
This command is used to print system date and time.
Example:
date
Command: cal
"cal" stands for calendar. It displays the calendar of the current month.
Example:
13
cal
Directory Commands examples:
Command: pwd
"pwd" stands for print working directory. It displays your current position in the UNIX/LINUX
file system.
Example:
pwd
It is simply used to report your current working directory.
Command: mkdir
The "mkdir" command is used to create new directories (sub-directories).
Examples:
mkdir Lab
This command creates a new directory named "Lab" in your current directory. (This example
assumes that you have the proper permissions to create a new sub-directory in your current
working directory.)
mkdir memos letters
This command creates two new sub-directories (memos, letters) in the current directory.
mkdir /home/labb
This command creates a new directory named "labb" in the directory "/home".
Command: rm
The "rm" command is used to remove files and directories. (Warning - be very careful when
removing files and directories!)
Examples:
rm -r labb
14
This command deletes the directory named "labb". This directory, and all of its' contents, are
erased from the disk, including any sub-directories and files.
To remove a single empty directory, type rmdir followed by the directory name or path to the
directory as follows:
rmdir ~/Documents/myfiles
This command will remove the “myfiles” directory located in the “~/Documents” path only if
it is empty. If the directory is not empty, the command will fail and displays the “Directory not
empty” message.
Command: cd, chdir
"cd" stands for change directory. It is the primary command for moving around the file system.
Examples:
cd /home
This command moves you to the "/home" directory. "/home" becomes your current working
directory.
cd /h*
Moves you to the "/home" directory - if this is the only directory matching this wildcard pattern.
cd
Issuing the "cd" command without any arguments moves you to your home directory.
FILE COMMANDS
Command: cat
“cat" is short for concatenate. This command is used to create, view and concatenate files.
Example:
cat>file1
This command creates the file, enter the contents into it and to save the content, press Ctrl+D.
cat file1
This command shows the content of the file.
15
cat file1 file2 > file3
This command combines the contents of the first two files into the third file.
Command: ls
"ls" stands for list. It is used to list information about files and directories.
Example:
ls
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.
Command: mv
The "mv" command is used to move and rename files.
Examples:
mv Lab OSLab
This command renames the file "Lab" to the new name "OSLab".
mv Chapter1 garbage
This command renames the file "Chapter1" to the new name "garbage". (Notice that if
"garbage" is a directory, "Chapter1" would be moved into that directory).
Command: rm
The "rm" command is used to remove files and directories.
Examples:
rm OSLab
This command deletes the file named "OSLab" (assuming you have permission to delete this
file).
16
rm Chapter1 Chapter2
This command deletes the files named "Chapter1", "Chapter2"
rm -r OSLab1
This command will delete the OSLab1(Assume that OSLab1 is a directory).
rm in*
This command deletes all files in the current directory whose filename begins with the
characters "in".
rm *.txt
This command deletes all files in the current directory whose filename ends with the characters
".txt".
Command: cp
The "cp" command is used to copy files and directories. When using the cp command, you
must always specify both the source and destination of the file(s) to be copied.
Examples:
cp /home/Lab1 /home/OSLabs
This command copies the "Lab1" file in "/home" to the directory named "/home/OSLabs".
Command: cat
It displays the content of the file sequentially with no break.
Example:
cat > file1
(”>” is called Redirection Operator)
It will create the file ‘file1’ and you can enter the text there. Then press “Ctrl+d” for saving the
file. If file1 already exists then it over writes the contents of the file1.
cat >>file1
This example appends more text to already existing file. Then press “Ctrl+d” for saving the
file.
17
Command: head
It displays the top part/lines of the file. By default it allows 10 lines, but you can change the
settings of the number of lines to be displayed by using –n.
Examples:
head –25 file1.txt
This example displays the first 25 lines of the file named “file1.txt”.
Command: tail
It displays the last part/lines of the file. By default it allows 10 lines, but you can change the
settings of the number of lines to be displayed by using –n.
Examples:
tail –25 file1.txt
This example displays the last 25 lines of the file named “file1.txt”.
Command: ls -l
This command is use to find permission level of the file.
The permission levels are
“r” Read only
“w” Write
“x” execute
I n case of directory, “x” grants permission to list directory contents.
Command: chmod
Each file in UNIX/LINUX has an associated permission level. This allows the user to prevent
others from reading/writing/executing their files or directories.
If you own a file, you can change its permissions with “chmod”.
Example
18
chmod 7 7 7 filename
user group others
Gives user, group and others r, w, x permissions
chmod 770 filename
· Gives the user read, write and execute.
· Gives group members read and execute.
· Gives others no permissions.
Using numeric representations for permissions:
r = 4; w = 2; x = 1; total = 7
Redirection of Input and Output
Mostly all command gives output on screen or take input from keyboard, but in Linux (and in
other OSs also) it's possible to send output to file or to read input from file.
Command: ls
“ls” command is used to output to screen.
Examples:
ls > file1
This command put output of ls to file1.
It outputs Linux-commands result (output of command or shell script) to file. Note that if file
already exist, it will be overwritten else new file is created.
ls >> file1
This command put output of ls at the end of file1.
It output Linux-commands result (output of command or shell script) to end of file. If file exists,
it will be opened and new information/data will be written to end of file, without losing
previous information/data, and if file does not exist, then new file is created.
ls < file1
This command put contents to command or shell script from the file.
19
It takes input to Linux-command from file instead of key-board.
Command: sort
“sort” is used to sort the contents in the file.
Examples:
sort file1
This command sorts the contents in the file1.
sort < file1 > sort_names
cat sort_names
In above example sort command takes input from “file1” file and output of sort command (i.e.
sort_name) is redirected to sort_name file.
tr "[a-z]" "[A-Z]" < file1 > cap_names
cat cap_names
tr command is used to translate all lower case characters to upper-case letters. It takes input
from “file1” file, and tr's output is redirected to cap_names file.
Lab task(s)
· Verify that you are in your home directory.
· Make a new directory named LABS.
· List the files in the current directory to verify that the directory LABS has been made
correctly.
· Change directories to LABS. Create the file named file1.
· List the contents of the file file1 to the screen.
· Make a copy of the file file1 under the name file2.
· Verify that the files file1 and file2 both exist.
· List the contents of both file1 and file2 to the monitor screen. Then delete the file file1.
Clear the window.
· Rename file2 to thefile. Copy thefile to your home directory. Remove thefile from the
current directory. Copy thefile from your home directory to the current directory.
20
Change directories to your home directory. Remove thefile from your home directory
and from directory LABS. Verify thefile is removed from the directory LABS.
· Remove the directory LABS from your home directory with the following command.
Verify that thefile and LABS are gone from your home directory.
21