0% found this document useful (0 votes)
32 views

CS312 Operating Systems: Usman Institute of Technology

This document provides instructions for completing Lab 1, which involves executing common Linux commands. It begins by listing the objective of the lab and providing spaces for student and instructor details. Next, it provides background information on Linux commands, input/output redirection, command options, and how to execute commands. It then describes and provides syntax for several common commands: su, pwd, cd, ls, more, less, find, and grep. It concludes by explaining how to use manual ("man") pages to view documentation for commands.

Uploaded by

ali ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

CS312 Operating Systems: Usman Institute of Technology

This document provides instructions for completing Lab 1, which involves executing common Linux commands. It begins by listing the objective of the lab and providing spaces for student and instructor details. Next, it provides background information on Linux commands, input/output redirection, command options, and how to execute commands. It then describes and provides syntax for several common commands: su, pwd, cd, ls, more, less, find, and grep. It concludes by explaining how to use manual ("man") pages to view documentation for commands.

Uploaded by

ali ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab No 1

USMAN INSTITUTE OF TECHNOLOGY

Department of Computer Science


CS312 Operating Systems

Lab#1

Objective:

1. Executing some of the most frequently used Linux commands.

Name of Student:

Roll No: Sec.

Date of Experiment:

Marks Obtained/Remarks:

Signature:
Lab No 1

THEORY

A Linux command is any executable file. This means that any executable file added to the system
becomes a new command on the system. A Linux command is a type of file that is designed to be run,
as opposed to files containing data or configuration information.

Input and Output Redirection

Linux allows to "pipe" the output from a command so that it becomes another command's input. This is
done by typing two or more commands separated by the | character. The | character means "Use the
output from the previous command as the input for the next command." Therefore, typing
command_1|command_2 does both commands, one after the other, before giving you the results.
Another thing you can do in Linux is to send output to a file instead of the screen. To send output to a
file, use the “>” symbol. There are many different reasons why you might want to do this. You might
want to save a "snapshot" of a command's output as it was at a certain time, or you might want to save
a command's output for further examination. You might also want to save the output from a command
that takes a very long time to run, and so on.

Command Options & other parameters

You can use command options to fine-tune the actions of a Linux command. Instead of making you
learn a second command, Linux lets you modify the basic, or default, actions of the command by using
options. Linux commands often use parameters that are not actual command options. These parameters,
such as filenames or directories, are not preceded by a dash.

Executing a Linux Command

From the command prompt simply type the name of the command:
$ command
Where $ is the prompt character for the Bourne shell.

Or if the command is not on your path type the complete path and name of the command such as:
$ /usr/bin/command.
Some of the frequently used Linux commands are: su, pwd, cd, ls, more and less,
find and grep, man. (Refer to Appendix B for detail of few commands)

1. su
Description: "su" stands for "super user". Runs a new shell under different user and group IDs. If no
user is specified, the new shell will run as the root user.
Syntax: su [-flmp] [-c command] [-s shell] [--login] [--fast] [--preserve-environment] [-
command=command] [--shell=shell] [-] [user]
(See the man pages for the description of the flags and options by using man su)
Lab No 1
$ su <username> (to become another user) or
$ su (to become the root user).

Adding a new user

#useradd user1
#passwd user1
<password>

2. pwd

Description: Displays the name of the current directory.pwd stands for present working directory. By
typing this command you are informed of which directory you are currently in.

Syntax: pwd

3. cd

Description: Changes the current directory to any accessible directory on the system.

Syntax: For instance to change from /home/user1 to a subdirectory of user1 wordfiles use the following:
$ cd wordfiles
$ pwd
/home/user1/wordfiles
To change to /tmp use the following:
$ cd /tmp
$ pwd
/tmp

4. ls

Description: Displays the listing of files and directories. If no file or directory is specified, then the
current directory’s contents are displayed. By default the contents are sorted alphabetically.

Syntax: To view the contents of user1 home directory use this:


$ ls

To list the contents of any directory on the system use:


$ ls /usr

(See the man pages for the description of the flags and options by using man ls)
5. more
Lab No 1
Description: Displays one or more files screen by screen and allows for searching and jumping to an
arbitrary location in the file.

Syntax: more [-dlfs] [-number] [+number] [file….]


(See the man pages for the description of the flags and options by using man more)

For example if there is a large text file called “textfile”, it can be viewed a page at a time with the given
command: $ more textfile
After pressing Enter, the first screen of the file is seen with the text -More- displayed on the last line of
the screen. Pressing the space bar jumps forward a full screen length, while pressing enter key moves
forward one line at a time. When the end of the body of the text is reached, the command prompt appears.
To search forward through a file, enter slash followed by the word or phrase you want to search for and
then press enter. The display jumps forward to the first occurrence of the word or phrase being searched
for and displays the occurrence near the top of the screen. The same search can be repeated by entering
n after the first search, avoiding the need to type the same word or phrase repeatedly.

6. less

Description: Displays a text file one screen at a time while allowing searching and backward scrolling.

Syntax: less [-aeEGiINrsS] file….. (See the man pages for the description of the flags and options by using man less)
This command is an improved version of the previous one. On addition to the functions previously
described the following are some of the other actions that can be performed using less:
• Jumping directly to a line.
• Jumping directly to the beginning or end of file.
• Moving backward through the file.
• Searching backward through the file.

7. find

Description: Looks for files below the specified paths that match all the criteria indicated by the
command-line options and takes any action indicated by those options. If no paths are specified, the
search takes place below the current directory.

Syntax: find [path…..] [Options] (See the man pages for the
description of the flags and options by using man find)
This command can be used to search for files by name, date of creation, size and even file type. To
search for files by name use the following syntax:
$ find starting-directory parameters actions
The starting-directory specifies where to begin searching.
The parameters are where you specify the criteria by which to search. Here use –
name filename to specify the file to search for.
The actions section indicates what actions to take on found files. If –print is used then the full name and
path of file is displayed.
Lab No 1
To search for all files named myfile on your system, use this:
$ find / -name myfile –print
(Notice that the previous command attempted to search for the entire system. To do this effectively user
must be logged in as the root user).

8. grep

Description: Searches files for lines matching a specific pattern and displays the lines

Grep stands for Global Regular Expression Parser. What grep does, essentially, is find and display lines
that contain a pattern that you specify. There are two basic ways to use grep.
The first use of grep is to filter the output of other commands. The general syntax is <command> | grep
<pattern>. For instance, if we wanted to see every actively running process on the system, we would
type ps -a | grep R. In this application, grep passes on only those lines that contain the pattern (in this
case, the single letter) R. Note that if someone were running a program called Resting, it would show
up even if its status were S for sleeping, because grep would match the R in Resting. An easy way around
this problem is to type grep " R ", which explicitly tells grep to search for an R with a space on each
side. You must use quotes whenever you search for a pattern that contains one or more blank spaces.
The second use of grep is to search for lines that contain a specified pattern in a specified file. The syntax
here is grep <pattern> <filename>. Be careful. It's easy to specify the filename first and the pattern
second by mistake! Again, you should be as specific as you can with the pattern to be matched, in order
to avoid "false" matches.

Syntax: Assuming that you are in your home directory, then the following command searches for the
word “hello” in each file in your home directory and produces the results as follows:
$ grep hello*
This command then returns one line for each occurrence of the word. The name of the file is also shown.
In general the pattern for the grep command is:
$ grep text-pattern file-list
The text-pattern can be a simple word or phrase or a more complicated regular expression. The use of
regular expressions can be found in the man pages.
The file-list can take any form allowed by the shell.
To check for the contents of all files in a directory use the following:
$ grep text-pattern * where * indicates that all files in the current
directory should be searched.
In its simplest form, the text pattern is a single word or part of a word containing no spaces. To search
for a phrase such as “is a test”, enclose the pattern in quotation marks as follows:
$ grep “is a test” *
(See the man pages for the description of the flags and options by using man grep)

9. USING THE man PAGES

The man pages are manual pages provided in a standard format with most Linux software. Almost all
the commands that ship with Red Hat Linux distribution include man pages. Using the man command
in its most basic form, any existing man page can be read:
Lab No 1
$ man command-name
The above displays the man page for the specified command and allows scrolling through it and
searching it the same way as when using the less command to display text. If the specified man page
cannot be found an error is displayed.

EXERCISES
1. Write a command to:
• List all files (and subdirectories) in the home directory.

• Display the content of /etc/passwd file with as many lines at a time as the last digit of your roll number.

• Count all files in the current directory.

• Page through file in “clear” mode and display prompts.

You might also like