You are on page 1of 5

CPE 413 OPERATING SYSTEMS

LABORATORY EXERCISES
Name: ___________________________________
Yr. & Sec.: ___________ Lab. No.: 2

Course: ________ Date: ___________


Instructor: _________________________

Exercise 2: Getting Started with UNIX/LINUX


I. OBJECTIVES
1. to know how many and what are the commands available in UNIX that can be used by
the user
2. to know how to check the details of each command
3. to understand some UNIX commands and usage by executing them
4. to understand commands by solving some simple problems
5. to know about any one of UNIX supported editor (vi or pico) by typing a simple C
program.
6. how to compile and execute a C program in UNIX.
II. MATERIALS
PC with Linux/UNIX based Operating System
Ubuntu or similar command line interpreter
III. PROCEDURE
Commands in UNIX
In the prompt, type ls /usr/bin this will display all the commands available in UNIX
system that can be used by the user.
Details about UNIX commands
To check the detail of a command (for example ls command) type man ls where man is
the command which displays the manual pages of the given command
UNIX commands
You interact with the Unix operating system by entering a command at the shell prompt.
The basic form of any Unix command is:
command_nameoptionsargument(s)
Most descriptions for commands such as those given in the On-line Manual use a much
more precise syntax. For example:
cp [-iprR] filename ... directory
This syntax has a few simple rules. Apply them to the command description and you can
understand how the command is to be used.To understand the command syntax apply the
following simple rules:
1. Any options or arguments enclosed in [] square brackets are optional.
2. Anything not enclosed in [] square brackets must be entered.
CPE 413 Operating System Lab Exercises
by: Engr. Jeremiah Dearborn F. Gangoso, CpE

Page 1 of 5

3. Boldface words are considered to be literals and must be typed exactly as they appear.
This usually applies to the command name and command options.
4. Arguments shown in italics must be replaced by whatever it is that they represent. This is
usually the name of a file or directory.
5. Ellipses '...' mean that the previous argument can be repeated any number of times.
Command Options
Options modify the way that a command works. They usually consist of a hyphen
followed by a single letter.
For example the wc command counts the number of words, characters and lines in a file.
By using a different option you can choose what is counted.
wc -w file1
wc -c file1
wc -l file1

counts the words


counts the characters
counts the lines

Using multiple options Some commands accept multiple options. These can be grouped
together after a single hyphen:
command -abc argument
Where are commands located? Unix commands are executable binary files located in
directories with the name bin (for binary). Many of the commands that you use are located in the
directory /usr/bin.
When you enter the name of a command the shell checks to see if it is a built-in
command. If it is not, it looks for the binary file that the command name refers to in each of the
directories that are defined in the PATH environment variable.
Locating a command To find out if a command or utility is available on your system
enter the command:
which command_name
If the command is found its pathname is displayed. The message command_name:
Command not found is displayed when the command cannot be found.
Entering more than one command To enter several commands on the one command line,
use a ; (semicolon) to separate each one from the next. For example:
cd .. ; ls l
This command line contains two commands. The first, cd .. changes the current directory
to the parent directory. The second, ls -l produces a long listing of the contents of the current
directory. If necessary you can continue the commands onto another line.
Creating a command alias Remembering the name of a command and how it is used can
be difficult. Assigning your own name for a command an alias - is very easy.
Assigning an alias is done with the command:alias name definition
For Example:
alias greet echo Hello World
will display Hello World by just entering greet without entering the full command.
CPE 413 Operating System Lab Exercises
by: Engr. Jeremiah Dearborn F. Gangoso, CpE

Page 2 of 5

Displaying value of current aliases To display the value of each alias known to the shell
enter the command:alias
To display the current value for a particular alias use the command:alias name_of_alias
Cancelling a command alias To cancel a command alias during your current login session
use the command:unalias alias_name
Using the command history Each time that you enter a command it is added to a
command history list. You can reuse commands from this list. This command history facility is
not available with the Bourne shell (sh).
History
To display the command history list enter the command:history
This displays a numbered list of commands in the order in which you have used them.Rerunning a command from the history list To run a command from the history list, use a command
from below:
Run the previous command
Run command number n
Run most recent command starting
with characters in string
Run most recent command containing
characters that match string

!!
!n
!string
!?string

By default only the previous command is saved in the command history list. To save a
larger number of commands set the history variable in your shell start-up file. For example:
set history=50
This will keep a history of the fifty (50) most recent commands that you have used.
Redirecting Standard Input And Output
Unix considers any device attached to the system to be a file. And that includes your
terminal! By default, a command treats your terminal as the standard input file from which to
read in information. Your terminal is also treated as the standard output file to which information
is sent from the command. This action can be changed by redirecting standard input and standard
output from and to any other file.
Redirecting standard input To redirect the standard input for a command use the < (less
than) character followed by the name of the input file. For example:
mail tony < memo
This redirects the standard input to the mail command so that it comes from the file
memo. The effect of this is to mail the contents of this file to the user tony.
Redirecting standard output To redirect the standard output from a command use the >
(greater than) symbol followed by the name of the output file. If the file that you redirect
standard output to does not already exist it will be created. For example:
grep Smith /etc/passwd > popular

CPE 413 Operating System Lab Exercises


by: Engr. Jeremiah Dearborn F. Gangoso, CpE

Page 3 of 5

This redirects the standard output from the grep command so that it goes to the file
popular. This file will contain all occurrences of the string Smith that were found in the
/etc/passwd file.
Appending standard output to a file .To append the standard output from a command to a
file use two >> (greater than) symbols followed by the name of the file. If the file does not exist
it is created. For example:
cat part1 > chapt2
cat part2 >> chapt2
This creates a file called chapt2 with the same contents as part1. It then reads the contents
of part2 and appends them to the file chapt2. The file chapt2 now contains the data from part1
followed by the data from part2.
Connecting commands together Unix allows you to link two or more commands together
using a pipe. The pipe takes the standard output from one command and uses it as the standard
input to another command.
command1 | command2 | command3
The | (vertical bar) character is used to represent the pipeline connecting the commands.
With practice you can use pipes to create complex commands by combining several simpler
commands together.
To pipe the output from one command into another command:
who | wc -l
This command tells you how many users are currently logged in on the system. The
standard output from the who command - a list of all the users currently logged in on the system
- is piped into the wc command as its standard input. Used with the -l option this command
counts the numbers of lines in the standard input and displays the result on the standard output.
Some Other Useful Unix Commands To Practice
Try the following commands and observe the output. Try the option in each command.
date - Displays the date and time
cal Displays the current month calendar
echo Welcome to UNIX - Displays the text typed next to it . echo
$LOGNAME - Displays the variable value.
banner Welcome- Displays the text Welcome as a banner
who - Displays information about all users who are currently logged into the system.
who am i - Displays information about your login.
pwd - Displays the current working directory
id - Displays userid and groupid.
finger - Displays information about a specified userid if user id is specified otherwise it gives
information about all currently logged users.
tty - shows special file that represents your terminal.
du - Displays disk storage usage statistics.
clear - Clears the contents of the screen
whatis - Display a one-line summary about a keyword.
which - Locate a command; display its pathname or alias.
cat /etc/profile - Displays the content of the given file profile which is in the directory /etc
pg /etc/profile - Displays the content of the given file profile which is in the directory /etc page
wise
Page 4 of 5
CPE 413 Operating System Lab Exercises
by: Engr. Jeremiah Dearborn F. Gangoso, CpE

Try the following commands also:


passwd
hostname
head
expr
df
ftp
vi

alias
cat
tail
Kill
ulimit
ls
lp

cut
cat >
wc
ps
compress
at

find
touch
bc
jobs
diff
sleep

whereis
sort
Grep
login
park
cd

IV. PROBLEMS/EXERCISES
After you have practiced with the above commands, try to solve the following problems
using the UNIX commands. Show correct commands at the back of paper.
1. Find the number of words in 3rd line of a given file.
2. List the number of files starting with the letter C.
3. List the FULL NAMEs of all the users currently logged in. ans. WHO
4. Save the output of the command (from problem 3) into a file. Ans.
6. Append the output of a command into a file.
7. Find the size of a given file.

CPE 413 Operating System Lab Exercises


by: Engr. Jeremiah Dearborn F. Gangoso, CpE

Page 5 of 5