0% found this document useful (0 votes)
40 views

Module - 1 Part1

Uploaded by

vinutha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Module - 1 Part1

Uploaded by

vinutha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Module 1: Introduction

1.1 Unix Components/Architecture

1) Division of Labor: Kernel n Shell


2) The File and Process
3) The System Calls
 Levels of Unix Software Unix uses a layer approach of defining software
 Layered approach is basis for Unix security
 At the lowest level, the level closest to the hardware, are the basic hardware interface
modules, Process scheduling, Memory management, Interrupt handling, Low level
device control, Disk system management and data buffering, Process synchronization
and inter-process communication (IPC)
Division of Labor: Kernel & Shell
“Filter ideas” is division of labor between two agencies- The Kernel and Shell
 The Kernel interacts with the machine's hardware & the Shell with the user
(Mapping between user requests and device driver actions)
 The Kernel is the core of the operating system.
 Its a collections of routines mostly written in c
 Its loaded into memory when its booted n communicates with hardware directly
 Programs access the Kernel through set of System calls
 It actually also performs the requested service Ex: Process creation and termination
(Schedules the process)I/O services, File/disk access services (manages sys
memory)Terminal handling services
 Decides the Priorities n perform other tasks.
Shell
 Command Interpreter is a job that is handled by the outer part of the OS called as
Shell
 Unix command interface is a separate program referred to as the shell
 The Shell actually acts as an interface between user & the Kernel
 There could be several Shells in action-one for each user who logged in.
 shell communicates with Kernel to see that command is executed.
 It is represented by sh(Bourne shell), csh(C shell),ksh(Korn shell) or bash(Bash shell)
The File and Process
“File have places and Process have Life”
A File is an array of bytes n also a part of single hierarchical structure.
File can be moved from one place to another It considers even directories and devices as a
single file system.
The Process is an entity, name given to a file when its executed.
The System Calls
System calls provide a programming interface that allows user programs to access kernel
functions
There are a large number of system calls that perform functions to manage system resources
such as memory, disk storage, and peripherals.
System calls are defined in a run-time library that provides a mapping of the system call
interface to the kernel routine(s) that perform the system functions The shell also provides
functionality equivalent to many of the system calls

1.2 Features of Unix

 A Multiuser System
 A Multitasking System
 The Building Block Approach
 The UNIX Toolkit
 Pattern Matching
 Programming Facility

1.3 The Unix Environment and UNIX Structure

An important Unix concept is the environment, which is defined by environment variables.


Some are set by the system, others by you, yet others by the shell, or any program that loads
another program.
A variable is a character string to which we assign a value. The value assigned could be a
number, text, filename, device, or any other type of data.
For example, first we set a variable TEST and then we access its value using the echo command

$TEST="Unix Programming"
$echo $TEST

It produces the following result.


Note that the environment variables are set without using the $ sign but while accessing them
we use the $ sign as prefix. These variables retain their values until we come out of the shell.
When you log in to the system, the shell undergoes a phase called initialization to set up the
environment. This is usually a two-step process that involves the shell reading the following
files

 /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.

The .profile File


The file /etc/profile is maintained by the system administrator of your Unix machine and
ncontains shell initialization information required by all users on a system.
The file .profile is under your control. You can add as much shell customization information as
you want to this file. The minimum set of information that you need to configure includes −

 The type of terminal you are using.


 A list of directories in which to locate the commands.
 A list of variables affecting the look and feel of your terminal.
You can check your .profile available in your home directory. Open it using the vi editor and
check all the variables set for your environment.

Setting the Terminal Type


Usually, the type of terminal you are using is automatically configured by either
the login or gettyprograms. Sometimes, the auto configuration process guesses your terminal
incorrectly.
If your terminal is set incorrectly, the output of the commands might look strange, or you might
not be able to interact with the shell properly.
To make sure that this is not the case, most users set their terminal to the lowest common
denominator in the following way −
$TERM=vt100
$

Setting the PATH


When you type any command on the command prompt, the shell has to locate the command
before it can be executed.
The PATH variable specifies the locations in which the shell should look for commands.
Usually the Path variable is set as follows −
$PATH=/bin:/usr/bin
$
Here, each of the individual entries separated by the colon character (:) are directories. If you
request the shell to execute a command and it cannot find it in any of the directories given in the
PATH variable, a message similar to the following appears −
$hello
hello: not found
$
There are variables like PS1 and PS2 which are discussed in the next section.

PS1 and PS2 Variables


The characters that the shell displays as your command prompt are stored in the variable PS1.
You can change this variable to be anything you want. As soon as you change it, it'll be used by
the shell from that point on.
For example, if you issued the command −
$PS1='=>'
=>
=>
=>

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
$

The example given below re-defines PS2 with a customized prompt −


$ PS2="secondary prompt->"
$ echo "this is a
secondary prompt->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).

Posix and Single Unix specification


The Single UNIX Specification is the standard in which the core interfaces of a UNIX OS are
measured. The UNIX standard includes a rich feature set, and its core volumes are
simultaneously the IEEE Portable Operating System Interface (POSIX) standard and the
ISO/IEC 9945 standard. The specification encompasses the base operating system environment,
networking services, windowing system services, and internationalization aspects and
programming languages. The latest version of the certification standard is UNIX V7, aligned
with the Single UNIX Specification Version 4, 2018 Edition.

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.

1.4 General Features of UNIX Commands/ Command Structure


A command is a program that tells the Unix system to do something. It has the form:

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:

command [-option(s)] [argument(s)]


where an argument indicates on what the command is to perform its action, usually a file or
series of files. An option modifies the command, changing the way it performs.

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

command -option1 -option2 -option3

1.5 Basic UNIX Commands

1) echo
 The syntax for echo is:

 echo [option(s)] [string(s)]

 1. Input a line of text and display on standard output

 $ echo Tecmint is a community of Linux Nerds

 Outputs the following text:

 Tecmint is a community of Linux Nerds

 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:

 $ echo The value of variable x = $x

 The value of variable x = 10

 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.

 $ echo -e "Tecmint \bis \ba \bcommunity \bof \bLinux \bNerds"

 TecmintisacommunityofLinuxNerds

 4. Using option ‘\n‘ – New line with backspace interpretor ‘-e‘ treats new line from
where it is used.

 $ echo -e "Tecmint \nis \na \ncommunity \nof \nLinux \nNerds"

 Tecmint

 is

 a

 community

 of
 Linux

 Nerds

 5. Using option ‘\t‘ – horizontal tab with backspace interpretor ‘-e‘ to have horizontal tab
spaces.

 $ echo -e "Tecmint \tis \ta \tcommunity \tof \tLinux \tNerds"

 Tecmint is a community of Linux Nerds

 6. How about using option new Line ‘\n‘ and horizontal tab ‘\t‘ simultaneously.

 $ echo -e "\n\tTecmint \n\tis \n\ta \n\tcommunity \n\tof \n\tLinux \n\tNerds"

 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.

 $ echo -e "\n\vTecmint \n\vis \n\va \n\vcommunity \n\vof \n\vLinux \n\vNerds"

 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 -e "Tecmint \ris a community of Linux Nerds"

 is a community of Linux Nerds


 10. Using option ‘\c‘ – suppress trailing new line with backspace interpretor ‘-e‘ to
continue without emitting new line.

 $ echo -e "Tecmint is a community \cof Linux Nerds"

 Tecmint is a community avi@tecmint:~$

 echo Options
Options Description

-n do not print the trailing newline.

-e enable interpretation of backslash escapes.

\b Backspace

\\ Backslash

\n new line

\r carriage return

\t horizontal tab

\v vertical tab

2) printf Unix command

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

1. List Files using ls with no option


ls with no option list files and directories in bare format where we won’t be able to view details
like file types, size, modified date and time, permission and links etc.

# ls

0001.pcap Desktop Downloads index.html install.log.syslog Pictures Templates


anaconda-ks.cfg Documents fbcmd_update.php install.log Music Public Videos

2 List Files With option –l


Here, ls -l (-l is character not one) shows file or directory, size, modified date and time, file or
folder name and owner of file and its permission.

# 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

3. View Hidden Files


List all files including hidden file starting with ‘.‘.
# ls -a

. .bashrc Documents .gconfd install.log .nautilus .pulse-cookie


.. .cache Downloads .gnome2 install.log.syslog .netstat.swp .recently-
used.xbel
0001.pcap .config .elinks .gnome2_private .kde .opera .spice-vdagent
anaconda-ks.cfg .cshrc .esd_auth .gtk-bookmarks .libreoffice Pictures .tcshrc
.bash_history .dbus .fbcmd .gvfs .local .pki Templates
.bash_logout Desktop fbcmd_update.php .ICEauthority .mozilla Public Videos
.bash_profile .digrc .gconf index.html Music .pulse .wireshark

4. List Files with Human Readable Format with option -lh


With combination of -lh option, shows sizes in human readable format.

# 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

5. List Files and Directories with ‘/’ Character at the end


Using -F option with ls command, will add the ‘/’ Character at the end each directory.

# ls -F

0001.pcap Desktop/ Downloads/ index.html install.log.syslog Pictures/ Templates/


anaconda-ks.cfg Documents/ fbcmd_update.php install.log Music/ Public/ Videos/
6. List Files in Reverse Order
The following command with ls -r option display files and directories in reverse order.

# ls -r

Videos Public Music install.log fbcmd_update.php Documents anaconda-ks.cfg


Templates Pictures install.log.syslog index.html Downloads Desktop 0001.pcap

7. Recursively list Sub-Directories


ls -R option will list very long listing directory trees. See an example of output of the command.

# 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

8. Display Inode number of File or Directory


We can see some number printed before file / directory name. With -i options list file / directory
with inode number.

# 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

4) The Who Command


Display who is on the system.
The basic syntax is as follows:
who
who am i
who [options] [File]
who --help
who --version
who | grep 'userNameHere'
Where,
 If no non-options provided, who displays the following information for each user currently
logged on:
o login name
o terminal line
o login time
o remote hostname or X display
 If you give one non-option argument, who uses that instead of a default system-maintained
file such s /var/run/utmp as the name of the file containing the record of users logged on.
 If given two non-option arguments, who prints only the entry for the user running it
preceded by the hostname. Traditionally, the two arguments given are ‘am i’, as in ‘who am
i’.
To display line of column headings pass the -H option:
$ who –H

To show only hostname and user associated with stdin (usually keyboard), enter:
$ who –m

To show active processes spawned by init:


$ who –p

To show user’s message status as +, – or ?, enter:


$ who -T
users logged in
Type the command:
$ who –u
Show time of last system boot
To display time of last system boot pass the -b option to who command:
$ who -b
Sample outputs:
system boot 2014-01-05 10:02
The output in this example, shows that the system was booted since 10:02 on 05 January.
processes on the system
You need pass the -d option to who command:
$ who -d
OR
$ who -d -H

system login processes


To just display system login processes pass the -l option:
$ who -l
OR
$ who -l -H

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

UNIX Date Command Examples and Syntax


The syntax is:
date
date "+format"

Display Current Date and Time


Type the following command:
Date
Sample outputs:
Tue Oct 27 15:35:08 CDT 2009
When executed without arguments, the date command shows the current date and time.

Set The Current Time


You must run command as root user. To set the current time to 05:30:30, enter:
date 0530.30
Warning: Please don’t change the Unix date and time when the server is running with more than
one user (multi-user mode) or critical services, as it may result in unexpected behaviors.

The syntax is as follows:


date mmddHHMM[YYyy] date mmddHHMM[yy]
Set the date to Oct 25, 12:45 a.m., enter:
date 10250045
Again you must run command as root user. In this example, set the current date and time to Oct
15, 2009 04:30 you type:
date --set="20091015 04:30"

Type the following command:


date '+DATE: %m/%d/%y%nTIME:%H:%M:%S'
Sample outputs:
DATE: 10/27/09
TIME:15:50:44
Try the following examples:
date "+%m/%d/%y"
date "+%Y%m%d"
date +'%-4.4h %2.1d %H:%M'

g date format strings


From the date command man page:
 %a – Displays the locale’s abbreviated weekday name.
 %A – Displays the locale’s full weekday name.
 %b – Displays the locale’s abbreviated month name.
 %B – Displays the locale’s full month name.
 %c – Displays the locale’s appropriate date and time representation (default).
 %C – Displays the first 2 digits of the four-digit year as a decimal number (00-99). A year
is divided by 100 and truncated to an integer.
 %d – Displays the day of the month as a decimal number (01-31). In a two-digit field, a 0 is
used as leading space fill.
 %D – Displays the date in the format equivalent to %m/%d/%y.
 %e – Displays the day of the month as a decimal number (1-31). In a two-digit field, a
blank space is used as leading space fill.
 %h – Displays the locale’s abbreviated month name (a synonym for %b).
 %H – Displays the hour (24-hour clock) as a decimal number (00-23).
 %I – Displays the hour (12-hour clock) as a decimal number (01-12).
 %j – Displays the day of year as a decimal number (001-366).
 %k – Displays the 24-hour-clock hour clock as a right-align, space-filled number (0 – 23).
 %m – Displays the month of year as a decimal number (01-12).
 %M – Displays the minutes as a decimal number (00-59).
 %p – Displays the locale’s equivalent of either AM or PM.
 %r – Displays 12-hour clock time (01-12) using the AM-PM notation; in the POSIX locale,
it is equivalent to %I:%M:%S %p.
 %S – Displays the seconds as a decimal number (00 – 59).
 %T – Displays the 24-hour clock (00-23) in the format equivalent to HH:MM:SS.
 %w – Displays the weekday as a decimal number in the range 0-6 (Sunday = 0). Refer to
the %u field descriptor.

6) The passwd 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

In fields 4 through 7, password ages are expressed in days.

Specifying -a in addition to -S displays password status for all users.


-u, --unlock Unlock the password of the named account. This option re-enables a password by ch
value before the -loption was used to lock it.
-w, --warndays WARN_DA Set the number of days of warning before a password change is required. WARN_DA
YS to the password expiring that a user will be warned that their password is about to ex
-x, --maxdays MAX_DAYS Set the maximum number of days a password remains valid. After MAX_DAYS, the

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.

Files used by passwd


/etc/passwd User account information.
/etc/shadow Secure user account information.
/etc/pam.d/passwd PAM configuration for passwd.

7) Cal UNIX command


cal - To display a calendar.

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

-m Display monday as the first day of the week.


-j Display julian dates (days one-based, numbered from January 1).
-y Display a calendar for the current year.

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.

1.6 Combining Commands


Unix allows you to specify more than one command in the command line. Each command has to
be separated from the other by a;

wc note ; ls –l

You can group command together within parenthesis and redirect the output.

( wc note ; ls –l note) > newlist


1.7 Meaning of Internal & External commands
The UNIX system is command-based i.e things happen because of the commands that you key
in. All UNIX commands are seldom more than four characters long.
They are grouped into two categories:
 Internal Commands : Commands which are built into the shell. For all the shell built-in
commands, execution of the same is fast in the sense that the shell doesn’t have to search
the given path for them in the PATH variable, and also no process needs to be spawned for
executing it.
Examples: source, cd, fg, etc.
 External Commands : Commands which aren’t built into the shell. When an external
command has to be executed, the shell looks for its path given in the PATH variable, and
also a new process has to be spawned and the command gets executed. They are usually
located in /bin or /usr/bin. For example, when you execute the “cat” command, which
usually is at /usr/bin, the executable /usr/bin/cat gets executed.
Examples: ls, cat etc.
If you know about UNIX commands, you must have heard about the ls command. Since ls is a
program or file having an independent existence in the /bin directory(or /usr/bin), it is branded as
an external command that actually means that the ls command is not built into the shell and
these are executables present in a separate file. In simple words, when you will key in the ls
command, to be executed it will be found in /bin. Most commands are external in nature, but
there are some which are not really found anywhere, and some which are normally not executed
even if they are in one of the directories specified by PATH. For instance, take echo command:
$type echo
echo is a shell builtin
echo isn’t an external command in the sense that, when you type echo, the shell won’t look in its
PATH to locate it(even if it is there in /bin). Rather, it will execute it from its own set of built-in
commands that are not stored as separate files. These built-in commands, of which echo is a
member, are known as internal commands.

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.

 -t : This option will display a single word as an output.


 alias – if command is a shell alias
 keyword – if command is a shell reserved word
 builtin – if command is a shell builtin
 function – if command is a shell function
 file – if command is a disk file

 -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.

1.9 THE ROOT LOGIN


The "root" account on a Linux computer is the account with full privileges. Root access is often
necessary for performing commands in Linux, especially commands that affect system files.
Because root is so powerful, it's recommended to only request root access when necessary, as
opposed to logging in as the root user. This can help prevent accidental damage to important
system files
Gaining Root Access in the Terminal
Open the terminal. If the terminal is not already open, open it.
Type .su - and press ↵ Enter . This will attempt to log you in as "super user." You can
actually use this command to log in as any user on the machine, but when left blank it will
attempt to log in as root

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.

1.10 Becoming a super user: su command


On a Unix system, the superuser refers to a privileged account with unrestricted access to all files
and commands. The username of this account is root. Many administrative tasks and their
associated commands require superuser status.

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

When the - option is used -p is ignored.

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

You might also like