You are on page 1of 88

1

- 2

.

http://kaniyam.com

GNU/Linux

- 2

2013
2013 .

:
:

. ,

.
.
.
, , www.kaniyam.com
. .
.

:
http://bit.ly/kaniyam

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0


Unported License.

GNU/ Linux .

4

. ,
.
, "
.
, MySQL-
. , ""
.
. ,

.

.
,
GNU/Linux- .
,
,
.
1 , ' GNU/Linux - - 2'
,
.
.

.
,

29 2013

: nithyadurai87@gmail.com
: http://nithyashrinivasan.wordpress.com

1...................................................................................................................................9
Linux - .............................................................................9
2.................................................................................................................................15
Users- ........................................................................................................15
sudo command.............................................................................................................15
adduser command.......................................................................................................16
su command.................................................................................................................17
user- sudo .............................................17
chown command..........................................................................................................19
chgrp command...........................................................................................................20
owner group- .......................................................20
userdel Command........................................................................................................21
Groups ...........................................................................................................................23
/etc/shadow file:......................................................................................................27
3.................................................................................................................................29
Disk Management............................................................................................................29
Partition- :..........................................................................................29
fdisk- :........................................................................................................30
4................................................................................................................................36
Mount Unmount...................................................................................................36
Mount- :........................................................................................36
Unmount :....................................................................................................37
/etc/fstab..................................................................................................................37
Filesystem :......................................................................................................38
Mount Options:.........................................................................................................38
file- mount :.......................................................................................39
file system- repair :.................................................................................41
5.................................................................................................................................42
Process Management......................................................................................................42
ps command.................................................................................................................42
pstree command..........................................................................................................42
top command................................................................................................................43
Kill command................................................................................................................44
6.................................................................................................................................45
:.............................................................45
:.....................................................................................45
repositories:..............................................................................................................45
repo- ...................................................................................46
Source- :........................................................................................46
:..................................................................................46
:..........................................................................................47
- upgrade:................................................................................................48

6
:...........................................................................................................48
:.......................................................................48
Deb file :..................................................................................................48
7.................................................................................................................................50
Shell Programming .............................................................................50
Shell Script- ........................................................................................50
Shell Script- .............................................................................................51
Variables........................................................................................................................51
Global Variables............................................................................................................51
Local Variables..............................................................................................................52
local variable - :......................................................................52
Local variables- Global variables- .................................................53
Special Variables..........................................................................................................54
8.............................................................................................................................55
Shell Script- 2....................................................................................................................55
Hash # .........................................................................................55
Escape Mechanism .....................................................................................................55
Command substitution................................................................................................56
Positional Parameters..................................................................................................57
File descriptors.............................................................................................................58
Shift Command.............................................................................................................61
exit Command..........................................................................................................61
Numerical Comparisons..............................................................................................61
Arithmetic Operators & Expressions........................................................................62
Conditional Operators.................................................................................................63
9.................................................................................................................................64
Shell Scripting 3............................................................................................................64
Programming language Constructs..............................................................................64
If Statement..................................................................................................................64
case Statement............................................................................................................65
for Loop..........................................................................................................................67
while Loop......................................................................................................................69
until Loop.......................................................................................................................70
Functions........................................................................................................................72
10...............................................................................................................................74
Job Scheduling..................................................................................................................74
at command..................................................................................................................74
crontab ..........................................................................................................................74
11...............................................................................................................................77
Rsync backup .........................................................................................77
;......................................................................................................77
.....................................................................................................................85
.......................................................................................................................85
........................................................................................................................85
.............................................................................................................86
........................................................................................................86

7
30, 2013
, .

: . .. , ,
, ( )
: . , 2004- ,
. ( )


,
,
- , . ..
100 (6500 ) .
, " MySQL" 5000
.

: http:// jasanpictures. com

Linux -
, Linux
.
.
Linux 20- 70- .
1969- AT&T subsidiary- Bell
Laboratories Unix .
unix- Linux .
Linux .
GNU Minix .
GNU
1983- Richard M Stallman
GNU .

Cambridge- Massachusetts Institute
of Technology Artificial
Intelligence
.
Stallman
.

source code-

. .
AT&T unix- $40,000
.
" unix- source
code- "
.
. Stallman- .
source code

source code -

10
. Stallman-
.
.
1983- Stallman GNU
.
.
Stallman :
========
, 27, 1983 12:35:59 EST
GNU (GNU is Not Unix) Unix compatible software system-

. ,
,
.
GNU kernel C Programs-
utilities- . editor, shell, C Compiler,
linker, assembler . text editor,
a YACC, an Empire game, spreadsheet applications-
.
online hardcopy documentation- .
GNU- unix . unix-
.

.
==========
.
GNU GNU is Not Unix . Unix
.

. unix-
windows- . , Stallman
Manifest GNU GPL (General Public License)
.
GPL-
() .

11


(Freedom #0)

,
(Freedom #1)

(Freedom #2)

Program- , programs-
(Freedom #3).
.
.
GPL
.
,
.
1990- GNU
components . Emacs
text editor, Compiler gcc, GNU C Compiler debugger gdb
stallman .
compiler
, .
compilers, C++, Pascal Fortran
. GCC GNU Compiler
Collection .
Richard Stallman- 1990- GNU System-
. component- kernel
. kernel- (GNU Hurd )
Mach- set of server process- implement
. Mach Carnegie Mellon Utah
micro kernel .
kernel
.

12
Minix:
1990- Intel Microprocessor-, Microsoft-
. Microsoft
desktop , server . Intel
Intel compatible processors- , unix-
workstations- .
unix based system- versions,
Intel- .
Unix- processors- (eg: SGI & MIPS)
. Unix-
Intel Processor- OS . 1987-
Andrew Tanenbaum Minix OS-
. Netherlands- , Amsterdam
Vrije University-
. Minix-
OS-
.
OS
. C Assembly language-
12,000
.
Tanenbaum- "Operating Systems: Design and Implementation"
Minix
. Linus Torvalds
Minix- Linux- .
Linux:
1991- Linus Benedict Torvalds Finland , Intel
Processor- Unix
kernel- . Minix
prototype- .
Minix-
kernel- GPL
. Linus Torvalds kernel-
Linux .
1991- August 25- , Minix news group-
Torvalds- .
.

13
:
Minix
!!!

.
. GNU
.
.
Minix- .
Minix-
.

.

.
bash(1.08) gcc(1.40)- port .
.
torvalds@kruuna.helsinki.fi .
================
.
Torvalds Freax
kernel/makefile 0.11- programs- .
Ari lemmke FTP site- system- load
directory- pub/OS/Linux . Linux
OS- .
1991- Torvalds -
.
Torvalds- .
1992- Linux - ,

.
Torvalds-
. Torvalds .
Linux- . Linux
. Linux - GPL
,
Linux versions-

14
.
Linux .
compile distribution format-
.
Linux GNU GNU/Linux Distribution
.
Red Hat, Caldera
. Debian
distribution . X-windows system, KDE,
GNOME GUI- distributions-
.
Torvalds
. version 0.01- acknowledgement
copyrights- . , GNU
license- .
Linux-
.
1992- February GNU/Linux distributions- floppy
disk-
, floppy disk-
.
linux .
GNU
GNU/Linux- .
GCC Compiler- linux .
GPL . Torvalds version
0.12- GPL .
GNU/Linux
GNU/Linux-
. GNU/Linux
.

15

Users-
user management- commands-
.
root ?
Ubuntu- install
root account- . root-
disable ubuntu developers
. root- .
sudo tool- .
sudo- root-
.
Ubuntu- install /etc/sudoers
file- . sudo
admin group-
.

sudo command
sudo- root user- command
.
root- sudo-
. sudo- , root user
.
root-
. sudo-
. adduser command- .

16

adduser command
sudo adduser command-
user- . .
$ adduser harini

, harini user .

users /etc/passwd
. harini
user .
$ cat /etc/passwd

password x . /etc/shadow
users- encrypt password-
. .

17
$ sudo cat /etc/shadow

su command
su switch user . su
.
logout login .
$ su harini

user- sudo
root user sudo user, user--
sudo . harini- sudo
, /etc/group- harini- .
.

18

nano vi- editor . nano-


file- open .

arrow mark key- sudo- entry


. sudo- entry
comma- 'harini' type ctrl+O
. file- overwrite .

19

harini- sudo

chown command
chown command- file- owner- .
LKG file- user, Nithya . Harini
, command- .
$ chown harini LKG

20

chgrp command
chgrp command- file- group- .
LKG file- group, Nithya . Harini
, command- .
$ chgrp harini LKG

owner group-
chmod command owner group-
.
$ chmod nithya:shrini LKG

21

owner- nithya-, group- shrini- .


colon- dot- .

userdel Command
userdel command- user- .
$ userdel harini

Password Policy:
password .
. password 4
, entropy checks-
.

22

/etc/pam.d/common-password file-
.

password :
user- , password-
.
password- .
password-
command .
$ sudo chage -l shrini

23

command- .
$ sudo chage shrini

password- .
sudo chage -l shrini
.

Groups
groups- command
. groups
groups- .

24
$ cat /etc/group

groups- command
.
$ groups shrini

group- commands .
$ sudo addgroup clg
$ sudo delgroup clg

25
clg group ,
.
group- :
$ sudo delgroup --only-if-empty group

user- group- :
$ sudo usermod -G group -a user

User :
user- backup .
$ sudo deluser --backup-to /location/to/put/backup/user
--remove-home user

User-
user- home directory
$ sudo deluser --remove-home user

26
user, home directory user- ownership file-

$ sudo deluser --remove-all-files user

Group-
user- Group- .
$ id user

/ etc/ passwd file


/etc/passwd text file .
file- . root .
system user human user .

/ etc/ passwd- :

. file- :
. 7 .

27
1. username :
2. password : . x . /etc/shadow
file- .
3. User id (UID) : user id ( ) . 0
root- . 1-999 system user-
human user- .
4. Group id (GID) : , (Primary group id
/etc/group ) - .
5. User Id info: . .
, , . finger
.
6. home directory: home directory ().
.
7. command / shell: login command
shell /bin/bash. command .

$ less /etc/passwd

$ grep nithya /etc/passwd

/ etc/ shadow file:


/etc/passwd- . /etc/shadow
file- .
encryption . root
. :
.

28

1. Username :
2. Password

: ( encryption )

3. last password change : Jan 1, 1970



4. Minimum
: .
.
5. Maximum
: .
.
6. Warn

7. Inactive

: ,

8. expire

md5 algorithm encryption


.

29

3
Disk Management
Hard disk .
/dev/sda
/dev/sdb
/dev/sdc
/dev/sdd

:
:
:
:

Primary Master
Primary slave
Secondary Master
Secondary slave

hard disk- /dev/sda .


partition .
/dev/sda1
/dev/sda2
/dev/sda3
/dev/sda4
...
4 primary partition . 4 extended
, logical partition- .
fdisk , partition-
.
: . root
. ,
. , .
.

Partition- :
partition-
$ sudo fdisk-l

30

1. disk- partition-
$ sudo fdisk -l /dev/sda

fdisk- :
, partition .
. m
# fdisk

/dev/sda

The number of cylinders for this disk is set to 9729.


There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Command (m for help): m
Command action
a
toggle a bootable flag
b
edit bsd disklabel
c
toggle the dos compatibility flag
d
delete a partition

31
l
m
n
o
p
q
s
t
u
v
w
x

list known partition types


print this menu
add a new partition
create a new empty DOS partition table
print the partition table
quit without saving changes
create a new empty Sun disklabel
change a partition's system id
change display/entry units
verify the partition table
write table to disk and exit
extra functionality (experts only)

2. partition-
# fdisk /dev/sda
The number of cylinders for this disk is set to 9729.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Command (m for help): p
Disk /dev/sda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xf6edf6ed
Device Boot
/dev/sda1
/dev/sda2
/dev/sda3
/dev/sda4
/dev/sda5
*
/dev/sda6
/dev/sda7
/dev/sda8
/dev/sda9

Start
1
1960
5284
6529
1960
2662
2905
3148
3265

End
1959
5283
6528
9729
2661
2904
3147
3264
5283

Blocks
15735636
26700030
10000462+
25712032+
5638752
1951866
1951866
939771
16217586

Command (m for help): d


Partition number (1-9): 8
Command (m for help): d
Partition number (1-8): 7
Command (m for help): d
Partition number (1-7): 6
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.

Id
c
f
7
c
83
83
83
82
b

System
W95 FAT32 (LBA)
W95 Ext'd (LBA)
HPFS/NTFS
W95 FAT32 (LBA)
Linux
Linux
Linux
Linux swap / Solaris
W95 FAT32

32
WARNING: Re-reading the partition table failed with error 16: Device or resource
busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

3. partition-
partition- , cylinder ,
partition- () cylinder .
p, print .

# fdisk

/dev/sda

The number of cylinders for this disk is set to 9729.


There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Command (m for help): n
First cylinder (2662-5283, default 2662):
Using default value 2662
Last cylinder, +cylinders or +size{K,M,G} (2662-3264, default 3264):
Using default value 3264

w .

Command (m for help): w


The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource
busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

mkfs format .

33
# mkfs.ext3 /dev/sda7

4. boot flag-
partition- boot a .
.

# fdisk /dev/sda
The number of cylinders for this disk is set to 9729.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Command (m for help): p
Disk /dev/sda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xf6edf6ed
Device Boot
/dev/sda1
/dev/sda2
/dev/sda3
/dev/sda4
/dev/sda5
*
/dev/sda6
/dev/sda7

Start
1
1960
5284
6529
1960
3265
2662

End
1959
5283
6528
9729
2661
5283
3264

Blocks
15735636
26700030
10000462+
25712032+
5638752
16217586
4843566

Id
c
f
7
c
83
b
83

System
W95 FAT32 (LBA)
W95 Ext'd (LBA)
HPFS/NTFS
W95 FAT32 (LBA)
Linux
W95 FAT32
Linux

Id
c
f
7
c
83
b
83

System
W95 FAT32 (LBA)
W95 Ext'd (LBA)
HPFS/NTFS
W95 FAT32 (LBA)
Linux
W95 FAT32
Linux

Partition table entries are not in disk order


Command (m for help): a
Partition number (1-7): 5
Command (m for help): p
Disk /dev/sda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xf6edf6ed
Device Boot
/dev/sda1
/dev/sda2
/dev/sda3
/dev/sda4
/dev/sda5
/dev/sda6
/dev/sda7

Start
1
1960
5284
6529
1960
3265
2662

End
1959
5283
6528
9729
2661
5283
3264

Blocks
15735636
26700030
10000462+
25712032+
5638752
16217586
4843566

34
Partition table entries are not in disk order
Command (m for help):

5. partition order-
logical partition- ,
, . Partition out of order. Partition table
entries are not in disk order.
Sda6, sda7, sda8 .
sda6 , sda7 . sda9 ,
sda6 . f .
.

$ fdisk

/dev/sda

The number of cylinders for this disk is set to 9729.


There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Command (m for help): p
Disk /dev/sda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xf6edf6ed
Device Boot
/dev/sda1
/dev/sda2
/dev/sda3
/dev/sda4
/dev/sda5
*
/dev/sda6
/dev/sda7

Start
1
1960
5284
6529
1960
3265
2662

End
1959
5283
6528
9729
2661
5283
3264

Blocks
15735636
26700030
10000462+
25712032+
5638752
16217586
4843566

Partition table entries are not in disk order


Command (m for help): x
Expert command (m for help): f
Done.
Expert command (m for help): w
The partition table has been altered!

Id
c
f
7
c
83
b
83

System
W95 FAT32 (LBA)
W95 Ext'd (LBA)
HPFS/NTFS
W95 FAT32 (LBA)
Linux
W95 FAT32
Linux

35
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource
busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

36

4
Mount Unmount
Partition- .
file system . ext3, ext4,
ReiserFS, Btrfs / filesystems.
Journalins Filesystem .
index , file
index . , , hard
disk- .
mkfs file system .
sudo mkfs.ext4 /dev/sda5
sudo mkfs.reiserf5 /dev/sda6
file system mount
.
mount raw- device/partition
/ folder .
Mount disk partition, Linux network share, windows network share, iso file
cd/dvd folder .

Mount- :
root sudo
.

1:
sudo mount /dev/sda5 /songs
/dev/sda5 /songs- mount . ls /songs
cd /songs /dev/sda5 file- .

37

2:
sudo mount -t iso9660 movie.iso /media/movie
movie.iso ISO file- /media/movie folder- mount .
folder- mount . mount point .
mount filesystem- , mount
.

Unmount :
mount filesystem unmount
.
root- . mount file
folder , unmount
.
sudo umount

/media/movie

/ etc/ fstab
boot filesystem- mountpoint- mount
. /etc/fstab file .
6 column .
filesystem .

38

Filesystem :
filesystem :
ext3/ext4
iso9660
proc
vfat
auto
nts
smbfs

: / file system
: iso file
: proc virtual file system
: fat 16, fat 32 file system
: filesystem
: network file system
: network file system

Mount Options:
Mount -o , .
/etc/fstab- .
noauto
: boot mount .
cd/dvd .
ro
: Read-Only mount
users : root mount () unmount
.
user
: , mount unmount

39
/etc/fstab- 5 column, dump .
dump, restore backup . filesystem
1 , cd/dvd/usb removable media- 0 .
6 column- pass-no . boot file system check
.
root / 1
boot- mount file system- 0
file system- 2

file- mount :
iso file file- device mount .
1.44 MB file .
dd if=/dev/zero of=disk.img bs=1024 count=1400
dd = disk dump
if = input file
of = output file
bs = block size
count = count of dump operation
file- .
file system back level copy .
dd if=/dev/sdb1 of=usb.img
mount -o loop usb.img /mnt/disk

40

disk :
disk space :
df

disk space- , . Mount point .


df -h

-h human readable MB, GB .

folder- :
du <foldername>
folder- .
du -h
du -sh

41

file system- repair :


, ,
, filesystem .
Hardware -
. .
fsck <option> <device>
sudo fsck /dev/sda5

.
sudo fsck -y -f /dev/sda5
-y = yes for questions
-f = force
fdisk file system unmount .

42

5
Process Management

process . parent process child process .
init process .
process- pid refer .

ps command
ps command-
process- .
ps

ps ax, ps -aux, ps -ef, ps -l


.

pstree command
pstree command-
process-, parent process child process-
tree .
pstree

43

top command
top command- process
process- . output-
real time monitoring .
$ top

44

Kill command
process- .
process- signal-

$ kill 2180
screenshot- terminal- pid 2180
. kill 2180 , terminal
.

killall <program name>


, program , kill .

45

:
synaptic, software center GUI
, command- .

Apt
, ,
. user interface, module, library, translation
.
. dependancy .
Package management system
, , .
Redhat distribution- yum , debian, ubuntu APT, Arch
linux- pacman .

:
sudo apt-cache search [search term 1] [search term 2] [search
term n]

repositories:
debian, ubuntu, centos, suse distribution
package .
repository .
repository- .
, .

46
debian, ubuntu- /etc/apt/sources.list repository .

repo-
sudo add-apt-repository [repository name here]
sudo add-apt-repository ppa:tualatrix/ppa

Source- :
repo- repo .
sudo apt-get update

:
:
sudo apt-get install [package name 1] [package name 2] [package
name n]

47

,
repository- .

:

sudo apt-get remove [package name 1] [package name 2] [package
name n]

48
configuration file directory
purge .
sudo apt-get remove purge <name1> <name2>

- upgrade:

sudo apt-get upgrade <name1> <name2> ... <name n>
upgrade
sudo apt-get upgrade
, ,
.
sudo apt-get -s upgrade
, simulate .

:
repo- deb file-
.
sudo apt-get clean

:
sudo dpkg list
,
.

Deb file :

49
debian/ubuntu packages deb .
,
.

sudo dpkg -i <software.deb>

50

7
Shell Programming
, shell-
, program- .
shell- programming language-
. programming language-
programming constructs, logical conditional operators, command
substitution, escape mechanisms, positional parameters
shell- .

Shell Script-
unix- commands-
. commands
"shell script"
.
date, ls, uname, echo unix commands-
'example1' file- . .

example1 shell script .


script- , output- .

51

Shell Script-
shell script- .
1: sh unix command- script file- argument-
shell script- . .

example1 shell script-


unix commands- output- .
2: chmod unix command file
execute permission- script file-
execute . .

Variables
Unix-, variables-, local variables global variable
.
.

Global Variables
Global Variables unix
,
. environment variables .
PATH variable- directory-
. PS1 variable- shell prompt-
, HOME variable- home directory-
.
$ (dollar) variables- ,
.

52
$ $PATH
$ $PS1
$ $HOME

Local Variables
shell environment- variable- local variable
. shell environment- ,
. 'shell variables'
.

local variable - :
a,b 2 variable- shell environment-
.

variables- echo .
.

53

variables- shell script- ,


.

variables . local variables


shell environment- shell variables.
global variables- ,
.
environment- shell variables
set command- .
$ set

Local variables- Global variables-


local variable- global variable- export command
.
a,b 2 local variable- global variable-
, shell environment- export command- .
$ export a
$ export b

54
variables- shell script- .
.

Special Variables
Local variable global variable unix-
variable- .
$# positional parameters
.
$- shell options- ,
$? execute command- exit .
$$ shell- process .
$! background command- process .
$0 execute command- .
$* Positional parameters- .

55

8
Shell Script- 2

Hash #
# type ,
type , execution
.
shell script
, # ,
shell script- .
ec.sh file- comment- ,
run .

output- comment-
.

Escape Mechanism
. *
(Asterisk symbol) echo .

56

echo command- *- .
* unix- special character.
* , Unix
. character-
unix- .
special characters- character-
unix- , character- backslash
(\) .

* .
escape mechanism .

Command substitution

. unix command- echo .

57
ls .
ls command- backticks- echo
.

ls command- output- echo


. command substitution .

Positional Parameters
Shell script- command line- arguments-
. shell script- $1
argument-, $2 argument-, $3
argument- .
$0 shell script- , $* command
line- arguments- .
$1,$2,$3 , arguments- position-
, positional parameters .
arguments.sh shell script- .

58

arguments.sh- command line- arguments-


.

arguments , shell script- positional


parameters- output- .

File descriptors
File descriptors 0,1 2 standard input,
standard output standard error- .
shell script- , commands
, error messages ,
program .

59
descriptors- , error messages
, file- ,
program .
man xyz command error-
. error descriptor- 2-
file- .
$ man xyz 2> file2

man ls xyz command, ls- output- , xyz-


error- . , output- file-,
error- file- .
$ man ls xyz 1> opfile 2> errfile

60

output error file- command-


.
$ man ls xyz > operrfile 2>&1

61

Shift Command
shell script- $1 $9 positional
parameters- . ,
shift command- .

exit Command
exit command- shell script- .
shell script-
0-, 1- .

Numerical Comparisons
shell script-
. , 3 5
.
3 -eq 5 (3 5 (equals) )
3 -ne 5 (3 5 (not equals) )
3 -gt 5 (3- 5- (greater than) )
3 -lt 5 (3- 5- (greater than) )
3 -ge 5 (3- 5-
(greater than or equal to) )
3 -le 5 (3- 5-
(greater than or equal to) )
command line- .
$
$
$
$

[
[
[
[

3
3
3
3

-eq
-eq
-lt
-lt

3
5
5
2

]
]
]
]

&&
&&
&&
&&

echo
echo
echo
echo

true
true
true
true

62

command- 3 -eq 3 true


. 3 -eq 5 true
. commands-
.

Arithmetic Operators & Expressions


, , , +, -, *, /
arithmetic operators .
shell script- .
.
$
$
$
$

expr
expr
expr
expr

6
6
6
6

+ 4
- 4
\* 4
/ 4

* operators- , \
escape operator- .
, / operator- ,
,
.

63

Conditional Operators
commands- successful execution-
failed execution- commands- execute
. && || conditional
execution operators .
&& operator- successful execution- ,
|| operator- failed execution-
commands- execute .
$ grep Apple fruits && echo found || echo Not found
$ grep Mango fruits && echo found || echo Not found

64

9
Shell Scripting 3
Programming language Constructs
Shell- programming language- looping & branching,
operations on variables, file creation argument passing
. shell-, DOS-
batch files- .

If Statement
If Construct- condition- set of commands-
execute .

syntax:
if (condition)
then
commands
else
commands
fi

:
example2.sh file- if statement- ,
run .

65

fruits file- Apple


if command
.
fruits file- absolute path
. shell script- absolute path
files- .

case Statement
Case statement option- ,
commands- execute .

syntax:
case value in
choice1) commands; ;
choice2) commands; ;
. . .
esac

:
example3.sh file- case statement- ,
run .

66

options echo statement


. read function
input , choice variable-
.
case statement choice variable- 1 pwd
command-, 2 date-, 3 who command-
execute . * 1,2,3
,
" message echo
statement .
input- , output
.

67

for Loop
list-
output- .

syntax:
for <variable> in
do
Commands
done

<looping condition>

:
example4.sh file- for loop- , run
.

68

seq- argument 1 10 command, 1 10


.
command- backticks
output- for loop- condition- .
1 10 loop- statement
execute , output .

69
.

while Loop
while loop- condition loop-
commands- execute .

syntax:
while <looping condition>
do
Commands
done

70

:
example5.sh file- while loop- ,
run .

i variable- 1 assign .
while loop- condition , i variable,
5 loop- execute
.
loop- i variable
. i- 5-
loop output .

until Loop
until loop- while loop- . until
loop- condition loop- commands-
execute .

71

syntax:
until <looping condition>
do
Commands
done

:
example6.sh file- until loop- , run
.

output .

72

Functions
shell script- function
. commands-
, logical
function- .
functions-
.
function input , parameter
.

Syntax:
function_name () {
commands
}

:
functions.sh file- function- , run
.

73

value1 , value 2 2 variables- read function


2 . 2
add(), sub(), mul(), div() 4 functions
.
functions- 2
value1 value2 2 variables function-
parameter- .
output .

74

10
Job Scheduling
Shell- jobs- schedule at crontab 2
commands . .

at command
command/shell script execute
schedule
at command .
$ touch india | at 16.39
touch command 4:39 PM- india file-
.

crontab
Crontab- command/shell script-,
execute .
crontab- file . crontab -e
file- open .
.

minute - (0 59 )
hour - (0 23 )
day of month - (1 31 )
month - (1 12 )
day of week - (0 6 ) (0=Sunday)

75
touch command 25- 4. 40
crontab-
command .
$ crontab -e
40 16 25 * * touch trade
(press)Esc:wq
(press)Enter

40 , 16 ,
25 . *
, * .
crontab- schedule commands
l option- . .

$ crontab -l

76

77

11

Rsync backup

. ,
.
rsync ,
, , .

;
:
source file directory
destination . , Source
file directory . backup
, .
:
, ssh
encryption - - , .
bandwidth:
, .
(compress). , .
(extract). bandwidth .
:
. root
.

Syntax:
rsync <options> <source> <destination>
source destination , network
. , , IP
hostname destination full path
.

78

- 1;
directory sync .
-zvr options .

$ rsync -zvr /var/opt/installation/inventory/ /root/temp


building file list ... done
sva.xml
svB.xml
.
sent 26385 bytes received 1098 bytes 54966.00 bytes/sec
total size is 44867 speedup is 1.63
$

,
-z = compress -
-v = verbose -
-r = recusrsive - .
timestamp .
$ ls -l /var/opt/installation/inventory/sva.xml /root/temp/sva.xml
-r--r--r-- 1 bin bin 949 Jun 18 2009 /var/opt/installation/inventory/sva.xml
-r--r--r-- 1 root bin 949 Sep 2 2009 /root/temp/sva.xml

. .

2: metadata sync .
Rsync -a option
sync .
-a = archive mode
symbolic link
permissions
timestamp
owner, group
-a .

79
$ rsync -azv /var/opt/installation/inventory/ /root/temp/
building file list ... done
./
sva.xml
svB.xml
.
sent 26499 bytes received 1104 bytes 55206.00 bytes/sec
total size is 44867 speedup is 1.63
$

timestamp sync .
$ ls -l /var/opt/installation/inventory/sva.xml /root/temp/sva.xml
-r--r--r-- 1 root bin 949 Jun 18 2009 /var/opt/installation/inventory/sva.xml
-r--r--r-- 1 root bin 949 Jun 18 2009 /root/temp/sva.xml

3: file- rsync
$ rsync -v /var/lib/rpm/Pubkeys /root/temp/
Pubkeys
sent 42 bytes received 12380 bytes 3549.14 bytes/sec
total size is 12288 speedup is 0.99

4: local remote sync


remote , , IP () hostname,
destination directory path .
username@hostname: /full/path/to/destination.
remote password ( )
rsync .
'Passwordless ssh' , password , remote
rsync .

$ rsync -avz /root/temp/ shrini@192.168.200.10:/home/shrini/temp/


Password:
building file list ... done
./
rpm/
rpm/Basenames
rpm/Conflictname
sent 15810261 bytes received 412 bytes
total size is 45305958 speedup is 2.87

2432411.23 bytes/sec

80

5: remote local rsync .


,
source = remote
destination = local
.
$ rsync -avz shrini@192.168.200.10:/var/lib/rpm /root/temp
Password:
receiving file list ... done
rpm/
rpm/Basenames
.
sent 406 bytes received 15810230 bytes 2432405.54 bytes/sec
total size is 45305958 speedup is 2.87

6: remote shell
-e option remote shell .
-e ssh encryption rsync .
$ rsync -avz -e ssh shrini@192.168.200.10:/var/lib/rpm /root/temp
Password:
receiving file list ... done
rpm/
rpm/Basenames
sent 406 bytes received 15810230 bytes
total size is 45305958 speedup is 2.87

2432405.54 bytes/sec

7: Destination- , overwrite
.
destination- File . rsync
source- . -u option
.
$ ls -l /root/temp/Basenames
total 39088
-rwxr-xr-x 1 root root

4096 Sep

2 11:35 Basenames

$ rsync -avzu shrini@192.168.200.10:/var/lib/rpm /root/temp


Password:
receiving file list ... done

81
rpm/
sent 122 bytes received 505 bytes 114.00 bytes/sec
total size is 45305958 speedup is 72258.31
$ ls -lrt
total 39088
-rwxr-xr-x 1 root root

4096 Sep

2 11:35 Basenames

8: directory structure- rsync


Directory Tree (File ) rsync -d .
$ rsync -v -d shrini@192.168.200.10:/var/lib/ .
Password:
receiving file list ... done
logrotate.status
CAM/
YaST2/
acpi/
sent 240 bytes received 1830 bytes
total size is 956 speedup is 0.46

318.46 bytes/sec

9: Progress
file- rsync , .
rsync .
--progress .
rsnapshot .
$ rsync -avz --progress shrini@192.168.200.10:/var/lib/rpm/ /root/temp/
Password:
receiving file list ...
19 files to consider
./
Basenames
5357568 100%
14.98MB/s
0:00:00 (xfer#1, to-check=17/19)
Conflictname
12288 100%
35.09kB/s
0:00:00 (xfer#2, to-check=16/19)
.
.
.
sent 406 bytes received 15810211 bytes 2108082.27 bytes/sec
total size is 45305958 speedup is 2.87

82

10: Destination- delete


source- file delete , target-
delete . .
delete --delete .

# Source and target are in sync. Now creating new file at the target.
$ > new-file.txt
$ rsync -avz --delete shrini@192.168.200.10:/var/lib/rpm/ .
Password:
receiving file list ... done
deleting new-file.txt
./
sent 26 bytes received 390 bytes 48.94 bytes/sec
total size is 45305958 speedup is 108908.55

11: rsync
source file , target
file rsync . --existing .
[/var/lib/rpm ]$ > new-file.txt
$ rsync -avz --existing root@192.168.1.2:/var/lib/rpm/ .
root@192.168.1.2's password:
receiving file list ... done
./
sent 26 bytes received 419 bytes 46.84 bytes/sec
total size is 88551424 speedup is 198991.96

12: source/destination
-i .

Source:
$ ls -l /var/lib/rpm
-rw-r--r-- 1 root root
-rw-r--r-- 1 root root
-rw-r--r-- 1 root root

5357568 2010-06-24 08:57 Basenames


12288 2008-05-28 22:03 Conflictname
1179648 2010-06-24 08:57 Dirnames

83

Destination:
$ ls -l /root/temp
-rw-r--r-- 1 root root
-rw-r--r-- 1 bin bin
-rw-r--r-- 1 root root

12288 May 28 2008 Conflictname


1179648 Jun 24 05:27 Dirnames
0 Sep 3 06:39 Basenames

$ rsync -avzi shrini@192.168.200.10:/var/lib/rpm/ /root/temp/


Password:
receiving file list ... done
>f.st.... Basenames
.f....og. Dirnames
sent 48 bytes received 2182544 bytes 291012.27 bytes/sec
total size is 45305958 speedup is 20.76

13:
rsync- ,
.
$ rsync -avz --include 'P*' --exclude '*' shrini@192.168.200.10:/var/lib/rpm/
/root/temp/
Password:
receiving file list ... done
./
Packages
Providename
Provideversion
Pubkeys
sent 129 bytes received 10286798 bytes
total size is 32768000 speedup is 3.19

2285983.78 bytes/sec

14: file size limit


--max-size option file- rsync .

$ rsync -avz --max-size='100K' shrini@192.168.200.10:/var/lib/rpm/ /root/temp/


Password:
receiving file list ... done
./
Conflictname
Group

84
Installtid
Name
Sha1header
Sigmd5
Triggername
sent 252 bytes received 123081 bytes 18974.31 bytes/sec
total size is 45305958 speedup is 367.35

15: file- sync


source, target file rsync file
rsync -w .
network bandwidth . checksum
, .
# rsync -avzW shrini@192.168.200.10:/var/lib/rpm/ /root/temp
Password:
receiving file list ... done
./
Basenames
Conflictname
Dirnames
Filemd5s
Group
Installtid
Name
sent 406 bytes received 15810211 bytes
total size is 45305958 speedup is 2.87

2874657.64 bytes/sec

85



.
, , .
.
.
, ,
.

.
.

.
editor@ kaniyam. com

.
:



.

.
, .

.
,
.
.
, , , , ,

.
.
editor@kaniyam.com
.

86
, .
editor@kaniyam.com .


.

.

.
.

.


2013 .

http://creativecommons.org/licenses/by-sa/3.0/
.
,

, , , , ,
.
: . editor@kaniyam.com +91 98417 95468
.
: . , 4, , 42, ,
- 600 059
. : +91 98417 95468 tshrinivasan@gmail.com
:
LibreOffice Writer 4.0.3.3

Kubuntu Linux

13.04 |

Gimp 2.8

87

Creative Commons
.
.

Name - Nithya Duraisamy


ICICI - 006101540799
Branch - Mcity branch, chengalpattu.
IFSC code - ICIC0000061

88

GNU/ Linux

- - 3

Basics of Networking
Basic Network Settings
Domain Name Server
Network File Server
Dynamic Host Control Protocol
Printing
Apache Web server
Ssh
.

You might also like