Unit Ii
Unit Ii
Unit Ii
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 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.
$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.
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]...
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.
Syntax of man:
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.
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.