Lab_1_OS[1]
Lab_1_OS[1]
Lab Session 01
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.
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)
1
Operating Systems Lab Session 01
NED University of Engineering & Technology – Department of Software Engineering
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
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
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.
• 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.
• List all file names containing your roll number in the end.
4
Operating Systems Lab Session 01
NED University of Engineering & Technology – Department of Software Engineering
• List C source files in the current directory, showing larger file first.
• 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