Module - 1 Part1
Module - 1 Part1
A Multiuser System
A Multitasking System
The Building Block Approach
The UNIX Toolkit
Pattern Matching
Programming Facility
/etc/profile
profile
The process is as follows −
The shell checks to see whether the file /etc/profile exists.
If it exists, the shell reads it. Otherwise, this file is skipped. No error message is
displayed.
The shell checks to see whether the file .profile exists in your home directory. Your
home directory is the directory that you start out in after you log in.
If it exists, the shell reads it; otherwise, the shell skips it. No error message is displayed.
As soon as both of these files have been read, the shell displays a prompt −
$
This is the prompt where you can enter commands in order to have them executed.
Note − The shell initialization process detailed here applies to all Bourne type shells, but some
additional files are used by bash and ksh.
Your prompt will become =>. To set the value of PS1 so that it shows the working directory,
issue the command −
=>PS1="[\u@\h \w]\$"
[root@ip-72-167-112-17 /var/www/tutorialspoint/unix]$
[root@ip-72-167-112-17 /var/www/tutorialspoint/unix]$
The result of this command is that the prompt displays the user's username, the machine's name
(hostname), and the working directory.
You can make the change yourself every time you log in, or you can have the change made
automatically in PS1 by adding it to your .profile file.
When you issue a command that is incomplete, the shell will display a secondary prompt and
wait for you to complete the command and hit Enter again.
The default secondary prompt is > (the greater than sign), but can be changed by re-defining
the PS2 shell variable −
Following is the example which uses the default secondary prompt −
$ echo "this is a
> test"
this is a
test
$
Environment Variables
Following is the partial list of important environment variables. These variables are set and
accessed as mentioned below −
Sr.No. Variable & Description
1 DISPLAY
Contains the identifier for the display that X11 programs should use by default.
2 HOME
Indicates the home directory of the current user: the default argument for the cd built-incommand.
3 IFS
Indicates the Internal Field Separator that is used by the parser for word splitting after
expansion.
4 LANG
LANG expands to the default system locale; LC_ALL can be used to override this. For example,
if its value is pt_BR, then the language is set to (Brazilian) Portuguese and the locale to Brazil.
5 LD_LIBRARY_PATH
A Unix system with a dynamic linker, contains a colonseparated list of directories that the
dynamic linker should search for shared objects when building a process image after exec, before
searching in any other directories.
6 PATH
Indicates the search path for commands. It is a colon-separated list of directories in which the shell
looks for commands.
7 PWD
Indicates the current working directory as set by the cd command.
8 RANDOM
Generates a random integer between 0 and 32,767 each time it is referenced.
9 SHLVL
Increments by one each time an instance of bash is started. This variable is useful for determining
whether the built-in exit command ends the current session.
10 TERM
Refers to the display type.
11 TZ
Refers to Time zone. It can take values like GMT, AST, etc.
12 UID
Expands to the numeric user ID of the current user, initialized at the shell startup.
Kernel
The kernel provides a bridge between the hardware and the user. It is a software application that
is central to the operating system. The kernel handles the files, memory, devices, processes and
the network for the operating system. It is the responsibility of the kernel to make sure all the
system and user tasks are performed correctly.
Shell
The program between the user and the kernel is known as the shell. It translates the many
commands that are typed into the terminal session. These commands are known as the shell
script. There are two major types of shells in Unix. These are Bourne shell and C Shell. The
Bourne shell is the default shell for version 7 Unix.
The character $ is the default prompt for the Bourne shell. The C shell is a command processor
that is run in a text window. The character % is the default prompt for the C shell.
Applications
The applications and utility layer in Unix includes the word processors, graphics programs,
database management programs, commands etc. The application programs provide an
application to the end users.
For example, a web browser is used to find information while gaming software is used to play
games. The requests for service and application communication systems used in an application
by a programmer is known as an application program interface (API).
The Portable Operating System Interface (POSIX) is an IEEE standard that helps compatibility
and portability between operating systems. Theoretically, POSIX compliant source code should
be seamlessly portable. In the real world, application transition often runs into system specific
issues. But POSIX compliance makes it simpler to port applications which can result in time
savings. So developers should get acquainted with the fundamentals of this widely used standard.
To give a command to a UNIX system you type the name of the command, along with any
associated information, such as a filename, and press the <Return> key. The typed line is called
the command line and UNIX uses a special program, called the shell or the command line
interpreter, to interpret what you have typed into what you want to do. The components of the
command line are:
the command;
any options required by the command
the command's arguments (if required).
For example, the general form of a UNIX command is:
Commands are case sensitive. command and Command are not the same.
Options are generally preceded by a hyphen (-), and for most commands, more than one option
can be strung together, in the form:
command -[option][option][option]
or
1) echo
The syntax for echo is:
2. Declare a variable and echo its value. For example, Declare a variable of x and assign
its value=10.
$ x=10
echo its value:
Note: The ‘-e‘ option in Linux acts as interpretation of escaped characters that are
backslashed.
3. Using option ‘\b‘ – backspace with backslash interpretor ‘-e‘ which removes all the
spaces in between.
TecmintisacommunityofLinuxNerds
4. Using option ‘\n‘ – New line with backspace interpretor ‘-e‘ treats new line from
where it is used.
Tecmint
is
a
community
of
Linux
Nerds
5. Using option ‘\t‘ – horizontal tab with backspace interpretor ‘-e‘ to have horizontal tab
spaces.
6. How about using option new Line ‘\n‘ and horizontal tab ‘\t‘ simultaneously.
Tecmint
is
a
community
Of
Linux
Nerds
7. Using option ‘\v‘ – vertical tab with backspace interpretor ‘-e‘ to have vertical tab
spaces.
$ echo -e "\vTecmint \vis \va \vcommunity \vof \vLinux \vNerds"
Tecmint
is
a
community
of
Linux
Nerds
8. How about using option new Line ‘\n‘ and vertical tab ‘\v‘ simultaneously.
Tecmint
is
a
community
of
Linux
Nerds
Note: We can double the vertical tab, horizontal tab and new line spacing using the
option two times or as many times as required.
9. Using option ‘\r‘ – carriage return with backspace interpretor ‘-e‘ to have specified
carriage return in output.
echo Options
Options Description
\b Backspace
\\ Backslash
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
Description
printf prints a formatted string to the standard output. Its roots are in the C programming
language, which uses a function by the same name. It is a handy way to produce precisely-
formatted output from numerical or textual arguments.
Syntax
printf FORMAT [ARGUMENT]...
printf OPTION
Options
FORMAT FORMAT controls the output, and defines the way that the ARGUMENTs
will be expressed in the output. See the Format section, below.
ARGUMENT Each ARGUMENT will be inserted into the formatted output
according to the definition of FORMAT.
--help Display a help message, and exit.
--version Display version information, and exit.
Format
The FORMAT string contains three types of objects:
ordinary characters, which are copied verbatim to the output.
interpreted character sequences, which are escaped with a backslash ("\").
conversion specifications, which define the way in which ARGUMENTs will be
expressed as part of the output.
Here is a quick example which uses these three types of objects:
printf "My name is \"%s\".\nIt's a pleasure to meet you." "John"
This command produces the output:
My name is "John".
It's a pleasure to meet you.
Here, FORMAT is enclosed in double-quotes ("). There is one conversion specification: %s,
which interprets the argument "John" as a string and inserts it into the output. There are three
escaped character sequences: two occurrences of \" and one occurrence of \n. The
sequence \" translates as a literal double-quote; it is escaped with a backslash so
that printf knows to treat it as a literal character, and not as the end of the FORMAT string. \n is
the sequence for a newlinecharacter, and tells printf to begin a new line and continue the output
from there.
The power of printf lies in the fact that for any given FORMAT string, the ARGUMENTs can be
changed to affect the output. For example, the output of the command in the above example can
be altered just by changing the argument, "John". If used in a script, this argument can be set to
a variable. For instance, the command
printf "Hi, I'm %s.\n" $LOGNAME
...will insert the value of the environment variable $LOGNAME, which is the username of
whoever ran the command
Conversion Specifications
- A minus sign. This tells printf to left-adjust the conversion of the argument.
number An integer that specifies field width; printf will print a conversion of ARGUMENT in a field at least
number characters wide.If necessary it will be padded on the left (or right, if left-adjustment is called
to make up the field width.
. A period, which separates the field width from the precision.
number An integer, the precision, which specifies the maximum number of characters to be printed from a stri
or the number of digits after the decimal point of a floating-pointvalue, or the minimum number of dig
for an integer.
h or l These differentiate between a short and a long integer, respectively, and are generally only needed for
programming.
Each conversion specification begins with a % and ends with a conversion character. Between
the % and the conversion character there may be, in order:
The conversion characters themselves, which tell printf what kind of argument to expect, are as
follows:
conversion
argument type
character
d, i An integer, expressed as a decimal number.
O An integer, expressed as an unsigned octal number.
x, X An integer, expressed as an unsigned hexadecimal number
U An integer, expressed as an unsigned decimal number.
C An integer, expressed as a character. The integer corresponds to the character's ASCII code.
S A string.
F A floating-point number, with a default precision of 6.
e, E A floating-point number expressed in scientific notation, with a default precision of 6.
P A memory address pointer.
% No conversion; a literal percent sign ("%") is printed instead.
A width or precision may be represented with an asterisk
("*"); if so, the asterisk reads in an argument, which must be
an integer, and uses that value. For example,
printf "%.*s" 5 "abcdefg"
...produces the following output:
abcde
The following table represents the way that printf would
output its ARGUMENT, "computerhope", using
various FORMAT strings. Each string is enclosed in quotes so
that it's easier to see the exact extent of each:
FORMAT string ARGUMENT string output string
"%s" "computerhope" "computerhope"
"%8s" "computerhope" "computerhope"
"%.8s" "computerhope" "computer"
"%-8s" "computerhope" "computerhope"
"%-15s" "computerhope" "computerhope "
"%15.8s" "computerhope" " computer"
"%-15.8" "computerhope" "computer "
"%-15.2" "computerhope" "co "
3) ls command
# ls
# ls -l
total 176
-rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap
-rw-------. 1 root root 1586 Jul 31 02:17 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Desktop
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Documents
drwxr-xr-x. 4 root root 4096 Aug 16 02:55 Downloads
-rw-r--r--. 1 root root 21262 Aug 12 12:42 fbcmd_update.php
-rw-r--r--. 1 root root 46701 Jul 31 09:58 index.html
-rw-r--r--. 1 root root 48867 Jul 31 02:17 install.log
-rw-r--r--. 1 root root 11439 Jul 31 02:13 install.log.syslog
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Music
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Pictures
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Public
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Templates
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Videos
# ls -lh
total 176K
-rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap
-rw-------. 1 root root 1.6K Jul 31 02:17 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Desktop
drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Documents
drwxr-xr-x. 4 root root 4.0K Aug 16 02:55 Downloads
-rw-r--r--. 1 root root 21K Aug 12 12:42 fbcmd_update.php
-rw-r--r--. 1 root root 46K Jul 31 09:58 index.html
-rw-r--r--. 1 root root 48K Jul 31 02:17 install.log
-rw-r--r--. 1 root root 12K Jul 31 02:13 install.log.syslog
drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Music
drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Pictures
drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Public
drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Templates
drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Videos
# ls -F
# ls -r
# ls -R
total 1384
-rw-------. 1 root root 33408 Aug 8 17:25 anaconda.log
-rw-------. 1 root root 30508 Aug 8 17:25 anaconda.program.log
./httpd:
total 132
-rw-r--r-- 1 root root 0 Aug 19 03:14 access_log
-rw-r--r--. 1 root root 61916 Aug 10 17:55 access_log-20120812
./lighttpd:
total 68
-rw-r--r-- 1 lighttpd lighttpd 7858 Aug 21 15:26 access.log
-rw-r--r--. 1 lighttpd lighttpd 37531 Aug 17 18:21 access.log-20120819
./nginx:
total 12
-rw-r--r--. 1 root root 0 Aug 12 03:17 access.log
-rw-r--r--. 1 root root 390 Aug 12 03:17 access.log-20120812.gz
:59 0001.pcap
# ls -i
20112 0001.pcap 23610 Documents 23793 index.html 23611 Music 23597
Templates
23564 anaconda-ks.cfg 23595 Downloads 22 install.log 23612 Pictures 23613
Videos
23594 Desktop 23585 fbcmd_update.php 35 install.log.syslog 23601 Public
To show only hostname and user associated with stdin (usually keyboard), enter:
$ who –m
Option Description
-a Same as -b -d –login -p -r -t -T –u
-b Time of last system boot
-d Print dead processes
-H Print line of column headings
-l Print system login processes
-m Only hostname and user associated with stdin
-p Print active processes spawned by init
-q All login names and number of users logged on
-r Print current runlevel
-t Print last system clock change
-T Add user’s message status as +, – or ?
-u List users logged in
5) Date command
In Unix-like operating systems, the passwd command is used to change thepassword of a user
account. A normal user can run passwd to change their ownpassword, and a system
administrator (the superuser) can use passwd to change another user's password, or define how
that account's password can be used or changed.
Description
The passwd command changes passwords for user accounts. A normal user can only change the
password for their own account, but the superuser can change the password for any
account. passwd can also change or reset the account's validity period — how much time can
pass before the password expires and must be changed.
Before a normal user can change their own password, they must first enter their current password
for verification. (The superuser can bypass this step when changing another user's password.)
to see if the user is allowed to change their password at this time. If not, passwd refuses to
continue, and exits.
Otherwise, the user is then prompted twice for a replacement password. Both entries must match
for passwd to continue.
Next, the password is tested for complexity. As a general guideline, passwords should consist of
at least 6 characters, including one or more of each of the following:
lower case letters
digits 0 through 9
punctuation marks
Syntax
passwd [OPTION] [USER]
Options
The following options will change the way passwd operates:
-a, --all When used with -S (see below), this option will show the password status
for all users. This option will not work if used without -S.
-d, --delete Delete a user's password (make it empty). This option is a quick way to disable
logins for an account, without disabling the account itself.
-e, --expire Immediately expire an account's password. This forces a user to change
their password the next time they log in.
-h, --help Display information about how to use the passwd command.
-i, --inactive INACTIVE This option is used to disable an account after the password has been expired for
a number of days. After a user account has had an expired password for
integer INACTIVE days, the user may no longer sign on to the account.
-k, --keep-tokens Keep password tokens. Indicates that this user's password should only be
changed if it has expired.
-l, --lock Lock the password of the named account. This option disables a password by
changing it to a value which matches no possible encrypted value. It does this by
adding a character at the beginning of the encrypted password.
-n, --mindays MIN_DAYS Set the minimum number of days between password changes to MIN_DAYS.
A value of zero for this field indicates that the user may change his/her password at a
-q, --quiet Quiet mode; passwd will operate without displaying any output.
-R, --root CHROOT_DIR For advanced users: this option will apply changes in the chrootdirectory
CHROOT_DIR and use the configuration files from the CHROOT_DIR directory.
-S, --status Display account status information. The status information consists of 7 fields:
1. The user's login name
2. password usability: L if the account has a locked password,
NP if the account has no password, or P if the account has a usable passwo
3. date of the last password change
4. minimum password age
5. maximum password age
6. password warning period
7. password inactivity period
Notes
Password complexity will vary depending on the system.
Consult your operating system documentation for default
complexity rules and how to change them.
On systems that use NIS (Network Information Services),
users may not be able to change their password if they are not
logged into the NIS server.
Syntax
cal [-mjy] [[month] year]
DESCRIPTION
A single parameter specifies the 4 digit year (1 - 9999) to be displayed. Two parameters denote
the Month (1 - 12) and Year (1 - 9999). If arguments are not specified, the current month is
displayed. A year starts on 01 Jan.
OPTIONS
Tag Description
EXAMPLES
To display current month's calendar
$ cal
April 2016
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
To display feb 2015 calendar
$ cal 2 2015
February 2015
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
To display complete year calendar.
wc note ; ls –l
You can group command together within parenthesis and redirect the output.
1.8 The Type command: Knowing the type of the command and
locating it.
$ type test
test is a shell builtin
$ type cp
cp is /bin/cp
$ type unknown
unknown not found
$ type type
type is a shell builtin
type is a command that describes how its arguments would be interpreted if used
as command names.
Syntax:
type [OPTIONS] COMMAND.
The type command is used to describe how its argument would be translated if used as
commands. It is also used to find out whether it is built-in or external binary file.
Syntax:
type [Options] command names
Example:
Options:
-a : This option is used to find out whether it is an alias, keyword or a function and it also
displays the path of an executable, if available.
-p : This option displays the name of the disk file which would be executed by the shell. It
will return nothing if the command is not a disk file.
Enter the root password when prompted. After typing su - and pressing ↵ Enter , you'll be
prompted for the root password.
Check the command prompt. When you are logged in as root, the command prompt should end
with # instead of $
Enter the commands that require root access. Once you've used su - to log in as root, you
can run any commands that require root access. The su command is preserved until the end of
the session, so you don't need to keep re-entering the root password every time you need to run a
command.
There are two ways to become the superuser. The first is to log in as root directly. The second
way is to execute the command suwhile logged in to another user account. The su command may
be used to change one’s current account to that of a different user after entering the proper
password. It takes the username corresponding to the desired account as its argument; root is the
default when no argument is provided.
After you enter the su command (without arguments), the system prompts you for
the root password. If you type the password correctly, you’ll get the normal root account prompt
(by default, a number sign: #), indicating that you have successfully become superuser and that
the rules normally restricting file access and command execution do not apply. For example:
$ su
Password: Not echoed
The most commonly used option when invoking su is -, -l, --login. This makes the shell a login
shell with an environment very similar to a real login and changes the current directory :
su -
If you want to run another shell instead of the one defined in the passwd file, use the -s, --
shell option. For example, to switch to root and to run the zsh shell you would type:
su -s /usr/bin/zsh
To preserve the entire environment (HOME, SHELL, USER, and LOGNAME) of the calling
user use the -p, --preserve-environment option.
su -p
If you want to run a command as the substitute user without starting an interactive shell , use
the -c, --command option. For example, to invoke the ps command as root you would type:
su -c ps
To switch to another user account, pass the user name as an argument to su. For example, to
switch to the user tyrion you would type:
su tyrion