You are on page 1of 12

Linux Notes Unit II

1. Write a short note on command line argument.


• A Command-line Arguments are passed after the name of a program in
command-line operating systems like DOS or Linux and are passed into the
program from the operating system.
• Shell scripts also accept command line arguments similar to nix commands.
• Command line arguments are useful for passing input to script at runtime
which has its own advantage.
• To pass a command line argument we can simply write them after script
name separated with space. All command line parameters can be access by
their position number using $
• Maximum length of command line parameters are not defined by shell but
it’s defined by operating system and measured in Kilobytes.
➢ $* – Store all command line arguments
➢ $@ – Store all command line arguments
➢ $# – Store count of command line arguments
➢ $0 – Store name of script itself
➢ $1 – Store first command line argument
➢ $2 – Store second command line argument
➢ $3 – Store third command line argument
➢ ..
➢ $9 – Store 9’th command line argument
Example:
# sh example.sh abc 22 mumbai

Here,
sh – It is linux shell
example.sh- It is linux shell script stored in argument $0
abc – First command line argument stored in $1
22 – Second command line argument stored in $2
Mumbai – Third command line argument stored in $3
2. Explain the use of following commands:
i. cal
ii. date
iii. who
iv. echo
v. wc
i. cal
cal command is a calendar command in Linux which is used to see the
calendar of a specific month or a whole year.
Syntax:
cal [ [ month ] year]
cal : Shows current month calendar on the terminal
cal month year : Shows calendar of selected month and year
cal year : Shows the whole calendar of the year
cal -3 : Shows calendar of previous, current and next month
Linux Notes Unit II
ii. date
date command is used to display the system date and time. date command
is also used to set date and time of the system. By default the date command
displays the date in the time zone on which unix/linux operating system is
configured.You must be the super-user (root) to change the date and time.
Syntax:
$date
iii. who
who command is used to find out the following information :
1. Time of last system boot
2. Current run level of the system
3. List of logged in users and more.
The who command is used to get information about currently logged in
user on to system.
Syntax : $who [options] [filename]
iv. echo
echo command in linux is used to display line of text/string that are
passed as an argument . This is a built in command that is mostly used in
shell scripts and batch files to output status text to the screen or a file.
Syntax :
echo [option] [string]

v. wc
wc stands for word count. As the name implies, it is mainly used for
counting purpose.
It is used to find out number of lines, word count, byte and characters
count in the files specified in the file arguments.
Syntax:
wc [OPTION]... [FILE]...
3. Define environment variables . Explain its types.
Shell variables are of two types – local and environmental. Local variables are
restricted a scope. Environment variables are available in user total environment i.e.
its sub shell and editor. They control the behavior of the system and determine the
environment in which you work.
Following are various environmental variables:
Command Description
HOME Specifies the home directory of the user
PATH Specifies the list of directories searched
by shell to locate a acommand
USER Specifies the login name of the user
LOGNAME Specifies the login name of the user
OSTYPE Specifies the kernel version of the
operating system used
SHELL Specifies the user login shell
PWD Specifies the absolute path of the
current directory
Linux Notes Unit II
TERM Specifies the type of terminal
MAIL Specifies the absolute path of user
mailbox file

The ‘env’ command displays all the environment variables.


Set New Environment Variables :
You can create your own user defined variable with syntax:
Variable_name=variable_value
Environment variables are case-sensitive and created in upper case.
Deleting variables:
Syntax:
unset variable_name
4. What is the purpose of chmod command ? Explain in detail.
the chmod command is used to change the access mode of a file.
The name is an abbreviation of change mode.
Syntax :
chmod [reference][operator][mode] file...
The references are used to distinguish the users to whom the permissions apply i.e.
they are list of letters that specifies whom to give permissions. The references are
represented by one or more of the following letters:

Reference Class Description


u owner file's owner
g group users who are members of the file's group
o others users who are neither the file's owner nor members of
the file's group
a all All three of the above, same as ugo

The operator is used to specify how the modes of a file should be adjusted. The
following operators are accepted:
Operator Description
+ Adds the specified modes to the
specified classes
- Removes the specified modes from
the specified classes
= The modes specified are to be made
the exact modes for the specified
classes
Linux Notes Unit II
The modes indicate which permissions are to be granted or removed from the
specified classes. There are three basic modes which correspond to the basic
permissions:
r Permission to read the file.
w Permission to write (or delete) the file.
x Permission to execute the file, or, in
the case of a directory, search it.

5. What is shell ?
SHELL is a program which provides the interface between the user and an operating
system. When the user logs in OS starts a shell for user. Kernel controls all essential
computer operations, and provides the restriction to hardware access, coordinates all
executing utilities, and manages Resources between process. Using kernel only user
can access utilities provided by operating system.
6. Explain man command.
man command in Linux is used to display the user manual of any command that we
can run on the terminal. It provides a detailed view of the command which
includes NAME, SYNOPSIS, DESCRIPTION, OPTIONS, EXIT STATUS,
RETURN VALUES, ERRORS, FILES, VERSIONS, EXAMPLES,
AUTHORS and SEE ALSO.
Every manual is divided into the following sections:
•Executable programs or shell commands
•System calls (functions provided by the kernel)
•Library calls (functions within program libraries
•Games
•Special files (usually found in /dev)
•File formats and conventions eg /etc/passwd
•Miscellaneous (including macro packages and conventions), e.g. groff(7)
•System administration commands (usually only for root)
•Kernel routines [Non standard]
1. No Option: It displays the whole manual of the command.
Syntax :
$ man [COMMAND NAME]
Example:
$ man printf
2. Section-num: Since a manual is divided into multiple sections so this option is
used to display only a specific section of a manual.
Syntax :
$ man [SECTION-NUM] [COMMAND NAME]
Example:
$ man 2 intro
Linux Notes Unit II
7. Explain cut , paste , and sort command with proper example.
Cut :
Cut is a command line utility that allows you to cut parts of lines from specified files
or piped data and print the result to standard output. It can be used to cut parts of a
line by delimiter, byte position, and character.
The syntax for the cut command is as follows:
cut OPTION... [FILE]...
When using the cut command you must use one and only one of the following
options:
-f (--fields=LIST) - Select by specifying a field, a set of fields, or a range of fields.
This is the most commonly used option.
-b (--bytes=LIST) - Select by specifying a byte, a set of bytes, or a range of bytes.
-c (--characters=LIST) - Select by specifying a character, a set of characters, or a
range of characters.
Paste:
Paste command is one of the useful commands in Unix or Linux operating system. It
is used to join files horizontally (parallel merging) by outputting lines consisting of
lines from each file specified, separated by tab as delimiter, to the standard output.
When no file is specified, or put dash (“-“) instead of file name, paste reads from
standard input and gives output as it is until a interrupt command [Ctrl-c] is given.
Syntax:
paste [OPTION]... [FILES]...
Sort:
SORT command is used to sort a file, arranging the records in a particular order. By
default, the sort command sorts file assuming the contents are ASCII. Using options
in sort command, it can also be used to sort numerically.
• SORT command sorts the contents of a text file, line by line.
• sort is a standard command line program that prints the lines of its input or
concatenation of all files listed in its argument list in sorted order.
• The sort command is a command line utility for sorting lines of text files. It
supports sorting alphabetically, in reverse order, by number, by month and can
also remove duplicates.
• The sort command can also sort by items not at the beginning of the line,
ignore case sensitivity and return whether a file is sorted or not. Sorting is done
based on one or more sort keys extracted from each line of input.
• By default, the entire input is taken as sort key. Blank space is the default field
separator.
Example :
Command :
$ cat > file.txt
abhishek
chitransh
Linux Notes Unit II
satish
rajan
naveen
divyam
harsh
Sorting a file : Now use the sort command
Syntax :
$ sort filename.txt
Command:
$ sort file.txt
Output :
abhishek
chitransh
divyam
harsh
naveen
raja
8. Explain fields of /etc/shadow file.
The /etc/shadow file contains one entry per line, each representing a user account.
mark:$6$.n.:17736:0:99999:7:::

Username. The string you type when you log into the system. The user account that
exist on the system.

Encrypted Password. The password is using the $type$salt$hashed format. $type is


the method cryptographic hash algorithm and can have the following values:

$1$ – MD5
$2a$ – Blowfish
$2y$ – Eksblowfish
$5$ – SHA-256
$6$ – SHA-512
If the password field contains an asterisk (*) or exclamation point (!), the user will
not be able to login to the system using password authentication. Other login
methods like key-based authentication or switching to the user are still allowed.
In older Linux systems, the user’s encrypted password was stored in the /etc/passwd
file.
Linux Notes Unit II
Last password change. This is the date when the password was last changed. The
umber of days is counted since January 1, 1970 (epoch date).

Minimum password age. The number of days that must pass before the user
password can be changed. Typically it is set to zero, which means that there is no
minimum password age.

Maximum password age. The number of days after the user password must be
changed. By default, this number is set to 99999.

Warning period. The number of days before the password expires during which the
user is warned that the password must be changed.

Inactivity period. The number of days after the user password expires before the
user account is disabled. Typically this field is empty.

Expiration date. The date when the account was disabled. It is represented as an
epoch date.
Unused. This field is ignored. It is reserved for future use.

9. Explain following commands


A. cp
B. mv
C. cat
D. pwd
E. wc

cp:
'cp' means copy. 'cp' command is used to copy a file or a directory.
To copy a file into the same directory syntax will be,
cp <existing file name> <new file name>

mv:
Linux mv command is used to move existing file or directory from one location to
another. It is also used to rename a file or directory. If you want to rename a single
directory or file then 'mv' option will be better to use.

cat:
The 'cat' command is the most universal and powerful tool. It is considered to be one
of the most frequently used commands. It can be used to display the content of a file,
copy content from one file to another, concatenate the contents of multiple files,
display the line number, display $ at the end of the line, etc.
The 'cat' command can be used to display the content of a file.
Syntax:
cat <fileName>
Linux Notes Unit II
pwd:
Linux pwd (print working directory) command displays your location currently you
are working on. It will give the whole path starting from the root ending to the
directory.
Syntax:
pwd

wc
wc stands for word count. As the name implies, it is mainly used for counting
purpose.
It is used to find out number of lines, word count, byte and characters count in the
files specified in the file arguments.
Syntax:
wc [OPTION]... [FILE]...

10. Explain in brief man , info and help command.


man:

The "man" is a short term for manual page. In unix like operating systems such as
linux, man is an interface to view the system's reference manual.

A user can request to display a man page by simply typing man followed by a
space and then argument. Here its argument can be a command, utility or
function. A manual page associated with each of these arguments is displayed.

If you will provide a section number in the command, then man will be directed
to look into that section number of the manual and that section page will be
displayed. And if not, then by default it will display the first page and you have to
go through the entire sections in a pre-defined manner.

We'll read about section number in this tutorial.

Syntax of man:

man [option(s)] keyword(s)

info:

Some commands don't have man pages or have incomplete man pages and store their
information as document form. To view these pages info command is used.
Linux Notes Unit II
Command info display information in the document format. It is similar to man
command with more robustness for linking pages together.

Info pages are made with texinfo tools, can link with other pages and create menus.

The info document's default location is /usr/share/info.

Syntax:

1. info

help:

The help command which as its name says help you to learn about any built-in
command . Help command just displays information about shell built-in commands.
Syntax:
help[-dms][pattern]
here d,m,s are options that you can use with the help command.

11. Write a short note on sed command.


SED command in UNIX is stands for stream editor and it can perform lot’s of
function on file like, searching, find and replace, insertion or deletion. Though most
common use of SED command in UNIX is for substitution or for find and replace.
By using SED you can edit files even without opening it, which is much quicker way
to find and replace something in file, than first opening that file in VI Editor and
then changing it.
• SED is a powerful text stream editor. Can do insertion, deletion, search and
replace(substitution).
• SED command in unix supports regular expression which allows it perform
complex pattern matching.
Syntax:
sed OPTIONS... [SCRIPT] [INPUTFILE...]
Example:
Consider the below text file as an input.
$cat > file.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
Replacing or substituting string : Sed command is mostly used to replace the text
in a file. The below simple sed command replaces the word “unix” with “linux” in
the file.
1. $sed 's/unix/linux/' file.txt
Linux Notes Unit II
Output :
linux is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
2. Replacing the nth occurrence of a pattern in a line : Use the /1, /2 etc flags
to replace the first, second occurrence of a pattern in a line. The below
command replaces the second occurrence of the word “unix” with “linux” in a
line.
$sed 's/unix/linux/2' file.txt
Output:
unix is great os. linux is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.linux is a multiuser os.Learn unix .unix is a powerful.
3. Replacing all the occurrence of the pattern in a line : The substitute flag /g
(global replacement) specifies the sed command to replace all the occurrences
of the string in the line.
$sed 's/unix/linux/g' file.txt
Output :
linux is great os. linux is opensource. linux is free os.
learn operating system.
linux linux which one you choose.
linux is easy to learn.linux is a multiuser os.Learn linux .linux is a powerful.
4. Replacing from nth occurrence to all occurrences in a line : Use the
combination of /1, /2 etc and /g to replace all the patterns from the nth
occurrence of a pattern in a line. The following sed command replaces the
third, fourth, fifth… “unix” word with “linux” word in a line.
$sed 's/unix/linux/3g' file.txt
Output:
unix is great os. unix is opensource. linux is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn linux .linux is a powerful.
5. Replacing string on a specific line number : You can restrict the sed
command to replace the string on a specific line number. An example is
$sed '3 s/unix/linux/' file.txt
Output:
Linux Notes Unit II
unix is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
The above sed command replaces the string only on the third line.
6. Duplicating the replaced line with /p flag : The /p print flag prints the
replaced line twice on the terminal. If a line does not have the search pattern
and is not replaced, then the /p prints that line only once.
$sed 's/unix/linux/p' file.txt
Output:
linux is great os. unix is opensource. unix is free os.
linux is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
7. Printing only the replaced lines : Use the -n option along with the /p print
flag to display only the replaced lines. Here the -n option suppresses the
duplicate rows generated by the /p flag and prints the replaced lines only one
time.
$sed -n 's/unix/linux/p' file.txt
Output:
linux is great os. unix is opensource. unix is free os.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
If you use -n alone without /p, then the sed does not print anything.
8. Replacing string on a range of lines : You can specify a range of line
numbers to the sed command for replacing a string.
$sed '1,3 s/unix/linux/' file.txt
Output:
linux is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
Linux Notes Unit II
Here the sed command replaces the lines with range from 1 to 3. Another
example is
$sed '2,$ s/unix/linux/' file.txt
Output:
unix is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful

You might also like