0% found this document useful (0 votes)
23 views6 pages

Lab_1_OS[1]

Uploaded by

kanzaakram123
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)
23 views6 pages

Lab_1_OS[1]

Uploaded by

kanzaakram123
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

Operating Systems Lab Session 01

NED University of Engineering & Technology – Department of Software Engineering

Lab Session 01

Use some of the most frequently executed operating system commands

Introduction
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 discussed below:

1) ls (list)
The ls command lists the contents of a directory. It is a program in the /bin directory.
Examples:
ls list the current working directory
ls -l list the current working directory in a long format (with details)

ls /home list the /home directory


ls -d *.dat list the current working directory for the .dat files/directories, for directories print only
the names not the contents
ls -a list all files in the current working directory including those
whose names begin with a dot

1
Operating Systems Lab Session 01
NED University of Engineering & Technology – Department of Software Engineering

ls ~ list the user’s home directory

2) pwd (print working directory)


The pwd command displays the name of (absolute path to) the current working directory. It is a program
in the /bin directory.
Example:
pwd print the absolute path to the working directory

3) cd (change directory)
The cd command changes the current working directory. It is a part of the bash.
Examples:
cd mydir change to the mydir directory
cd .. change to the parent directory

cd change to the home directory


cd ../somedir change to the somedir directory residing in the parent directory

4) less
The less command displays the contents of a file on the screen in its own environment. Listing with the
cursor up and down, page up and page down keys is enabled. It is a program in the /usr/bin directory. To
terminate the ‘less’ environment, press q.
Examples:
less data display the contents of the data file
less –N data display the contents of the data file with the line numbers
Searching for a particular phrase or sequence of characters is possible. To find all the occurrences of a
phrase consisting for instance of two words, i.e. word1 word2, type /word1 word2 in the less
environment. No quotes are needed. The space character is treated as any other character. To display the
next occurrence of the searched phrase, type n.

5) find
The find command finds the files, directories, etc., matching a given name pattern. It searches the entire
directory structure beneath one or more given directories. All subdirectories are included recursively. The
find command is a program in the /usr/bin directory.
Examples:
find /usr . -name ’*.txt’ -type f search /usr and current directory for the files ending
with .txt
find . -name ’data’ -type d search the current directory for the data directories
find . -not -name ’*.txt’ -type f search the current directory for the non .txt files

6) grep (Global Regular Expression Print)


The grep command searches for a particular phrase or a sequence of characters in a file. It is a program in
the /bin directory. The following characters have a special meaning in the search description:
. any single character
* arbitrary characters
[] any single character listed within brackets
^ beginning of a line
$ end of a line
\\ escape sequence for special characters
grep ’pattern’ *.txt search all the .txt files in the current directory and print all lines
containing the pattern word
grep ’pattern’ search all files in the /home/user/data directory and print all lines
/home/user/data/* containing a pattern
grep -l ’pattern’ print the filenames of the files in /home/user/data containing the
/home/user/data/* pattern

2
Operating Systems Lab Session 01
NED University of Engineering & Technology – Department of Software Engineering

grep -i ’pattern’ data word print all lines in the data file containing the case-insensitive
pattern word
grep -v ’pattern’ data print all lines in the data file not containing the word pattern
grep -n ’pattern’ data print all lines in the data file containing the word pattern with the
line numbers
grep -c ’pattern’ data print the number of lines in the data file containing the pattern
word
grep ’j..k’ data print all lines in the data file containing a four-character word
beginning with j and ending with k (e.g. jack)
grep ’j*k’ data print all lines in the data file containing a word beginning with j
and ending with k (e.g. jack, jailbreak)
grep ’j[ao]ck’ data print all lines in the data file containing the word jack or jock
grep ’^jack’ data print all lines in the data file starting with the word jack
grep ’jack$’ data print all lines in the data file ending with the word jack

7) wc (word count)
The wc command counts the lines, words and characters (bytes) in a file. It is a program in the /usr/bin
directory.
Examples:
wc data count the lines, words and bytes of the data file
wc –l data count the lines in the data file
wc –w data count the words in the data file
wc –c data count the bytes in the data file

8) su (substitute user)
The su command changes the user identity. It is a program in the /bin directory.
Examples:
su become the root user
su jekyll become the jekyll user

9) man (manual)
The man command provides in-depth information about a requested command or allows users to search
for commands related to a particular keyword.
To terminate, press q. It is a program in the /usr/bin directory.
Examples:
man ls Print the manual pages of ls command

10) history
The history command displays a list of the executed commands. It is a part of the bash.
Examples:
history print the history list
history -c clear the history list

3
Operating Systems Lab Session 01
NED University of Engineering & Technology – Department of Software Engineering

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

• List all files named chapter1 in the /work directory.

• List all files beginning with memo owned by ann.

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

• Search the current directory, look for filenames that don’t begin with a capital letter.

• Search the system for files that were modified within the last two days.

• Recursively grep for your-name down a directory tree.

• List all file names containing your roll number in the end.

• List files in your home folder in human readable format.

4
Operating Systems Lab Session 01
NED University of Engineering & Technology – Department of Software Engineering

• List the contents of directories /bin and /etc.

• List C source files in the current directory, showing larger file first.

• Count all files in the current directory.

• Create a group called directors and assign it a password. Add user Ann and user James to the group
and assign passwords to each of them.

5
Operating Systems Lab Session 01
NED University of Engineering & Technology – Department of Software Engineering

You might also like