You are on page 1of 316

shell

Web shell shell LAN

Linux shell

[] Shantanu Tushar Sarath Lakshman

11
100164315@ptpress.com.cn
http://www.ptpress.com.cn

80010001/16
19.5
460
1 5 000

2014 1 1
2014 1 1

01-2013-5214

59.00
(010)51095186600(010)81055316
(010)81055315
0021

2
3

Copyright 2013 Packt Publishing. First published in the English language under the title Linux Shell
Scripting Cookbook Second Edition.
Simplified Chinese-language edition copyright 2014 by Posts & Telecom Press. All rights reserved.
Packt Publishing

4
5
6
7
8
9
10
11
12
13

Shantanu Tushar

2
3

GNU/Linux

shellLinux
shell
shell

shellshell

4
5
6
7

1Bash
Bash
2GNU/Linux

cryptographic hashing command


3
ISO

8
9
10
11

4GNU/Linux
sed/awk

12

5shell
shellWebWeb

13

POSTGET
TwitterOAuth
6B

7Linuxshell
sshGNU/Linux
raw
communication channel
8Linux
CPU

9
shellMySQL
GNU Screen

GNU/Linux
Linux

Ubuntu/Debian LinuxLinux
shell

shellLinux

repeatwhile
$@

if

[ $var -eq 0 ]; then echo "True"; fi

if

test $var -eq 0 ; then echo "True"; fi

while read line;


do something
done < filename

5
6

# mkdir /mnt/loopback
# mount -o loop loopbackfile.img /mnt/loopback

10

feedback@packtpub.com

11
12

ituring.com.cn/book/1170

13

www.packtpub. com/authors

Packt

http://www.packtput.comPackt
http://www.packtput.com/support


http://www.packtpub.com/submit-errata

http://www.packtpub.com/support

Packt

copyright@packtpub.com

questions@packtpub.com

3
4

. ...................................... 1

1.1 ...............................................................1
1.2 . ......................................................3
1.2.1 ....................................................4
1.2.2 ....................................................5
1.2.3 ....................................................5
1.3 ...................................6
1.3.1 ....................................................6
1.3.2 ....................................................7
1.3.3 ....................................................9
1.4 . ...........................10
1.4.1 . ................................................11
1.4.2 . ................................................11
1.5 shell . .........................11
1.5.1 . ................................................12
1.5.2 . ................................................12
1.6 . .......................14
1.6.1 . ................................................14
1.6.2 . ................................................14
1.6.3 . ................................................17
1.6.4 . ................................................17
1.7 . .......................................19
1.7.1 . ................................................19
1.7.2 . ................................................20
1.7.3 . ................................................20
1.8 . ....................................................21
1.8.1 . ................................................21
1.8.2 . ................................................22
1.9 . ...........................................23
1.9.1 . .......................................23
1.9.2 . .......................................23

. .........................24
1.10.1 . ...................................24
1.10.2 . ...................................24
1.10.3 . ...................................26
1.10.4 . ...................................26
1.11 . ..................................................27
1.11.1 . ...................................27
1.11.2 . ...................................28
1.11.3 . ...................................28
1.12 ...............................................29
1.12.1 . ...................................29
1.12.2 . ...................................30
1.13 . .................31
1.13.1 . ...................................31
1.13.2 . ...................................32
1.13.3 . ...................................32
1.14 n ..............33
1.15 . .........................34
1.15.1 . ..............................................34
1.15.2 . ..............................................35
1.15.3 . ..............................................35
1.16 ...............................35
1.16.1 . ..............................................36
1.16.2 . ..............................................36
1.17 ...............................................38

1.10

2
2.1
2.2

. .....................................42
.............................................................42
cat . ......................................42
2.2.1 . .....................................43
2.2.2 . .....................................43
2.2.3 . .....................................43

5
6
7
8
9
10
11
12
13
14
15
16
17
18

. ...............................45
2.3.1 . .......................................45
2.3.2 . .......................................45
2.3.3 . .......................................46
2.4 . ...............................46
2.4.1 . .......................................46
2.4.2 . .......................................46
2.4.3 . .......................................47
2.5 xargs. ................................................54
2.5.1 . .......................................54
2.5.2 . .......................................55
2.5.3 . .......................................55
2.5.4 . .......................................56
2.6 tr ..........................................59
2.6.1 . .......................................59
2.6.2 . .......................................59
2.6.3 . .......................................60
2.6.4 . .......................................60
2.7 .............................................63
2.7.1 . .......................................63
2.7.2 . .......................................63
2.7.3 . .......................................64
2.7.4 . .......................................65
2.8 . .......................................65
2.9 . ...................................68
2.9.1 ..................................................68
2.9.2 ..................................................68
2.9.3 ..................................................69
2.9.4 ..................................................69
2.10 ...........................72
2.10.1 ................................................72
2.10.2 ................................................73
2.11 . .....................................73
2.11.1 ................................................74
2.11.2 ................................................74
2.12 ...........................76
2.12.1 ................................................76
2.12.2 ................................................76
2.13 . .................................78
2.13.1 . ...................................78

2.3

2.13.2 . .............................................. 78
2.13.3 . .............................................. 79
2.14 . ............................ 80
2.14.1 . .............................................. 80
2.14.2 . .............................................. 81
2.15 . ..................................... 82
2.15.1 . .............................................. 82
2.15.2 . .............................................. 82
2.15.3 . .............................................. 82
2.15.4 . .............................................. 83
2.16 .................. 84
2.16.1 . .............................................. 84
2.16.2 .................................................... 85

. .................................86

3.1 . ........................................................... 86
3.2 . .............................. 86
3.3 . .......................... 88
3.3.1 . ................................................ 88
3.3.2 . ................................................ 88
3.3.3 . ................................................ 90
3.4 . .............................. 90
3.4.1 . ................................................ 90
3.4.2 . ................................................ 91
3.4.3 . ................................................ 92
3.5 .................... 93
3.5.1 . ................................................ 95
3.5.2 . ................................................ 96
3.6 . .............................. 97
3.6.1 . ................................................ 98
3.6.2 . ................................................ 98
3.7 .................................... 98
3.7.1 . ................................................ 98
3.7.2 . ................................................ 98
3.8 .................... 99
3.8.1 . ................................................ 99
3.8.2 . .............................................. 100
3.9 . ........................ 100
3.9.1 . ..................................... 100
3.9.2 . ..................................... 100
3.9.3 . ..................................... 102


3.10

. .......................................102
3.10.1 ..................................103
3.10.2
3.10.3

3.11

4.6

ISO ISO ................106


3.11.1 ..................................106
3.11.2
3.11.3

3.12

..................................104
..................................104

4.5

..................................106
..................................107

. ...................108
3.12.1
3.12.2

4.7

..................................108
..................................110

3.13

head tail 10
10 . .............................................110
3.14 . .......................113
3.14.1 ..................................113
3.14.2
3.14.3
3.15

..................................113
..................................113

pushd popd
.................................................114
3.15.1
3.15.2

3.17

..................................115
. .....115

.............................................116
3.17.1 ..................................117
3.17.2
3.17.3

4
4.1
4.2

4.3

4.4

4.9

..................................114
..................................114

3.15.3
3.16

4.8

..................................117
..................................118

4.10

4.11

. ...................................119
. .........................................................119
. .....................................119
4.2.1 . .....................................120
4.2.2 . .....................................120
4.2.3 . .....................................121
grep ..................122
4.3.1 . .....................................122
4.3.2 . .....................................125
cut . ...........................128
4.4.1 . .....................................128
4.4.2 . .....................................130

4.12

4.13

4.14

sed . .......................131
4.5.1 . .....................................131
4.5.2 . .....................................132
awk . ................135
4.6.1 . .....................................135
4.6.2 . .....................................135
4.6.3 . .....................................136
4.6.4 . .....................................137
...........................141
4.7.1 . .....................................141
4.7.2 . .....................................141
4.7.3 . .....................................142
4.7.4 . ..............................................142
JavaScript . .......................142
4.8.1 . .....................................142
4.8.2 . .....................................143
4.8.3 . .....................................144
4.8.4 . ..............................................145
. .................................145
4.9.1 . .....................................145
4.9.2 . ..............................................146
n ....
146
4.10.1 . .................................146
4.10.2 ...........................................146
. ...................146
4.11.1 . .................................146
4.11.2 . .................................147
4.11.3 ...........................................147
. ...............................147
4.12.1 . .................................148
4.12.2 . .................................148
4.12.3 . .................................148

URL .........................................................149
4.13.1 . .................................149
4.13.2 . .................................149
4.13.3 ...........................................150
....
150
4.14.1 . .................................150
4.14.2 . .................................150

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

4.15

4.16

5
5.1
5.2
5.2.1
5.2.2
5.2.3
5.2.4
5.3
5.4
5.4.1
5.4.2
5.4.3
5.4.4
5.4.5
5.5
5.5.1
5.5.2
5.5.3
5.6
5.6.1
5.6.2
5.6.3
5.7
5.7.1
5.7.2
5.7.3
5.8

4.14.3 . .................................151
4.14.4 ...........................................151
.... 151
4.15.1 . .................................151
4.15.2 . .................................151
4.15.3 . .................................152
............................152
4.16.1 . .................................152
4.16.2 ...........................................153

. ................ 154

5.8.3 . .............................................. 169


5.8.4 ....................................................... 169
5.9 Twitter . ........................... 169
5.9.1 . .............................................. 169
5.9.2 . .............................................. 171
5.9.3 . .............................................. 173
5.9.4 ....................................................... 173
5.10 Web . ......... 173
5.10.1 . ............................................ 173
5.10.2
5.10.3

...........................................................154
Web ..........................................154

5.10.4

................................................154
................................................155

5.11.1

................................................155
................................................156

5.11.3

...........................157
cURL . ...............................................157
................................................158
................................................158
................................................158
................................................159
. ......................................................161
Gmail . ..............................161
................................................162
................................................162
. ......................................................163
...........................................163
................................................163
................................................164
. ......................................................164
...........................164
................................................164
................................................165
. ......................................................166
. .....................................167
5.8.1
5.8.2

. .....................................167
. .....................................167

5.11
5.11.2
5.11.4

. ............................................ 175
. ............................................ 175
. ................................................... 175
. ...................... 175
. ............................................ 176
. ............................................ 176
. ............................................ 177
. ................................................... 177

. ....................................... 177
5.12.1 . ............................................ 177
5.12

5.12.2
5.12.3

. ............................................ 177
. ............................................ 179

5.12.4 . ................................................... 179


5.13 POST
. ....................................................... 179
5.13.1 .................................. 179
5.13.2 .................................. 180
5.13.3 .................................. 180
5.13.4 . ........................................ 181

B . ...................................... 182

. ......................................................... 182
6.2 tar . ............................................ 182
6.2.1 . .............................................. 182
6.2.2 . .............................................. 183
6.2.3 . .............................................. 183
6.2.4 . .............................................. 184
6.2.5 ....................................................... 188
6.3 cpio . .......................................... 188
6.3.1 . ..................................... 188
6.3.2 . ..................................... 189
6.1


gzip ...............................189
6.4.1 . .....................................189
6.4.2 . .....................................190
6.4.3 ...............................................193
6.5 zip .................................193
6.5.1 . .....................................193
6.5.2 . .....................................194
6.6 pbzip2 ...................194
6.6.1 . .....................................194
6.6.2 . .....................................194
6.6.3 . .....................................195
6.6.4 . .....................................195
6.7 ..................................195
6.7.1 . .....................................196
6.7.2 . .....................................196
6.7.3 . .....................................196
6.8 rsync . ...................197
6.8.1 . .....................................197
6.8.2 . .....................................199
6.8.3 . .....................................199
6.9 Git . .........200
6.9.1 . .....................................200
6.9.2 . .....................................201
6.10 fsarchiver . ........203
6.10.1 ..................................203
6.10.2 ..................................203
6.10.3 ..................................204

6.4

. ...................................205

7.1 . .........................................................205
7.2 . ..................................................205
7.2.1 . ..............................................206
7.2.2 . ..............................................206
7.2.3 . ..............................................207
7.2.4 . .......................................................210
7.3 ping ................................................210
7.3.1 . ..............................................211
7.3.2 . ..............................................212
7.4 ..................213
7.4.1 . .....................................214

7.4.2 . .....................................214
7.4.3 . .....................................215
7.4.4 . .....................................215
7.4.5 . ..............................................216
7.5 SSH . .......216
7.5.1 ................................................216
7.5.2 ................................................217
7.5.3 ................................................219
7.5.4 . .......................................................220
7.6 . .................................220
7.6.1 ................................................220
7.6.2 ................................................220
7.6.3 ................................................221
7.6.4 . .......................................................223
7.7 ...........................................223
7.7.1 ................................................223
7.7.2 ................................................223
7.7.3 ................................................224
7.7.4 . .......................................................225
7.8 SSH . ................225
7.8.1 ................................................225
7.8.2 ................................................225
7.9 SSH . .......................227
7.9.1 ................................................227
7.9.2 ................................................227
7.10 . ......228
7.10.1 . ............................................228
7.10.2 . ............................................228
7.10.3 . .....................................................228
7.11 .............................229
7.11.1 . ............................................229
7.11.2 . ............................................229
7.11.3 . ............................................230
7.11.4 . ............................................230
7.12 .............................................230
7.12.1 . ............................................231
7.12.2 . ............................................231
7.12.3 . ............................................231
7.13 . ...................................231
7.13.1 . .................................232

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

7.14

7.13.2 . .................................232
iptables . .....233
7.14.1 . .................................233
7.14.2 . .................................233
7.14.3 . .................................234

8.11

................................ 235

8.1 ...........................................................235
8.2 . .................................235
8.2.1 ................................................236
8.2.2 ................................................236
8.2.3 ................................................237
8.3 . .................................240
8.3.1 ................................................240
8.3.2 ................................................242
8.4
. .................................243
8.4.1 . .....................................243
8.4.2 . .....................................243
8.5

8.10

1 CPU 10

...........................................................245
8.5.1 . .....................................245
8.5.2 . .....................................245
8.5.3 . .....................................247
8.5.4 . .............................................247
8.6 watch . ...................247
8.6.1 ................................................247
8.6.2 ................................................248
8.7 . .............................248
8.7.1 ................................................248
8.7.2 ................................................248
8.7.3 ................................................249
8.8 logrotate ..............249
8.8.1 ................................................250
8.8.2 ................................................250
8.8.3 ................................................250
8.9 syslog ..............................251
8.9.1 . .....................................251
8.9.2 . .....................................252
8.9.3 . .............................................252

8.12

8.13

8.14

8.15

. .......... 252
8.10.1 .................................. 253
8.10.2 .................................. 253
8.10.3 .................................. 254
.................... 255
8.11.1 .................................. 256
8.11.2 .................................. 256
8.11.3 .................................. 257
8.11.4 . ........................................ 258
. .............. 258
8.12.1 .................................. 258
8.12.2 .................................. 258
8.12.3 .................................. 259
. ...................... 260
8.13.1 .................................. 260
8.13.2 .................................. 260
. ....................................... 261
8.14.1 .................................. 261
8.14.2 .................................. 262
.................... 262
8.15.1 .................................. 262
8.15.2 .................................. 262
8.15.3 .................................. 263

................................... 264

9.1 . ......................................................... 264


9.2 . ......................................... 264
9.2.1 . .............................................. 264
9.2.2 . .............................................. 265
9.2.3 . .............................................. 266
9.2.4 . .............................................. 267
9.2.5 ....................................................... 273
9.3 . ............ 273
9.3.1 . .............................................. 273
9.3.2 . .............................................. 274
9.3.3 . .............................................. 274
9.4 . ............................ 276
9.4.1 . ..................................... 276
9.4.2 . ..................................... 276
9.4.3 . ..................................... 278


9.5 . .........................................278
9.6 proc ...............................279
9.7 cron . ..................................280
9.7.1 . ..............................................280
9.7.2 . ..............................................280
9.7.3 . ..............................................281
9.7.4 . ..............................................282
9.8 Bash MySQL . ...........283
9.8.1 . ..............................................283
9.8.2 . ..............................................283
9.8.3 . ..............................................286
9.9 . .........................................287
9.9.1 . .....................................287

9.9.2
9.10

................................................289
................291
. ............................................291
. ............................................291

. ............................................294
. .....................................................294

.............................................294
9.11.1 . ............................................295

. ............................................295
. .......................................295

. .................................295
. .................................295

9.10.1
9.10.2
9.10.3
9.10.4
9.11

9.11.2
9.12

9.12.1
9.12.2

7
8
9
10
11
12
13
14
15
16
17
18

1.1

1.1

shell

"n"

UnixUnix
shellshell

shell
shell
BashBourne Again ShellGNU/Linuxshell
GNU/LinuxUnix
Linux

shellshellshell

username@hostname$

root@hostname #

$#
$#rootrootLinux

rootshell
shell
$root#
sudosudo <command> <arguments>
root
shellshebang
#!/bin/bash

shebang#!/bin/bashBash
bash

#
$ bash script.sh #

$ bash /home/path/script.sh #script.sh

bashshebang
shebang
#!
$ chmod a+x script.sh

script.sh
$ ./script.sh #./

shebangUnixsharphashmesh#
bang !shebanghttp://en.wikipedia.org/wiki/
Shebang_(Unix)

1.2

$ /home/path/script.sh #

shebang#!/bin/bash/bin/bash

$ /bin/bash script.sh

shell
~/.bashrcshell~/.bash_profileBash
~/.bash_history
~/home/useruserroot

/root
shellshell
GNOMEKDEshellshell
Bash

$ cmd1 ; cmd2

$ cmd1
$ cmd2

#
#

1.2

shell
shell

shell

1.2.1
echo

echo
$ echo "Welcome to Bash"
Welcome to Bash

echo

$ echo Welcome to Bash


Welcome to Bash

$ echo 'text in quotes'

$ echo "cannot include exclamation - ! within double quotes"

bash: !: event not found error

!
\!
$ echo Hello world !

$ echo 'Hello world !'

$ echo "Hello world \!" #

echo;
Bash shell

echo hello;helloecho hellohello

1.2

printfprintfCprintf

$ printf "Hello world"

printfprintf

printfecho

#!/bin/bash
#: printf.sh
printf
printf
printf
printf

"%-5s
"%-5s
"%-5s
"%-5s

%-10s
%-10s
%-10s
%-10s

%-4s\n" No Name Mark


%-4.2f\n" 1 Sarath 80.3456
%-4.2f\n" 2 James 90.9989
%-4.2f\n" 3 Jeff 77.564

No
1
2
3

Name
Sarath
James
Jeff

Mark
80.35
91.00
77.56

5
1.2.2
%s%c%d%fformat substitution character

%-5s5-
-

Name
10Name1010

Mark%-4.2f.22
\n

1.2.3
echoprintf
Bash

1. echo
echo-n
echo
echo e ""
echo -e "1\t2\t3"
1 2 3

2.

=0=30=31=32=33
=34=35=36=37

echo -e "\e[1;31m This is red text \e[0m"

\e[1;31\e[0m31

=0=40=41=42=43
=44=45=46=47

echo -e "\e[1;42m Green Background \e[0m"

1.3

Bash
shell

1.3.1
env

cat /proc/$PID/environ

1.3

PIDIDPID

geditpgrepgeditID
$ pgrep gedit
12501

$ cat /proc/12501/environ
GDM_KEYBOARD_LAYOUT=usGNOME_KEYRING_PID=1560USER=slynuxHOME=/home/slynux

name=value
null\0\0\n
=tr
$ cat /proc/12501/environ

| tr '\0' '\n'

1.3.2

var=value

varvaluevalue

var = valuevar=valuevar=valuevar = value

$
var="value" #var
echo $var

echo ${var}

value

printfecho
#!/bin/bash
#:variables.sh
fruit=apple
count=5
echo "We have $count ${fruit}(s)"

We have 5 apple(s)

HTTP_PROXY

HTTP_PROXY=192.168.1.23:3128
export HTTP_PROXY

exportshell

shell
shell
PATHPATH
$ echo $PATH
/home/slynux/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr
/games

shellPATH
PATH/etc/environment/etc/profile~/.bashrc
PATH
export PATH="$PATH:/home/user/bin"

$ PATH="$PATH:/home/user/bin"
$ export PATH
$ echo $PATH
/home/slynux/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr
/games:/home/user/bin

/home/user/binPATH
HOMEPWDUSERUIDSHELL

1.3

expand

$ echo '$var' will print $var

var$ echo "$var"

2
1.3.3

1.

length=${#var}

$ var=12345678901234567890$
echo ${#var}
20

length

2. shell
shell

echo $SHELL

echo $0

$ echo $SHELL
/bin/bash
$ echo $0
/bin/bash

3.
UID

10

If [ $UID -ne 0 ]; then


echo Non root user. Please run as root.
else
echo Root user
fi

rootUID0
4. Bashusername@hostname:~$
shelluser@hostname:/home/$
GNU/LinuxPS1
shell~/.bashrc
PS1
$ cat ~/.bashrc | grep PS1
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '


slynux@localhost: ~$ PS1="PROMPT>"
PROMPT> Type commands here #

\e[1;31 1.2

\u\h
\w

1.4

$PATH
$LD_LIBRARY_PATH
PATH=/usr/bin;/bin
LD_LIBRARY_PATH=/usr/lib;/lib

shell/usr/bin/bin

binPATHmyapp/opt/myapp
binlib

1.5 shell

11

1.4.1

export PATH=/opt/myapp/bin:$PATH
export LD_LIBRARY_PATH=/opt/myapp/lib;$LD_LIBRARY_PATH

PATHLD_LIBRARY_PATH
PATH=/opt/myapp/bin:/usr/bin:/bin
LD_LIBRARY_PATH=/opt/myapp/lib:/usr/lib;/lib

.bashrc-

prepend() { [ -d "$2" ] && eval $1=\"$2':'\$$1\" && export $1; }

prepend PATH /opt/myapp/bin


prepend LD_LIBRARY_PATH /opt/myapp/lib

1.4.2
prepend()
eval:

prepend() { [ -d "$2" ] && eval $1=\"$2\$\{$1:+':'\$$1\}\" && export $1 ; }

shell
${parameter:+expression}

parameterexpression

1.5

shell

shell

12

1.5.1
Bash shelllet(( ))[]
exprbc

1.5.2
(1)

#!/bin/bash
no1=4;
no2=5;

(2) letlet$

let result=no1+no2
echo $result


$ let no1++


$ let no1--


let no+=6
let no-=6

let no=no+6let no=no-6


[]let
result=$[ no1 + no2 ]

[]$
result=$[ $no1 + 5 ]

(())(())$

1.5 shell

13

result=$(( no1 + 50 ))

expr
result=`expr 3 + 4`
result=$(expr $no1 + 5)

(3) bc

echo "4 * 0.56" | bc


2.24
no=54;
result=`echo "$no * 1.5" | bc`
echo $result
81.0

stdin
bc

scale=22

bc

echo "scale=2;3/8" | bc
0.37

bc

#!/bin/bash

no=100
echo "obase=2;$no" | bc
1100100
no=1100100
echo "obase=10;ibase=2;$no" | bc
100

echo "sqrt(100)" | bc #Square root


echo "10^10" | bc #Square

14

1.6

stdinstdoutstderr

1.6.1
stdin stdout
stderr

012
0 stdin

1 stdout

2 stderr

1.6.2
(1)
$ echo "This is a sample text 1" > temp.txt

temp.txt
echotemp.txt
(2)
$ echo "This is sample text 2" >> temp.txt

(3)
$ cat temp.txt
This is sample text 1
This is sample text 2

(4) stderr

1.6

15

$ ls +
ls: cannot access +: No such file or directory

0
0$?
echo $?

stderrstdout
out.txt
$ ls + > out.txt
ls: cannot access +: No such file or directory

stderrout.txt

$ ls + 2> out.txt #

stderrstdout
$ cmd 2>stderr.txt 1>stdout.txt

stderrstdoutstderrstdout

$ cmd 2>&1 output.txt

$ cmd &> output.txt

(5)

stderrstderr /dev/null
3a1a2a3
a1a
cat

$ echo a1 > a1
$ cp a1 a2 ; cp a2 a3;
$ chmod 000 a1 #

a*
a1

16

$ cat a*
cat: a1: Permission denied
a1
a1

cat: a1: Permission deniedstderrstderr


stdout
$ cat a* 2> err.txt # stderrerr.txt
a1
a1
$ cat err.txt
cat: a1: Permission denied

$ cmd 2>/dev/null

stderrstdout
|
stdin
(6)
stdintee
stdouttee:
command | tee FILE1 FILE2

teestdinstdout
out.txtstdincat -nstdin
stdout:
$ cat a* | tee out.txt | cat -n
cat: a1: Permission denied
1a1
2a1

out.txt
$ cat out.txt
a1
a1

cat: a1: Permission denied


stderrteestdin

1.6

17

tee-a
$ cat a* | tee -a out.txt | cat n

command FILE1 FILE2 command


FILE

(7) stdin-

$ cmd1 | cmd2 | cmd -

$ echo who is this | tee who is this


who is this

/dev/stdinstdin
/dev/stderr/dev/stdout
stdinstderrstdout

1.6.3

>>>

>1>>>>>1>>

stderr/dev/null/dev/null
null

1.6.4
stdincat

$ cat file | cmd


$ cmd1 | cmd

18

1.
stdin
$ cmd < file

2.

shelllog

#!/bin/bash
cat<<EOF>log.txt
LOG FILE HEADER
This is a test log file
Function: System statistics
EOF

cat <<EOF>log.txt EOF stdin log.txt

$ cat log.txt
LOG FILE HEADER
This is a test log file
Function: System statistics

3.
abstract indicator
012stdinstdoutstderr
exec
3


< stdin>

>>

$ exec 3<input.txt #3

1.7

19

$ echo this is a test line > input.txt


$ exec 3<input.txt

3
$ cat<&3
this is a test line

3exec
3

$ exec 4>output.txt #

$ exec 4>output.txt
$ echo newline >&4
$ cat output.txt
newline

$ exec 5>>input.txt

$ exec 5>>input.txt
$ echo appended line >&5
$ cat input.txt
newline
appended line

1.7

shell
Bash

1.7.1
Bash4.0

20

1.7.2
(1)
array_var=(1 2 3 4 5 6)
#0

-
array_var[0]="test1"
array_var[1]="test2"
array_var[2]="test3"
array_var[3]="test4"
array_var[4]="test5"
array_var[5]="test6"

(2)
echo ${array_var[0]}
test1
index=5
echo ${array_var[$index]}
test6

(3)
$ echo ${array_var[*]}
test1 test2 test3 test4 test5 test6

$ echo ${array_var[@]}
test1 test2 test3 test4 test5 test6

(4)
$ echo ${#array_var[*]}
6

1.7.3
Bash 4.0

1.

1.8

21

$ declare -A ass_array

--
$ ass_array=([index1]=val1 [index2]=val2)

$ ass_array[index1]=val1
$ ass_array'index2]=val2

$ declare -A fruits_value
$ fruits_value=([apple]='100dollars' [orange]='150 dollars')

$ echo "Apple costs ${fruits_value[apple]}"


Apple costs 100 dollars

2.

$ echo ${!array_var[*]}

$ echo ${!array_var[@]

fruits_value
$ echo ${!fruits_value[*]}
orange apple

1.8

alias

1.8.1

22

(1)
$ alias new_command='command sequence'

apt-get install
$ alias install='sudo apt-get install'

install pidginsudo apt-get install pidgin


(2) alias
~/.bashrc
shell ~/.bashrc
$ echo 'alias cmd="command seq"' >> ~/.bashrc

(3) ~/.bashrc
unalias alias example= example

(4) rmbackup
alias rm='cp $@ ~/backup && rm $@'

1.8.2

alias

$ \command

\
\

1.9

1.9

23

shell

2
1.9.1
tputstty

1.9.2


tput cols
tput lines


tput longname

(100,100)

tput cup 100 100


tputsetb n

n07


tputsetf n

n07


tput bold

tput smul
tput rmul


tputed

24

stty

#!/bin/sh
#Filename: password.sh
echo -e "Enter password: "
stty -echo
read password
stty echo
echo
echo Password read.

-echoecho

1.10

1.10.1
Unix
UTC197011000
Unix

1.10.2

(1)
$ date
Thu May 20 23:09:04 IST 2010

(2)
$ date +%s
1290047248

UTCCoordinated Universal TimeUTC

UnixUTC 1970110POSIXPOSIX

1.10

25

$ date --date "Thu Nov 18 08:07:21 IST 2010" +%s


1290047841

--date

$ date --date "Jan 20 2001" +%A


Saturday

1-1
(3) + date

$ date "+%d %B %Y"


20 May 2010

(4)

# date -s ""

# date -s "21 June 2009 11:01:22"

(5)
#!/bin/bash
#: time_take.sh
start=$(date +%s)
commands;
statements;
end=$(date +%s)
difference=$(( end - start))
echo Time taken to execute commands is $difference seconds.

time<scriptpath>

26

1.10.3
197011000

1-1
1-1

mm/dd/yy

%aSat
%ASaturday
%bNov
%BNovember
%d31
%D10/18/10
%y10
%Y2010

%I%H08

%M33

%S10

%N695208515

Unix

%s1290049486

1.10.4

sleep$ sleep no_of_seconds.


tputsleep040
#!/bin/bash
#: sleep.sh
echo -n Count:
tput sc
count=0;
while true;

1.11

27

do
if [ $count -lt 40 ];
then
let count++;
sleep 1;
tput rc
tput ed
echo -n $count;
else exit 0;
fi

done

count01echocount
tput sc
counttput rctput ed
count1sleep

1.11

Bash

1.11.1

Bash
(1) xshell
$ bash -x script.sh

-x
sh -x script

(2) set -xset +x


#!/bin/bash
#: debug.sh
for i in {1..6};
do
set -x
echo $i
set +x

28

done
echo "Script executed"

echo $i-x+x

(3) Bash
_DEBUG

#!/bin/bash
function DEBUG()
{
[ "$_DEBUG" == "on" ] && $@ || :
}
for i in {1..10}
do
DEBUG echo $i
done

"on"
$ _DEBUG=on ./script.sh

DEBUG _DEBUG=on
Bash:shell

1.11.2
-xstdout

set builtin

set x
set +x
set v
set +v

1.11.3
shebang

1.12

29

shebang

shebang#!/bin/bash #!/bin/bash -xv

1.12

Bash

1.12.1

(1)
function fname()
{
statements;
}

fname()
{
statements;
}

(2)

$ fname ; #

(3)
fname arg1 arg2 ; #

fnamefname
fname()
{
echo $1, $2; #12
echo "$@";#
echo "$*"; #$@
return 0; #
}

script:$0

30

$1
$2
$nn
"$@" "$1" "$2" "$3"
"$*" "$1c$2c$3"cIFS
"$@" "$*" "$*"

1.12.2
Bash
1.
Bash
F() { echo $1; F hello;
sleep 1; }

Fork
:(){ :|:& };:

&
Fork
http://en.wikipedia.
org/wiki/Fork_bombFork
/etc/security/limits.conf

2.
export

export -f fname

3.

cmd;
echo $?;

1.13

31

$? cmd

00

#!/bin/bash
#: success_test.sh
CMD="command" #command
$CMD
if [ $? -eq 0 ];
then
echo "$CMD executed successfully"
else
echo "$CMD terminated unsuccessfully"
fi

4.

-p-v-k N

$ command -p -v -k 1 file

$ command -pv -k 1 file


$ command -vpk 1 file
$ command file -pvk 1

1.13

shell

1.13.1
stdinstderrstdout
stdinstdout

filterpipe
|
$ cmd1 | cmd2 | cmd3

32

3cmd1cmd2cmd2cmd3
cmd3

1.13.2
shell
(1)
$ ls | cat -n > out.txt

lscat -nstdin
out.txt

(2)
cmd_output=$(COMMANDS)

shell
cmd_output=$(ls | cat -n)
echo $cmd_output

cmd_output=`COMMANDS`

cmd_output=`ls | cat -n`


echo $cmd_output

1.13.3

1. shell
shell()shell
pwd;
(cd /bin; ls);
pwd;

shellshellshell
cdshellshell

1.14 n

33

pwd

cd

2. shell
shell
\n
$ cat text.txt
1
2
3

$ out=$(cat text.txt)
$ echo $out
1 2 3 # Lost \n spacing in 1,2,3
$ out="$(cat tex.txt)"
$ echo$out
1
2
3

1.14

readBashread

read

+ + +
read

read
(1) nvariable_name

read -n number_of_chars variable_name

$ read -n 2 var
$ echo $var

34

(2)
read -s var

(3)
read -p "Enter input:"

var

(4)
read -t timeout var

$ read -t 2 var
#2var

(5)
read -d delim_char var

$ read -d ":" var


hello:#var hello

1.15

shell

1.15.1

repeat()
{
while true
do
$@ && return
done
}

shellrc
repeat() { while true; do $@ && return; done }

1.16

35

1.15.2

repeatwhile$@

1.15.3

1.

true/bin
whileshellshell:
0
repeat() { while :; do $@ && return; done }

2.
repeat()Internet

repeat wget -c http://www.example.com/software-0.1.tar.gz

www.example.com

repeat() { while :; do $@ && return; sleep 30; done }

30

1.16
Internal Field SeparatorIFSshell

delimiterIFS
shell
Comma Separated ValueCSV
IFS="."IFS=","

36

1.16.1
CSV
data="name,sex,rollno,location"

IFS
oldIFS=$IFS
IFS=, now,
for item in $data;
do
echo Item: $item
done
IFS=$oldIFS

Item:
Item:
Item:
Item:

name
sex
rollno
location

IFS

IFSshell$item

IFS

1.16.2
/etc/passwdIFS/etc/passwd

root:x:0:0:root:/root:/bin/bash
shellIFSshell
#!/bin/bash
#: IFS
line="root:x:0:0:root:/root:/bin/bash"
oldIFS=$IFS;
IFS=":"
count=0
for item in $line;
do

1.16

37

[ $count -eq 0 ] && user=$item;


[ $count -eq 6 ] && shell=$item;
let count++
done;
IFS=$oldIFS
echo $user\'s shell is $shell;

root's shell is /bin/bash

Bash

for
for var in list;
do
commands; #$var
done

list

echo {1..50}1~50echo {a..z}{A..Z}{a..h}

concatenate

ia~z
for i in {a..z}; do actions; done;

forCfor
for((i=0;i<10;i++))
{
commands; #$i
}

while
while condition
do
commands;
done

true
until

Bashuntil

38

x=0;
until [ $x -eq 9 ]; #[$x -eq 9 ]
do
let x++; echo $x;
done

1.17

BashUnix
ifif else
test

if
if condition;
then
commands;
fi

else ifelse
if condition;
then
commands;
else if condition; then
commands;
else
commands;
fi

ifelseif

[ condition ] && action; # conditionaction


[ condition ] || action; # conditionaction
&&||Bash

1.17

39

[]

[$var -eq 0 ] or [ $var -eq 0]

[ $var -eq 0 ] # $var 0


[ $var -ne 0 ] # $var 0

-gt

-lt

-ge

-le

[ $var1 -ne 0 -a $var2 -gt 2 ] #-a


[ $var1 -ne 0 -o var2 -gt 2 ] # -o

[ -f $file_var ]

[ -x $var ]

[ -d $var ]

[ -e $var ]

[ -c $var ]

[ -b $var ]

[ -w $var ]

[ -r $var ]

[ -L $var ]

fpath="/etc/passwd"
if [ -e $fpath ]; then
echo File exists;
else
echo Does not exist;
fi

40

[[ $str1 = $str2 ]]str1str2str1str2

[[ $str1 == $str2 ]]

[[ $str1 != $str2 ]]str1str2

[[ $str1 > $str2 ]]str1str2

[[ $str1 < $str2 ]]str1str2

[[ -z $str1 ]]str1

[[ -n $str1 ]]str1

&& ||
if [[ -n $str1 ]] && [[ -z $str2 ]] ;
then
commands;
fi

str1="Not empty "


str2=""
if [[ -n $str1 ]] && [[ -z $str2 ]];
then
echo str1 is nonempty and str2 is empty string.
fi

str1 is nonempty and str2 is empty string.

1.17

41

testtest[]

test

if

[ $var -eq 0 ]; then echo "True"; fi

if

test $var -eq 0 ; then echo "True"; fi

42

cat

xargs

renamemv

tr

2.1

Unix

grepawksedfind
Unix/Linux

2.2

cat

cat
cat

stdin
catcat

2.2 cat

43

2.2.1

catconcatenate

cat
$ cat file1 file2 file3 ...


$ cat file.txt
This is a line inside file.txt
This is the second line inside file.txt


$ cat one.txt two.txt
This is line from one.txt
This is line from two.txt

2.2.2

cat
cat

OUTPUT_FROM_SOME COMMANDS | cat

catstdin

$ echo 'Text through stdin' | cat - file.txt

-stdin

2.2.3

cat

1.

44

$ cat -s file

:
$ cat multi_blanks.txt
line 1

line2
line3

line4
$ cat -s multi_blanks.txt #
line 1
line2
line3
line4

trz2.6
2. ^|
Python

cat
cat -T
^|
$ cat file.py
def function():
var = 5
next = 6
third = 7
$ cat -T file.py
def function():
^Ivar = 5
next = 6
^Ithird = 7^I

2.3

45

3.

cat-ncat
stdout
$ cat lines.txt
line
line
line
$ cat -n lines.txt
1 line
2 line
3 line

-n-b

2.3

scriptscriptreplay

2.3.1

scriptscirptreplayGNU/Linux

2.3.2

8
$ script -t 2> timing.log -a output.session
type commands;

..
exit

46

stderrshellcsh shell

scripttiming.log
output.session-t
stderr2>stderrtiming.log
timing.logoutput.session

$ scriptreplay timing.log output.session


#

2.3.3

KB
timing.logoutput.session

2.4

findUnix/Linuxshell

find

2.4.1
find

find

2.4.2

$ find base_path

bash_path /home/slynux
find

2.4

47

$ find . -print
#

. .. Unix
-print
-print'\n'

-printfind

-print0'\0'

2.4.3

findfind
find
1.
name
*.txt.txt -print -name
find

$ find /home/slynux -name "*.txt" -print

find -iname
-name

$ ls
example.txt EXAMPLE.txt file.txt
$ find . -iname "example*" -print
./example.txt
./EXAMPLE.txt

OR
$ ls
new.txt some.jpg text.pdf
$ find . \( -name "*.txt" -o -name "*.pdf" \) -print
./text.pdf
./new.txt

.txt.pdffind
\(\) -name "*.txt" -o -name "*.pdf"

48

-path-name
-path
$ find /home/users -path "*/slynux/*" -print

/home/users/list/slynux.txt
/home/users/slynux/eg.css

-regex-path-regex

E-mailE-mailname@host.root[a-z0-9]+
@[a-z0-9]+.[a-z0-9]+ +
.py.sh
$ ls
new.PY next.jpg test.py
$ find . -regex ".*\(\.py\|\.sh\)$"
./test.py

-iregex
$ find . -iregex ".*\(\.py\|\.sh\)$"
./test.py
./new.PY

2.
find!
$ find . ! -name "*.txt" -print

find.txt
$ ls
list.txt

new.PY

new.txt

next.jpg

$ find . ! -name "*.txt" -print


.
./next.jpg

test.py

2.4

49

./test.py
./new.PY

3.
find-maxdepth -mindepth

find

findfind
12
-maxzdepthfind
-mindepth
find1
$ find . -maxdepth 1 -name "f*" -print

f
-maxdepth 2
-mindepth -maxdepthfind

$ find . -mindepth 2 -name "f*" -print


./dir1/dir2/file1
./dir3/dir4/f2

dir1dir3
-maxdepth-mindepthfind4
find
-maxdepth-typefind
-type
-type
find

4.
Unix
FIFO

50

-typefind

$ find . -type d -print

find
$ find . -type f -print

$ find . -type l -print

2-1type
2-1

FIFO

5.
Unix/Linux
-atime

-mtime

-ctime

Unix

-atime-mtime-ctimefind

- + - +
7

2.4

51

$ find . -type f -atime -7 -print

7
$ find . -type f -atime 7 -print

$ find . -type f -atime +7 -print

-mtime-ctime

-atime-mtime-ctime

-amin

-mmin

-cmin

7
$ find . -type f -amin +7 -print

find -newer -newer

file.txt

$ find . -type f -newer file.txt -print

find

6.

$ find . -type f -size +2k


# 2KB

$ find . -type f -size -2k


# 2KB
$ find . -type f -size 2k
# 2KB

52

k
b 512

c
w 2

k 1024
M 1024K
G 1024M

7.
-deletefind

.swp
$ find . -type f -name "*.swp" -delete

8.

$ find . -type f -perm 644 -print


# 644

-permfind3.5

Apache WebWebPHP
PHP
$ find . -type f -name "*.php" ! -perm 644 -print

-user USER

USERUID
slynux
$ find . -type f -user slynux -print

9. find
find-exec-execfind

-permPHP
rootWebApache
www-data -userroot-exec

2.4

53

rootfind

# find . -type f -user root -exec chown slynux {} \;

{} -exec
{} findtest1.txttest2.txt
slynuxfind

chown slynux {}

chown slynux test1.txtchown slynux test2.txt

exec
+;
Call_c_files.txt
findC -execcat

$ find . -type f -name "*.c" -exec cat {} \;>all_c_files.txt

-exec{}{}

> findall_c_files.txt>>
findstdin
>>

10 .txtOLD
$ find . -type f -mtime +10 -name "*.txt" -exec cp {} OLD

\;

find
-exec

-exec
shellcommand.sh
-exec
-exec ./commands.sh {} \;

54

-execprintf
$ find . -type f -name "*.txt" -exec printf "Text file: %s\n" {} \;

10. find

Git
.git
.git

$ find devel/source_path

\( -name ".git" -prune \) -o \( -type f -print \)

# Instead of \( -type -print \),

.git
\( -name ".git" -prune \) .git

\( -type f -print \)

2.5

xargs

stdoutstdin

cat foo.txt | grep "test"

stdin

xargs
xargsstdinxargs

Bash

xargs

2.5.1
xargsstdin

2.5 xargs

55

command | xargs

2.5.2
xargsstdin
xargsfind -execxargs

" "'\n'
xargs

$ cat example.txt #
1 2 3 4 5 6
7 8 9 10
11 12
$ cat example.txt | xargs
1 2 3 4 5 6 7 8 9 10 11 12

nstdinn
" "

$ cat example.txt | xargs -n 3


1 2 3
4 5 6
7 8 9
10 11 12

2.5.3
xargs

-d
$ echo "splitXsplitXsplitXsplit" | xargs -d X
split split split split

56

stdinX -dX
Xxargs

-n
$ echo "splitXsplitXsplitXsplit" | xargs -d X -n 2
split split
split split

2.5.4
stdin

1. stdin
echoxargs
#!/bin/bash
#: cecho.sh
echo $*'#'

cecho.sh #
$ ./cecho.sh arg1 arg2
arg1 arg2 #

cecho.sh
./cecho.sh arg1
./cecho.sh arg2
./cecho.sh arg3

./cecho.sh arg1 arg2


./cecho.sh arg3


./cecho.sh arg1 arg2 arg3

2.5 xargs

57

xargsargs.txt

$ cat args.txt
arg1
arg2
arg3

$ cat args.txt | xargs -n 1 ./cecho.sh


arg1 #
arg2 #
arg3 #

X
INPUT | xargs -n X

$ cat args.txt | xargs -n 2 ./cecho.sh


arg1 arg2 #
arg3 #

$ cat args.txt | xargs ./cecho.sh


arg1 arg2 arg3 #

cecho.sh
args.txt

./cecho.sh -p arg1 -l

arg1
args.txt
./cecho.sh -p arg1 -l
./cecho.sh -p arg2 -l
./cecho.sh -p arg3 -l

xargs-I-I

xargs-Ixargs

58

$ cat args.txt | xargs -I {} ./cecho.sh -p {} -l


-p arg1 -l #
-p arg2 -l #
-p arg3 -l #

-I {} {}stdin

-I3
{}3{}
2. findxargs
xargsfind

$ find . -type f -name "*.txt"

-print | xargs rm -f

find
'\n'' '' 'xargs
hell text.txtxargshelltext.txt
findxargs -print0findnull
'\0'
find .txtxargs
$ find . -type f -name "*.txt" -print0 | xargs -0 rm -f

.txtxargs -0\0
3. C
CLines of CodeLOC

$ find source_code_dir_path -type f -name "*.c" -print0 | xargs -0 wc -l

SLOCCount
GNU/Linux
http://www.dwheeler.com/sloccount/

2.6 tr

59

4. stdinwhileshell

xargs

whileshell

$ cat files.txt | ( while read arg; do cat $arg; done )


# cat files.txt | xargs -I {} cat {}

whilecat $arg

shell

$ cmd0 | ( cmd1;cmd2;cmd3) | cmd4

cmd1cd /shellshellcmd4

2.6

tr

trUnix
tr
translate
tr

2.6.1

trstdin

tr [options] set1 set2

stdinset1set2stdoutset1
set2set2
set1set2set1set2set1

8
2.6.2

$ echo "HELLO WHO IS THIS" | tr 'A-Z' 'a-z'

60

'A-Z' 'a-z'
'ABD-}''aA.,''a-ce-x''a-c0-9'

3'\t'
'\n'ASCII

2.6.3
tr
tr
$ echo 12345 | tr '0-9' '9876543210'
87654 #
$ echo 87654 | tr '9876543210' '0-9'
12345 #

ROT13ROT13ROT13
13trROT13
$ echo "tr came, tr saw, tr conquered." | tr 'a-zA-Z' 'n-za-mN-ZA-M'

ge pnzr, ge fnj, ge pbadhrerq.

ROT13
$ echo ge pnzr, ge fnj, ge pbadhrerq. | tr 'a-zA-Z' 'n-za-mN-ZA-M'

tr came, tr saw, tr conquered.

tr
$ tr '\t' ' ' < file.txt

2.6.4
trtr

2.6 tr

61

1. tr

tr-dstdin

$ cat file.txt | tr -d
#set1set2

'[set1]'

$ echo "Hello 123 world 456" | tr -d '0-9'


Hello world
# stdin

2.
-cset1set2

tr -c [set1] [set2]

set1set1

$ echo hello 1 char 2 next 4 | tr -d -c '0-9 \n'


1 2 4

-d

3. tr
tr

tr-s
$ echo "GNU is
not
UNIX. Recursive
GNU is not UNIX. Recursive right ?
# tr -s '[set]'

right ?" | tr -s ' '

tr

$ cat sum.txt
1
2
3
4

62

5
$ cat sum.txt | echo $[ $(tr '\n' '+' ) 0 ]
15

tr'\n''+'"1+2+3+5+"
+0
$[ operation ]
echo $[ 1+2+3+4+5+0 ]

tr
$ cat multi_blanks.txt | tr -s '\n'
line1
line2
line3
line4

tr'\n''\n'
4.
tr
alnum
alpha
cntrl
digit
graph
lower
print
punct
space
upper
xdigit

2.7

63

tr [:class:] [:class:]

tr '[:lower:]' '[:upper:]'

2.7

checksum

Internet
CD

6
2.7.1
md5sumSHA-1

2.7.2
md5sum
$ md5sum filename
68b329da9893e34099c7d8ad5cb9c940 filename

md5sum32
MD5
$ md5sum filename > file_sum.md5

64

2.7.3
md5sum
$ md5sum file1 file2 file3 ..

[checksum1]
[checksum1]
[checksum1]

file1
file2
file3

$ md5sum -c file_sum.md5
#

.md5
$ md5sum -c *.md5

md5sumSHA-1
40SAH-1sha1summd5sum
md5sum sha1sum
file_sum.md5file_sum.sha1
ISO
2-1

2-1

2.8

65

2-2md5sum

2-2

2.7.4

md5deepsha1deepmd5deep

$ md5deep -rl directory_path > directory.md5


# -r
# -lmd5deep

find

$ find directory_path -type f -print0 | xargs -0 md5sum >> directory.md5

$ md5sum -c directory.md5

2.8

Linux

cryptgpgbase64md5sumsha1sumopenssl

66

cryptstdin

Stdout

$ crypt <input_file >output_file


Enter passphrase:

$ crypt PASSPHRASE <input_file >encrypted_file

$ crypt PASSPHRASE -d <encrypted_file >output_file

gpgGNU

gpg

gpg
$ gpg -c filename

filename.gpggpg
$ gpg filename.gpg

gpg
http://en.wikipedia.org/wiki/GNU_Privacy_Guard
Base64ASCII64radix-64

representationASCIIbase64/
Base64Base64
$ base64 filename > outputfile

$ cat file | base64 > outputfile

base64stdin

Base64

2.8

67

$ base64 -d file > outputfile

$ cat base64_file | base64 -d > outputfile

md5sumsha1sum

$ md5sum file
8503063d5488c3080d4800ff50850dc9

file

$ sha1sum file
1ba02b66e2e557fede8f61b7df282cd0a27b816b

file

md5sumSHA-1
bcryptsha512sum
http://codahale.com/how-to-saftylystore-a-password/

shadow-likesalt

shadow-like saltLinux
/etc/shadow

test:$6$fG4eWdUi$ohTKOlEUzNk77.4S8MrYe07NTRV4M3LrJnZP9p.qc1bR5c.EcOruzPXfEu1uloB
FUa18ENRH7F70zhodas3cR.:14790:0:99999:7:::

$6$fG4eWdUi$ohTKOlEUzNk77.4S8MrYe07NTRV4M3LrJnZP9p.qc1bR5c.
EcOruzPXfEu1uloBFUa18ENRH7F70zhodas3cRshadow

shellshadowshadow
openssl shadow
shadowsaltSALT
salt
salt

68

salt http://en.
wikipedia.org/wiki/Salt_(cryptography)

$ opensslpasswd -1 -salt SALT_STRING PASSWORD


$1$SALT_STRING$323VkWkSLHuhbt1zkSsUG.

SALT_STRINGPASSWORD

2.9

sortstdin
uniqsort
stdinsortuniq

2.9.1
sortstdinstdoutuniq

sort

2.9.2
(1) file1.txtfile2.txt
$ sort file1.txt file2.txt > sorted.txt

$ sort file1.txt file2.txt -o sorted.txt

(2)
$ sort -n file.txt

(3)
$ sort -r file.txt

(4)

$ sort -M months.txt

(5)
$ sort -m sorted1 sorted2

2.9

69

(6)

$ sort file1.txt file2.txt | uniq

(7)
#!/bin/bash
#
sort -C filename ;
if [ $? -eq 0 ]; then
echo Sorted;
else
echo Unsorted;
fi

filename

2.9.3

sortuniqsort

sortuniq

sort0
$?0

2.9.4

sortsort
1.

$ cat data.txt
1 mac
2000
2 winxp
4000
3 bsd
1000
4 linux
1000

-kkey

-rsort

70

# 1
$ sort -nrk 1 data.txt
4 linux
1000
3 bsd
1000
2 winxp
4000
1 mac
2000
# -nr
# 2
$ sort -k 2 data.txt
3 bsd
1000
4 linux
1000
1 mac
2000
2 winxp
4000

-nsort

-n

key1=character4-character8

$ cat data.txt
1010hellothis
2189ababbba
7464dfddfdfd
$ sort -nk 2,3 data.txt

23

$ sort -nk 1,1 data.txt

sort\0xargs
$ sort -z data.txt | xargs -0
# \0xargs

$ sort -bd unsorted.txt

-b-d

2.9

71

2. uniq

uniqstdin

uniquniq

sort

$ cat sorted.txt
bash
foss
hack
hack

$ uniq sorted.txt
bash
foss
hack

5
$ sort unsorted.txt | uniq

$ uniq -u sorted.txt
bash
foss

$ sort unsorted.txt | uniq -u

$ sort unsorted.txt | uniq -c


1 bash
1 foss
2 hack

$ sort unsorted.txt
hack

| uniq -d

-s-w

72

-s n
-w

uniq
$ cat data.txt
u:01:gnu
d:04:linux
u:01:bash
u:01:hack

2-s 2 -w-w
2
$ sort data.txt | uniq -s 2 -w 2
d:04:linux
u:01:bash

xargs0zero-byte
uniqxargs0
xargsstdin
this is a linexargs4
0\0

uniq0
$ uniq -z file.txt

files.txt
$ uniq -z file.txt | xargs -0 rm

uniqstdout

2.10

shell /tmp

2.10.1

2.11

73

(1)

$ filename=`mktemp`
$ echo $filename
/tmp/tmp.8xvhkjF5fH

$filename

(2)
$ dirname=`mktemp -d`
$ echo $dirname
tmp.NI8xzW7VRX

$dirname
(3)
$ tmpfile=`mktemp -u`
$ echo $tmpfile
/tmp/tmp.RsGmilRpcT

$tmpfile
(4)

$mktemp test.XXX
test.2tc

2.10.2

mktemp

Xmktemp
3X

2.11

E-mail

74

2.11.1
data.file100KB
10KB
$ split -b 10k data.file
$ ls
data.file xaa xab xac

xad

xae

xaf

xag

xah

xai

xaj

data.file10KBxabxacxad
-d
-a length
$ split -b 10k data.file -d -a 4

kKBMMBGGBcbytewword
$ ls
data.file x0009

x0019

x0029

x0039

x0049

x0059

x0069

x0079

2.11.2
split

x
splitPREFIX
$ split [COMMAND_ARGS] PREFIX

$ split -b 10k data.file -d -a 4 split_file


$ ls
data.file
split_file0002 split_file0005
split_file0000 split_file0003 split_file0006
split_file0001 split_file0004 split_file0007

split_file0008
split_file0009

strtok.c

-l no_of_lines
$ split -l 10 data.file
# 10

csplit

2.11

75

csplitsplitsplitcsplit

$ cat server.log
SERVER-1
[connection] 192.168.0.1
[connection] 192.168.0.2
[disconnect] 192.168.0.3
[connection] 192.168.0.4
SERVER-2
[connection] 192.168.0.1
[connection] 192.168.0.2
[disconnect] 192.168.0.3
[connection] 192.168.0.4
SERVER-3
[connection] 192.168.0.1
[connection] 192.168.0.2
[disconnect] 192.168.0.3
[connection] 192.168.0.4

2
success
failed
pending
success

failed
failed
success
failed
pending
pending
pending
failed

server1.logserver2.logserver3.log
SERVER
$ csplit server.log /SERVER/ -n 2 -s {*}
$ ls
server01.log

server02.log

server03.log

-f server -b "%02d.log"

; rm server00.log

server.log

/SERVER/
/[REGEX]/ SERVER

{*} {}

-s
-n 010203
-f server

-b %02d.logCprintf=
+=server + %02d.log

server00.log

76

2.12

shell

2.12.1
%.
sample.jpg
file_jpg="sample.jpg"
name=${file_jpg%.*}
echo File name is: $name

File name is: sample

#
.jpgfile_jpg
extension=${file_jpg#*.}
echo Extension is: jpg

Extension is: jpg

2.12.2
.%
${VAR%.*}
$VAR % .*

VARVAR=sample.jpg.jpg $VAR

sample
%non-greedy
%%%

2.12

77

VAR=hack.fun.book.txt

%
$ echo ${VAR%.*}

hack.fun.book
%.*.txt
%%

$ echo ${VAR%%.*}

hack
%%.*.fun.book.txt

# %

${VAR#*.}

$VAR#*.

%% # ##
##

VAR=hack.fun.book.txt

$ echo ${VAR#*.}

fun.book.txt
#*.hack

##
$ echo ${VAR##*.}

txt

78

##*.txt
'.'###
##
URL="www.google.com"
$ echo ${URL%.*} # .*
www.google
$ echo ${URL%%.*} # *.
www

$ echo ${URL#*.} # *.
google.com

$ echo ${URL##*.} # *.
com

2.13

Bash

2.13.1
renamePerlfindrenamemv

2.13.2

2.13

79

#!/bin/bash
#: rename.sh
#: .jpg .png
count=1;
for img in `find . -iname '*.png' -o -iname '*.jpg' -type f -maxdepth 1`
do
new=image-$count.${img##*.}

echo "Renaming $img to $new"


mv "$img" "$new"
let count++
done

$ ./rename.sh
Renaming hack.jpg to image-1.jpg
Renaming new.jpg to image-2.jpg
Renaming next.png to image-3.png

.jpg .png image-1.jpg


image-2.jpgimage-3.jpgimage-4.png

2.13.3
for.jpg.png
find-o-iname
-maxdepth 1$img
count=1mv
${img##*.}
2.12 ${img##*.}

let count++

*.JPG *.jpg

$ rename *.JPG *.jpg

$ rename 's/ /_/g' *

80

's/ /_/g' * *.txt


$ rename 'y/A-Z/a-z/' *
$ rename 'y/a-z/A-Z/' *

.mp3
$ find path -type f -name "*.mp3" -exec mv {} target_dir \;

$ find path -type f -exec rename 's/ /_/g' {} \;

2.14
Linux
aspell

2.14.1
/usr/share/dict/

$ ls /usr/share/dict/
american-english british-english

#!/bin/bash
#: checkword.sh
word=$1
grep "^$1$" /usr/share/dict/british-english -q
if [ $? -eq 0 ]; then
echo $word is a dictionary word;
else
echo $word is not a dictionary word;
fi

$ ./checkword.sh ful
ful is not a dictionary word

2.14

81

$ ./checkword.sh fool
fool is a dictionary word

2.14.2
grep^ $

-q

aspell
#!/bin/bash
#: aspellcheck.sh
word=$1

output=`echo \"$word\" | aspell list`


if [ -z $output ]; then
echo $word is a dictionary word;
else
echo $word is not a dictionary word;
fi

aspell list
-z $output

$ look word filepath

$ grep "^word" filepath

look/usr/share/dict/words

$look word
# look

$ look android
android
android's
androids

82

2.15

$ command
Enter a number: 1
Enter name : hello
You have entered 1,hello

2.15.1

2.15.2

1[Return]hello[Return]

1ReturnhelloReturn

"1\nhello\n"

\n \nstdin

2.15.3

#!/bin/bash
#: interactive.sh
read -p "Enter number:" no ;
read -p "Enter name:" name
echo You have entered $no, $name;

2.15

83

$ echo -e "1\nhello\n" | ./interactive.sh


You have entered 1, hello

echo -e-eecho

$ echo -e "1\nhello\n"
$ cat input.data
1
hello

> input.data

echo

$ ./interactive.sh < input.data

shellcode\xeb\x1a\x5e\x31\xc0\x88\x46

echo -e \xeb\x1a\x5e\x31\xc0\x88\x46"

shellcode
stdin

expectexpect

7
2.15.4
expectexpect

expect
Linuxexpect

84

expect
#!/usr/bin/expect
#: automate_expect.sh
spawn ./interactive.sh
expect "Enter number:"
send "1\n"
expect "Enter name:"
send "hello\n"
expect eof

$ ./automate_expect.sh

spawn
expect
send
expect eof

2.16

2.16.1
md5sumCPU
md5sum
#/bin/bash
#: generate_checksums.sh
PIDARRAY=()
for file in File1.iso File2.iso
do
md5sum $file &

2.16

85

PIDARRAY+=("$!")
done
wait ${PIDARRAY[@]}

$ ./generate_checksums.sh
330dcb53f253acdf76431cecca0fefe7
bd1694a6fe6df12c3b8141dcffaf06e6

File1.iso
File2.iso

md5sum File1.iso File2.iso

md5sum
time

2.16.2

Bash&shell
md5sum$!
PIDBash$!PIDPID
wait

86

ISOISO

headtail10

10

pushdpopd

3.1

Unix

3.2

3.2

87

mount

dddd
stdinstdout
dd
$ dd if=/dev/zero of=junk.data bs=1M count=1
1+0 records in
1+0 records out
1048576 bytes (1.0 MB) copied, 0.00767266 s, 137 MB/s

1MBjunk.dataifinput file
ofoutput file
bsblock sizecount

dd
dd
of=

bs1MBcount11MB
bs2MBcount24MB
3-1

3-1

1B

2B

512B

1024B

1024KB

1024MB

3-1
/dev/zero0\0

88

ifddstdin
ofddstdout
dd
1048576 bytes (1.0 MB) copied, 0.00767266 s, 137 MB/s

3.3

intersectionset difference

3.3.1
comm

difference


A

3.3.2
comm
$ cat A.txt
apple
orange
gold
silver
steel
iron
$ cat B.txt
orange
gold
cookies
carrot

ABA(1,2,3)B(3,4,5)
3
1,2,4,5
A
1,2

3.3

89

$ sort A.txt -o A.txt ; sort B.txt -o B.txt

(1) comm
$ comm A.txt B.txt
apple
carrot
cookies
gold
iron
orange
silver
steel

A.txtB.txt
A.txtB.txt\t
(2)

$ comm A.txt B.txt -1 -2


gold
orange

(3)
$ comm A.txt B.txt
apple
carrot
cookies
iron
silver
steel

-3

apple
carrot
cookies
iron
silver
steel

(4)
$ comm A.txt B.txt
apple

-3 | sed 's/^\t//'

90

carrot
cookies
iron
silver
steel

(5) A.txtB.txt
A.txt
$ comm A.txt B.txt -2 -3

-2 -3
B.txt
$ comm A.txt B.txt -1 -3

-1 -3

3.3.3
comm
-1
-2
-3

sedcomm \tseds
substitute/^\t/ \t^///
\t\t
A.txtB.txtA.txtB.txtcomm
A.txtB.txtB.txtA.txt

3.4

shell

3.4.1

3.4

91

3.4.2
(1)

$ echo "hello" > test ; cp test test_copy1 ; cp test test_copy2;


$ echo "next" > other;
# test_copy1test_copy2test

(2)
# !/bin/bash
# : remove_duplicates.sh
# :
ls -lS --time-style=long-iso | awk 'BEGIN {
getline; getline;
name1=$8; size=$5
}
{
name2=$8;
if (size==$5)
{
"md5sum "name1 | getline; csum1=$1;
"md5sum "name2 | getline; csum2=$1;
if ( csum1==csum2 )
{
print name1; print name2
}
};

size=$5; name1=name2;
}' | sort -u > duplicate_files

7
cat duplicate_files | xargs -I {} md5sum {} | sort | uniq -w 32 | awk '{ print
"^"$2"$" }' | sort -u > duplicate_sample
echo Removing..
comm duplicate_files duplicate_sample -2 -3 | tee /dev/stderr | xargs rm
echo Removed duplicates files successfully.

(3)
$ ./remove_duplicates.sh

92

3.4.3
shell

ls -lSawk
ls -lS

awkBEGIN{}{}
END{}ls -lS
total 16
-rw-r--r--rw-r--r--rw-r--r--rw-r--r--

1
1
1
1

slynux
slynux
slynux
slynux

slynux
slynux
slynux
slynux

5
6
6
6

2010-06-29
2010-06-29
2010-06-29
2010-06-29

11:50
11:50
11:50
11:50

other
test
test_copy1
test_copy2

1getline1

getline
85
awk{}
size

md5sum

awk
"cmd"| getline

$0$1,$2,$n
md5sumcsum1 csum2name1name2

md5sum-w 32md5summd5sum

3.5

93

32md5sum32
duplicate_sample

duplicate_filesduplicate_sample
comm3.3
commduplicate_filesduplicate_sample

sort -u
teermprint
teestdinstdoutstderr

/dev/stderrstderrstderr
stdin

3.5

Unix/LinuxextUnix/Linux

Linux

usergroup
others

ls -l
-rw-r--r-- 1 slynux slynux
drwxr-xr-x 2 slynux slynux
-rw-r--r-- 1 slynux slynux

2497
4096
539

2010-02-28 11:22 bot.py


2010-05-27 14:31 a.py
2010-02-10 09:11 cl.pl

1
-
d

c
b
l
s

94

3--- --- ---3---

99

'-'

rwx------

rw
x
setuidSx
setuid

setuid-rwS------

r
w
x

---rwx---
rwx

setuidsetgidS

----rwS---
------rwx

Ssetuidsetgid
sticky bit

xtT
tT

------rwt , ------rwT

3.5

95

/tmp

ls -lslynux slynux
slynuxslynux

3.5.1

chmod
rwx rw- r--
chmod

$ chmod u=rwx g=rw o=r filename

g
o

+ -

rwx rw- r--


$ chmod o+x filename

$ chmod a+x filename

aall
-

$ chmod a-x filename

r-- = 4
-w- = 2
--x = 1

96

rw- = 4 + 2 = 6
r-x = 4 + 1 = 5

rwx rw- r--


rwx = 4 + 2 + 1 = 7
rw- = 4 + 2 = 6
r-- = 4

rwx rw- r-- 764


$ chmod 764 filename

3.5.2

1.
chown
$ chown user.group filename

$ chown slynux.slynux test.sh

slynux
2.

chmod +t
$ chmod a+t directory_name

3.

$ chmod 777 . -R

-R

3.6

97

$ chmod 777 "$(pwd)" R.

4.
chown R

$ chown user.group . -R

5.

./executable_namesetuid

$ chmod +s executable_file
# chown root.root executable_file
# chmod +s executable_file
$ ./executable_file

root
setuidLinux ELF

3.6

Linuxext2ext3ext4
immutable
/etc/mtab
/dev/sda5
ext3

/etc/resolv.conf
DNSDNSpacktpub.comIP
ISPDNSDNS
/etc/resolv.confISP/etc/resolv.conf
/etc/resolv.conf

98

3.6.1
chattrchattr

3.6.2
(1)
# chattr +i file

(2) file:
rm file
rm: cannot remove 'file': Operation not permitted

(3)
chattr -i file

3.7

3.7.1
touch

3.7.2
(1) filename
$ touch filename

(2)
for name in {1..100}.txt
do
touch $name
done

3.8

99

{1..100}1, 2, 3, 4, 5, 6, 7100
{1..100}.txt test{1..200}.c
test{a..z}.txt
touch

touch -a
touch -m

(3)

$ touch -d "Fri Jun 25 20:50:14 IST 1999" filename

-d

Jan 20 2010

3.8

Unix

Mac OSWindows

3.8.1

(1)
$ ln -s target symbolic_link_name

$ ln -l -s /var/www/ ~/web

homeWeb
/var/www

(2)
$ ls -l web
lrwxrwxrwx 1 slynux slynux 8 2010-06-25 21:34 web -> /var/www

100

web -> /var/wwwweb /var/www

(3)
$ ls -l | grep "^l"

(4) find
$ find . -type l -print

(5) readlink
$ readlink web
/var/www

3.8.2
grepls -l^l
^
lrwxrwxrwxl
findtypelfind-print
stdout

3.9

3.9.1
findUnix/Linux

Windows
find

3.9.2
(1)
$ file filename

3.9

$ file /etc/passwd
/etc/passwd: ASCII text

101

(2)
$ file -b filename
ASCII text

(3)
# !/bin/bash
# : filestat.sh
if [ $# -ne 1 ];
then
echo "Usage is $0 basepath";
exit
fi
path=$1

declare -A statarray;
while read line;
do
ftype=`file -b "$line" | cut -d, -f1`
let statarray["$ftype"]++;

done < <(find $path -type f -print)


echo ============ File types and counts =============
for ftype in "${!statarray[@]}";
do
echo $ftype : ${statarray["$ftype"]}
done

(4)

$ ./filestat.sh /home/slynux/temp

$ ./filetype.sh /home/slynux/programs
============ File types and counts =============
Vim swap file : 1
ELF 32-bit LSB executable : 6
ASCII text : 2
ASCII C program text : 10

102

3.9.3
statarray
letfind
ftype='file -b "$line"'file
-bfile

$ file a.out -b
ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses
shared libs), for GNU/Linux 2.6.15, not stripped

ELF 32-bit LSB executablecut -d,


-f1
done<<(find $path type f print);
while read line;
do something
done < filename

filenamefind
<(find $path -type f -print)

<<<
shell<<
Bash 3.x<<<
loopdone
done <<< "`find $path type f print`"

${!statarray[@]}

3.10

loopbackLinux
/dev/device_name
mount point

3.10

103

3.10.1
1GBext4
(1) 1GB
$ dd if=/dev/zero of=loobackfile.img bs=1G count=1
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 37.3155 s, 28.8 MB/s

1GB

(2) mkfs1GBext4

$ mkfs.ext4 loopbackfile.img

(3)
$ file loobackfile.img
loobackfile.img: Linux rev 1.0 ext4 filesystem data,
UUID=c9d56c42-f8e6-4cbd-aeab-369d5056660a (extents) (large files) (huge files)

(4)
# mkdir /mnt/loopback
# mount -o loop loopbackfile.img /mnt/loopback

-o loop

/dev/loop1loop2

(5)
# losetup /dev/loop1 loopbackfile.img
# mount /dev/loop1 /mnt/loopback

(6) umount

# umount mount_point

# umount /mnt/loopback

104

(7) umount
# umount /dev/loop1

mountumountroot

3.10.2
dddd
raw dataifof
dd1GB11GB
/dev/zero0
mkfts. ext4ext4

mount/mnt/loopback

mountmkdir
-o loop
mount/dev
losetupmount

3.10.3

1.
mount
o loop0

# losetup /dev/loop1 loopback.img


# fdisk /dev/loop1

fdiskLinuxhttp://www.tldp.org/HOWTO/Partition/
fdisk_partitoning.html fdisk
/dev/hdb/dev/loop1

3.10

105

loopback.img

# losetup -o 32256 /dev/loop2 loopback.img

/dev/loop2-o32 256DOS
32 256
2.


losetupkpartx

# kpartx -v -a diskimage.img
add map loop0p1 (252:0): 0 114688 linear /dev/loop0 8192
add map loop0p2 (252:1): 0 15628288 linear /dev/loop0 122880

/dev/mapper

# mount /dev/mapper/loop0p1 /mnt/disk1

umount

# kpartx -d diskimage.img
loop deleted : /dev/loop0

3. ISO

ISO
ISO

# mkdir /mnt/iso
# mount -o loop linux.iso /mnt/iso

/mnt/isoISO
4. sync

losetup -o 32256 (512*63=32256)512


MBRMaster Boot Record
62

106

sync
$ sync

3.11

ISO ISO

ISOCD-ROMDVD-ROMISO
ISO
ISO

ISO
ISO
ISO
ISO

3.11.1
Unix

cat

3.11.2
/dev/cdromISO
# cat /dev/cdrom > image.iso

ISOdd
# dd if=/dev/cdrom of=image.iso

mkisofsISOcdrecordmkisofs
CD-ROMDVD-ROM
mkisofsISO
$ mkisofs -V "Label" -o image.iso source_dir/

3.11 ISO ISO

107

-oISOsource_dirISO -V
ISO

3.11.3
ISO

1. ISO
ISOUSB
ISOISO

isohybridISOISOisohybrid
Linuxhttp://syslinux.zytor.comsyslinux

# isohybrid image.iso

image.isoISOUSB
ISOUSB

# dd if=image.iso of=/dev/sdb1

/dev/sdb1cat
# cat image.iso >> /dev/sdb1

2. ISO

cdrecordISOCD-ROMDVD-ROMCD-ROM
# cdrecord -v dev=/dev/cdrom image.iso

-speed
-speed SPEED

# cdrecord -v dev=/dev/cdrom image.iso -speed 8

88x
CD-ROMmultisession

108

-multi
# cdrecord -v dev=/dev/cdrom image.iso -multi

3. CD-ROM

$ eject

$ eject -t

3.12

patch filepatch

3.12.1
diff

(1)
1version1.txt
this is the original text
line2
line3
line4
happy hacking !

3.12

109

2version2.txt
this is the original text
line2
line4
happy hacking !
GNU is not UNIX

(2) nonunifieddiff -u
$ diff version1.txt version2.txt
3d2
<line3
6c5
> GNU is not UNIX

(3) diff
$ diff -u version1.txt version2.txt
--- version1.txt 2010-06-27 10:26:54.384884455 +0530
+++ version2.txt 2010-06-27 10:27:28.782140889 +0530
@@ -1,5 +1,5 @@
this is the original text
line2
-line3
line4
happy hacking !
+GNU is not UNIX

-u

diff + -
(4) diff

$ diff -u version1.txt version2.txt > version.patch

patchversion1.txt
version2.txtversion2.txtversion1.txt
(5)

$ patch -p1 version1.txt < version.patch


patching file version1.txt

version1.txtverson2.txt

110

(6)
$ patch -p1 version1.txt < version.patch
patching file version1.txt
Reversed (or previously applied) patch detected!
#

Assume -R? [n] y

patch -Ry/n

3.12.2
diff

diff

$ diff -Naur directory1 directory2

-N
-a
-u
-r

3.13

head tail 10 10

cat
1010
n
nnn
mn
headtail

head

3.13 head tail 10 10

111

(1) 10

$ head file

(2) stdin
$ cat text | head

(3)
$ head -n 4 file

(4) M
$ head -n -M file

-M

5
$ seq 11 | head -n -5
1
2
3
4
5
6

15
$ seq 100 | head -n 5

(5) head
10

$ tail file

(6) stdin
$ cat text | tail

(7) 5
$ tail -n 5 file

(8) M

112

$ tail -n +(M+1)

5M+1=6
$ seq 100 | tail -n +6

6100
tail

tail
tail10
tail -f
--followtail
$ tail -f growing_file

# tail -f /var/log/messages

$ dmesg | tail -f

dmeg USBsdX
XSCSIsdtail -f -s

tailtail

Footail -f
Foo
$ PID=$(pidof Foo)
$ tail -f file --pid $PID

Footail

(1) file.txtgedit
(2) gedit
(3)
$ PID=$(pidof gedit)
$ tail -f file.txt --pid $PID

3.14

113

tailgedittail

3.14

3.14.1

dirdirls

3.14.2

(1) ls d
$ ls -d */

(2) grepls F
$ ls -F | grep "/$"

(3) grepls l

$ ls -l | grep "^d"

(4) find
$ find . -type d -maxdepth 1 -print

3.14.3
-F@*|
/ grep /$

ls d "d"grep

"d"^
findtypemaxdepth1

114

3.15

pushd popd

shell
GUI/var/www
/home/slynux /usr/src
pushd popd
Command-Line InterfaceCLI

3.15.1
pushdpopd
pushdpopd
Last In First Out, LIFO
pushpop

3.15.2
pushdpopdcd
(1)
~ $ pushd /var/www

/var/www ~ /var/www
(2)
/var/www $ pushd /usr/src

/usr/src /var/www ~ /usr/src


(3)
$ dirs
/usr/src /var/www ~ /usr/share /etc
0
1
2 3

(4) 0n

$ pushd +3

/use/share

3.16

115

pushdpopd

(5)
$ popd

/usr/src /var/www ~ /usr/share /etc /usr/sharepopd


/var/www ~ /usr/share /etc/var/www

(6) popd +num


num0n

3.15.3

3pushdpopd
cd -
/var/www

/var/www $ cd /usr/src
/usr/src $ #

/var/www

/usr/src $ cd -

/usr/src
/var/www $ cd -

3.16

wc

wcWord Countwc

116

wc
(1)
$ wc -l file

(2) stdin
$ cat file | wc -l

(3)
$ wc -w file
$ cat file | wc -w

(4)
$ wc -c file
$ cat file | wc -c

echo -n 1234 | wc -c
4

-necho

(5) wc
$ wc file
1435
15763

112200

(6) -L
$ wc file -L
205

3.17

3.17

117

3.17.1

treeLinux

2
3.17.2
Unix
$ tree ~/unixfs
unixfs/
|-- bin
|
|-- cat
|
`-- ls
|-- etc
|
`-- passwd
|-- home
|
|-- pactpub
|
|
|-- automate.sh
|
|
`-- schedule
|
`-- slynux
|-- opt
|-- tmp
`-- usr
8 directories, 5 files

tree

$ tree path -P PATTERN #

$ tree PATH -P "*.sh" # PATH


|-- home
|
|-- packtpub
|
|
`-- automate.sh

$ tree path -I PATTERN

-h
$ tree -h

118

3.17.3
tree
HTML
treeHTMLHTML

$ tree PATH -H http://localhost -o out.html

http://localhostURLPATH
. PATH
Web3-1

3-1

4.2

119

grep

cut

sed

URL

awk

JavaScript

4.1

shellUnix/Linuxshell
sedawkgrepcut

strip

4.2

120

[a-z0-9_]+
@[a-z0-9]+\.[a-z]+

4.2.1


( ?[a-zA-Z]+ ?)

?[a-zA-Z]+a~zA~Z
IP
[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}

[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}

IP192.168.0.24
0~255
[0-9][:digit:]0~9{1,3}13\."."

IP
123.300.1.1IP
IPIP

4.2.2
4-1

4.2

121

4-1

^tux tux

tux$ tux

[]

[]

coo[kl] cookcool

[^]

[^]

9[^01] 92939190

[-]

[]

[1-5] 15

10

colou?r colorcolourcolouur

Rollno-9+ Rollno-99Rollno-9Rollno-

co*l clcolcoool

()

ma(tri)?x maxmaxtrix

{n}

[0-9]{3} [0-9]{3}
[0-9][0-9][0-9]

{n,}

[0-9]{2,}

{n,m}

[0-9]{2,5}

Oct (1st | 2nd) Oct 1stOct 2nd

a\.ba.bajb . \
.

Hack.HacklHackiHackl2Hackil

http://www.linuxforu.com/2011/04/sed-explained-part-1/

4.2.3

1.

$ ^ . * + { }

a.txt

atxt'.'
'.'\

a\.txt

122

2.

http://www.regexper.com
4-1

4.3

4-1

grep

4.3.1
grepUnix

(1)

4.3 grep

123

$ grep pattern filename


this is the line containing pattern

$ grep "pattern" filename


this is the line containing pattern

(2) stdin
$ echo -e "this is a word\nnext line" | grep word
this is a word

(3) grep

$ grep "match_text" file1 file2 file3 ...

(4) --color
$ grep word filename --color=auto
this is the line containing word

(5) grepmatch_text
-Eextended
grepegrep

$ grep -E "[a-z]+" filename

$ egrep "[a-z]+" filename

(6) -o

$ echo this is a line. | egrep -o "[a-z]+\."


line.

(7) match_pattern

$ grep -v match_pattern file

-vinvert
(8)

$ grep -c "text" filename


10

-c
$ echo -e "1 2 3 4\nhello\n5 6" | egrep
2

-c "[0-9]"

124

62

(9)
$ echo -e "1 2 3 4\nhello\n5 6" | egrep -o "[0-9]" | wc -l
6

(10)
$ cat sample1.txt
gnu is not unix
linux is fun
bash is art
$ cat sample2.txt
planetlinux
$ grep linux -n sample1.txt
2:linux is fun

$ cat sample1.txt | grep linux -n

$ grep linux -n sample1.txt sample2.txt


sample1.txt:2:linux is fun
sample2.txt:2:planetlinux

(11)
$ echo gnu is not unix | grep -b -o "not"
7:not

0
"not"7not7gnu
is not unix
-b -o
(12)
$ grep -l linux sample1.txt sample2.txt
sample1.txt
sample2.txt

-l-L

4.3 grep

125

4.3.2
grepgrep
grep

1.

$ grep "text" . -R -n

grep-R-r

$ cd src_dir
$ grep "test_function()" . -R -n
./miscutils/test.c:16:test_function();

test_function()miscutils/test.c16

6
2.
-i
$ echo hello world | grep -i "HELLO"
hello

3. grep
-e
$ grep -e "pattern1" -e "pattern"

$ echo this is a line of text | grep -e "this" -e "line" -o


this
line

126

-fgrep
$ grep -f pattern_filesource_filename

$ cat pat_file
hello
cool
$ echo hello this is cool | grep -f pat_file
hello this is cool

4. grep
grepincludeexclude

includeexclude
.c .cpp
$ grep "main()" . -r

--include *.{c,cpp}

some{string1,string2,string3}somestring1 somestring2 somestring3


README
$ grep "main()" . -r --exclude "README"

--exclude-dir
--exclude-from FILE
5. 0grepxargs
xargs

0
New file.txtNew
file.txt0xargsgrep
findstdin0stdout
0\0xargs-0

$ echo "test" > file1


$ echo "cool" > file2
$ echo "test" > file3

4.3 grep

127

grep-Z0\0xargs -0
0

$ grep "test" file* -lZ | xargs -0 rm

-Z -l

6. grep

grep-qgrep

00
grep
#!/bin/bash
# : silent_grep.sh
# :
if [ $# -ne 2 ]; then
echo "Usage: $0 match_text filename"
exit 1
fi
match_text=$1
filename=$2
grep -q "$match_text" $filename
if [ $? -eq 0 ]; then
echo "The text exists in the file"
else
echo "Text does not exist in the file"
fi

silent_grep.shStudentstudent_data.txt

$ ./silent_grep.sh Student student_data.txt


The text exists in the file

7.
grepgrep
ngrep

128

3 -A
$ seq 10 | grep 5 -A 3
5
6
7
8

3 -B
$ seq 10 | grep 5 -B 3
2
3
4
5

3-C
$ seq 10 | grep 5 -C 3
2
3
4
5
6
7
8

--
$ echo -e "a\nb\nc\na\nb\nc" | grep a -A 1
a
b
-a
b

4.4

cut

RollNameMarkPercentagen

4.4.1
cutcut

4.4 cut

129

(1)

cut -f FIELD_LIST filename

FIELD_LIST
$ cut -f 2,3 filename

23
(2) cutstdin

cut -scut

$ cat student_data.txt
No Name Mark Percent
1 Sarath 45 90
2 Alex 49 98
3 Anu 45 90
$ cut -f1 student_data.txt
No
1
2
3

(3)
$ cut -f2,4 student_data.txt
Name
Percent
Sarath
90
Alex
98
Anu
90

(4) -f

(5) --complement
3
$ cut -f3 --complement student_data.txt
No Name
Percent
1
Sarath 90
2
Alex
98
3
Anu
90

(6) -d

130

$ cat delimited_data.txt
No;Name;Mark;Percent
1;Sarath;45;90
2;Alex;49;98
3;Anu;45;90
$ cut -f2 -d";" delimited_data.txt
Name
Sarath
Alex
Anu

4.4.2
cut

0
cut
4-2
4-2

N-

N-M

NMM

-M

1MM

-b
-c
-f

$ cat range_fields.txt
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxy

15

4.5 sed

131

$ cut -c1-5 range_fields.txt


abcde
abcde
abcde
abcde

2
$ cut range_fields.txt -c -2
ab
ab
ab
ab

-c b
-c-f -b
--output-delimiter "delimiter string"

-b-c --output-delimiter

$ cut range_fields.txt -c1-3,6-9 --output-delimiter ","


abc,fghi
abc,fghi
abc,fghi
abc,fghi

4.5

sed

sedstream editor

sedsed

4.5.1
(1) sed
$ sed 's/pattern/replace_string/' file

$ cat file | sed 's/pattern/replace_string/'

stdin

132

vi
sed

(2) sed
-i

$ sed 's/text/replace/' file >newfile


$ mv newfile file

$ sed -i 's/text/replace/' file

(3) sed
g
$ sed 's/pattern/replace_string/g' file

/gsedn
/Ng

$ echo thisthisthisthis | sed 's/this/THIS/2g'


thisTHISTHISTHIS
$ echo thisthisthisthis | sed 's/this/THIS/3g'
thisthisTHISTHIS
$ echo thisthisthisthis | sed 's/this/THIS/4g'
thisthisthisTHIS

/sed
sed 's:text:replace:g'
sed 's|text|replace|g'

\
sed 's|te\|xt|replace|g'

\|

4.5.2
sed

4.5 sed

133

1.
sed ^$
$ sed '/^$/d' file

/pattern/d

2.

sedstdout
-i
$ sed 's/PATTERN/replacement/' -i filename

3
$ cat sed_data.txt
11 abc 111 this 9 file contains 111 11 88 numbers 0000
$ sed -i 's/\b[0-9]\{3\}\b/NUMBER/g' sed_data.txt
$ cat sed_data.txt
11 abc NUMBER this 9 file contains NUMBER 11 88 numbers 0000

3\b[0-9]\{3\}\b3[0-9]
0~9{3}3\{3\}\{}
\b
-ised
-i
sed

sed i .bak 's/abc/def/' file

sedfile.bak

3. &
sed &

134

$ echo this is an example | sed 's/\w\+/[&]/g'


[this] [is] [an] [example]

\w\+ [&]&
4. \1
&

$ echo this is digit 7 in a number | sed 's/digit \([0-9]\)/\1/'


this is 7 in a number

digit 777\(pattern\)
() \1
\2
$ echo seven EIGHT | sed 's/\([a-z]\+\) \([A-Z]\+\)/\2 \1/'
EIGHT seven

([a-z]\+\)([A-Z]\+\)\1\2

back reference\2 \1

5.
sed
sed 'expression' | sed 'expression'

$ sed 'expression; expression'

$ sed -e 'expression' -e expression'

$ echo abc | sed 's/a/A/' | sed 's/c/C/'


AbC
$ echo abc | sed 's/a/A/;s/c/C/'
AbC
$ echo abc | sed -e 's/a/A/' -e 's/c/C/'
AbC

4.6 awk

135

6.

sed

sed

$ text=hello
$ echo hello world | sed "s/$text/HELLO/"
HELLO world

$texthello

4.6

awk

awkawk
C awk

4.6.1
awk
awk ' BEGIN{

print "start" } pattern { commands } END{ print "end" } file

awkstdin
awk3BEGINEND3

4.6.2

awk
awk 'BEGIN { statements } { statements } END { end statements }'

awk "BEGIN { statements } { statements } END { end statements }"

$ awk 'BEGIN { i=0 } { i++ } END{ print i}' filename

136

$ awk "BEGIN { i=0 } { i++ } END{ print i }" filename

4.6.3
awk

(1) BEGIN { commands }


(2) stdinpattern { commands }

(3) END { commands }


BEGINawk
BEGIN
ENDBEGINENDawk

END

pattern
{ print }awk
while
awk
{ }
awk{ }

$ echo -e "line1\nline2" | awk 'BEGIN{ print "Start" } { print } END{ print "End" } '
Start
line1
line2
End

printprint
printawkprint
concatenation operator

4.6 awk

137

$ echo | awk '{ var1="v1"; var2="v2"; var3="v3"; \


print var1,var2,var3 ; }'

v1 v2 v3

echoawk { } awk
{ }

$ echo | awk '{ var1="v1"; var2="v2"; var3="v3"; \


print var1 "-" var2 "-" var3 ; }'

v1-v2-v3

{ }

var=0
BEGINEND{}

4.6.4
awkawkawk

awk
1.
awk

NR
NF
$0
$1

$2

$ echo -e "line1 f2 f3\nline2 f4 f5\nline3 f6 f7" | \

138

awk '{
print "Line no:"NR",No of fields:"NF, "$0="$0, "$1="$1,"$2="$2,"$3="$3
}'
Line no:1,No of fields:3 $0=line1 f2 f3 $1=line1 $2=f2 $3=f3
Line no:2,No of fields:3 $0=line2 f4 f5 $1=line2 $2=f4 $3=f5
Line no:3,No of fields:3 $0=line3 f6 f7 $1=line3 $2=f6 $3=f7

print $NF $(NF-1)

awk printf() C
print

awk23
$awk '{ print $3,$2 }'

file

$ awk 'END{ print NR }' file

ENDawkNR
NRENDNR

$ seq 5 | awk 'BEGIN{ sum=0; print "Summation:" }


{ print $1"+"; sum+=$1 } END { print "=="; print sum }'
Summation:
1+
2+
3+
4+
5+
==
15

2. awk
-vstdinawk
$ VAR=10000
$ echo | awk -v VARIABLE=$VAR '{ print VARIABLE }'
10000

awk
$ var1="Variable1" ; var2="Variable2"
$ echo | awk '{ print v1,v2 }' v1=$var1 v2=$var2

4.6 awk

139

Variable1 Variable2

$ awk '{ print v1,v2 }' v1=$var1 v2=$var2 filename

-v1=$var1 v2=$var2awk
BEGIN{}END

3. getline
awkgetline
BEGIN

getline varvargetline
$0$1$2

$ seq 5 | awk 'BEGIN { getline; print "Read ahead first line", $0 } { print $0 }'
Read ahead first line 1
2
3
4
5

4. awk

$
$
$
$

awk
awk
awk
awk

'NR < 5' # 5


'NR==1,NR==4' # 15
'/linux/' # linux
'!/linux/' # linux

5.

-F "delimiter"
$ awk -F: '{ print $NF }' /etc/passwd

awk 'BEGIN { FS=":" } { print $NF }' /etc/passwd

BEGINOFS="delimiter"

140

6. awk
echocmdoutgrep root /etc/passwd
root
output
"command" | getline output ;

$ echo | awk '{ "grep root /etc/passwd" | getline cmdout ; print cmdout }'
root:x:0:0:root:/root:/bin/bash

getlineshellcmdout
awk

7. awk
awkfor
for(i=0;i<10;i++) { print $i ; }

for(i in array) { print array[i]; }

8. awk
awk
length(string)
index(string, search_string)search_string
split(string, array, delimiter)

substr(string, start-position, end-position)

sub(regex, replacement_str, string)

replacment_str
gsub(regex, replacment_str, string)sub()

match(regex, string)

00match()RSTARTRLENGTH

4.7

141

RSTARTRLENGTH

4.7

4.7.1

awksedgrep

sedawkgrep

4
4.7.2

#!/bin/bash
# word_freq.sh
# :
if [ $# -ne 1 ];
then
echo "Usage: $0 filename";
exit -1
fi

filename=$1
egrep -o "\b[[:alpha:]]+\b" $filename | \
awk '{ count[$0]++ }
END{ printf("%-14s%s\n","Word","Count") ;
for(ind in count)
{ printf("%-14s%d\n",ind,count[ind]); }

}'

$ ./word_freq.sh words.txt
Word
Count

142

used
this
counting

1
2

4.7.3
egrep -o "\b[[:alpha:]]+\b" $filename -o

\b [:alpha:] awk
awk{}
count[$0]++END{}

4.7.4
1.7Bash
4.6awk

4.8

JavaScript

JavaScriptJavaScript
JavaScript

JavaScriptminified
JSJavaScript
shell

4.8.1
JavaScript
Javascript
$ cat sample.js
function sign_out()
{
$("#loading").show();
$.get("log_in",{logout:"True"},

4.8 JavaScript

143

function(){
window.location="";
});

JavaScript

(1)
(2)
(3) /**/
JavaScript

;\n ;
{\n {\n} }

4.8.2

$ cat
tr -d
| sed
| sed

sample.js | \
'\n\t' | tr -s ' ' \
's:/\*.*\*/::g' \
's/ \?\([{}();,:]\) \?/\1/g'

function sign_out(){$("#loading").show();$.get("log_in",{logout:"True"},
function(){window.location="";});}

$ cat obfuscated.txt | sed 's/;/;\n/g; s/{/{\n\n/g; s/}/\n\n}/g'

$ cat obfuscated.txt | sed 's/;/;\n/g' | sed 's/{/{\n\n/g' | sed 's/}/\n\n}/g'

var a = "hello

world"

JavaScript

144

4.8.3

(1) '\n' '\t'


tr -d '\n\t'

(2)
tr -s ' 'sed 's/[ ]\+/ /g'

(3)
sed 's:/\*.*\*/::g'

/* */ :sed /

* sed \*
.* /* */
(4) {}();:,
sed 's/ \?\([{}();,:]\) \?/\1/g'

sed
sed / \?\([{ }();,:]\) \?/ /\1/g
\([{ }();,:]\) [ { }( ) ; , : ]

\(\)
()
\?

() \1

s/;/;\n/g ;;\n
s/{/{\n\n/g { {\n\n
s/}/\n\n}/g } \n\n}

4.9

145

4.8.4

2.6tr
4.5sed

4.9

cat

4.9.1
paste
$ paste file1 file2 file3

$ cat file1.txt
1
2
3
4
5
$ cat file2.txt
slynux
gnu
bash
hack
$ paste file1.txt file2.txt
1slynux
2gnu
3bash
4hack
5

-d
$ paste file1.txt file2.txt -d ","
1,slynux
2,gnu
3,bash
4,hack
5,

146

4.9.2
4.4

4.10

4.10.1
awkcut
(1) 5
$ awk '{ print $5 }' filename

(2)

$ ls -l | awk
-rw-r--r-- :
-rw-r--r-- :
-rw-r--r-- :
-rw-r--r-- :

'{ print $1 " : " $8 }'


delimited_data.txt
obfuscated.txt
paste1.txt
paste2.txt

4.10.2
4.4
4.6awk

4.11

4.11.1
awkgrepsedawk

4.12

147

awk

4.11.2
(1) MN

$ awk 'NR==M, NR==N' filename

stdin
$ cat filename | awk 'NR==M, NR==N'

(2) MN
$ seq 100 | awk 'NR==4,NR==6'
4
5
6

(3) start_patternend_pattern
$ awk '/start_pattern/, /end _pattern/' filename

$ cat section.txt
line with pattern1
line with pattern2
line with pattern3
line end with pattern4
line with pattern5
$ awk '/pa.*3/, /end/' section.txt
line with pattern3
line end with pattern4

awk

4.11.3
4.6awk

4.12
Bash

148

4.12.1
awktac
cat

4.12.2
tac
(1)
tac file1 file2

stdin
$ seq 5 | tac
5
4
3
2
1

tac\n -s ""

(2) awk
$ seq 9 | \
awk '{ lifo[NR]=$0 }
END{ for(lno=NR;lno>-1;lno--){ print lifo[lno]; }
}'

shell\

4.12.3
awk
NR awk END { }
lno=NR 0

4.13 URL

4.13

149

URL

URL
HTML

4.13.1

[A-Za-z0-9._]+@[A-Za-z0-9.]+\.[a-zA-Z]{2,4}

$ cat url_email.txt
this is a line of text contains,<email> #slynux@slynux.com. </email> and email address,
blog "http://www.google.com", test@yahoo.com dfdfdfdddfdf;cool.hacks@gmail.com<br />
<a href="http://code.google.com"><h1>Heading</h1>

+egrep
$ egrep -o '[A-Za-z0-9._]+@[A-Za-z0-9.]+\.[a-zA-Z]{2,4}'
slynux@slynux.com
test@yahoo.com
cool.hacks@gmail.com

url_email.txt

HTTP URLegrep

http://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,4}

$ egrep -o "http://[a-zA-Z0-9.]+\.[a-zA-Z]{2,3}" url_email.txt


http://www.google.com
http://code.google.com

4.13.2

name@domain.some_2-4_letter

[A-Za-z0-9.]+@[A-Za-z0-9.]+\.[a-zA-Z]{2,4}

150

[A-Za-z0-9.]+ @ []

+ [A-Za-z0-9.] + \.
.[a-zA-Z]{2,4}24
24
HTTP URLname@
http://[a-zA-Z0-9.]+\.[a-zA-Z]{2,3}

4.13.3
4.2
4.5sed

4.14

4.14.1
sedsed

4.14.2

$ cat sentence.txt
Linux refers to the family of Unix-like computer operating systems that use the Linux
kernel. Linux can be installed on a wide variety of computer hardware, ranging from
mobile phones, tablet computers and video game consoles, to mainframes and supercomputers.
Linux is predominantly known for its use in servers.

mobile phonessed
$ sed 's/ [^.]*mobile phones[^.]*\.//g' sentence.txt
Linux refers to the family of Unix-like computer operating systems
that use the Linux kernel. Linux is predominantly known for its use
in servers.

4.15

151

4.14.3

sed's/ [^.]*mobile phones[^.]*\.//g'


's///g'

.++
++
[^.][^.]* mobile phone
[^.]* ////

4.14.4
4.2
4.5sed

4.15

URIshell

4.15.1

findsed

.cppCopyrightCopyleft
$ find . -name *.cpp -print0 |

xargs -I{} -0 sed -i 's/Copyright/Copyleft/g' {}

4.15.2
find.cppprint0null\0

152

xargssedsed

4.15.3
find-execfind

$ find . -name *.cpp -exec sed -i 's/Copyright/Copyleft/g' \{\} \;

$ find . -name *.cpp -exec sed -i 's/Copyright/Copyleft/g' \{\} \+

sed
findsed

4.16
Bash

4.16.1

$ var="This is a line of text"


$ echo ${var/line/REPLACED}
This is a REPLACED of text"

lineREPLACED

${variable_name:start_position:length}

5
$ string=abcdefghijklmnopqrstuvwxyz
$ echo ${string:4}
efghijklmnopqrstuvwxyz

4.16

153

58

$ echo ${string:4:8}
efghijkl

0 -1
(-1)
echo ${string:(-1)}
z
$ echo ${string:(-2):2}
yz

4.16.2
4.5

154

5.1

Web

Twitter

cURL

Web

Gmail

POST

WebshellPHPWeb

HTTP
Web

5.2

Web
URL

5.2.1
wget

5.2

Web

155

5.2.2

wget
$ wget URL

$ wget http://slynux.org
--2010-08-01 07:51:20-- http://slynux.org/
Resolving slynux.org... 174.37.207.60
Connecting to slynux.org|174.37.207.60|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 15280 (15K) [text/html]
Saving to: "index.html"
100%[======================================>] 15,280

75.3K/s

in 0.2s

2010-08-01 07:51:21 (75.3 KB/s) - "index.html" saved [15280/15280]

URL
$ wget URL1 URL2 URL3 ..

5.2.3

URLstdout
-O
truncate

-ostdout
$ wget ftp://example_domain.com/somefile.img -O dloaded_file.img -o log

logdloaded_file.img

wget
-t

$ wget -t 5 URL

wget
$ wget -t 0 URL

156

5.2.4
wget

1.

wget

--limit-ratewget
$ wget

--limit-rate 20k http://example.com/file.iso

km
quota

--quota-Q
$ wget -Q 100m http://example.com/file1 http://example.com/file2

2.
wget-c
$ wget -c URL

3.
wgetURL

--mirror
$ wget --mirror --convert-links exampledomain.com

$ wget -r -N -l -k DEPTH URL

-lwget-rrecursive

-N URL -k
convert-linkswget

5.4 cURL

157

4. HTTPFTP

HTTPFTP--user--password
$ wget --user username --password pass URL

--password
--ask-password

5.3

HTMLJavascriptCSSHTMLHTML
Bash
HTML
HTMLHTML
LynxWeb

lynx-dumpASCII

$ lynx URL -dump > webpage_as_text.txt

<a href="link">
References

$lynx -dump http://google.com > plain_text_page.txt

cat

$ cat plain_text_page.txt

5.4

cURL
cURLHTTPHTTPSFTPPOST

158

cookiereferer

web page usage sequence


cURLcURL

5.4.1
LinuxcURL
wget
cURLstdoutstderr
--silent

5.4.2
curlHTTPHTTP
cURL
stdout

$ curl URL

curl--silent
$ curl URL --silent

OURL

$ curl URL --silent -O

# --progress --silent
$ curl http://slynux.org -o index.html --progress
################################## 100.0%

5.4.3
cURLstdoutURL
URLURLcurl
http://slynux.org -O silentURL
-o
$ curl URL --silent -o new_filename

5.4 cURL

159

5.4.4

HTMLcURL

1.

cURL
$ curl URL/file -C offset

cURL
cURL -C -

$ curl -C - URL

cURL
2.cURL
refererHTTP
ABB
AURL
HTMLGoogle
GooglelogoURL

www.google.comGoogle

curl --referer
$ curl --referer Referer_URL target_URL

$ curl --referer http://google.com http://slynux.org

3.cURLcookie

curlHTTPcookie

refererreferrer

160

cookie --cookie "COOKIES"


cookiesname=valuecookie
$ curl http://example.com --cookie "user=slynux;pass=hack"

cookie --cookie-jar
$ curl URL --cookie-jar cookie_file

4.cURL
user agent
Internet ExplorerIE
IEcurl

cURL --user-agent -A
$ curl URL --user-agent "Mozilla/5.0"

HTTPcURL-H""
$ curl -H "Host: www.slynux.org" -H "Accept-language: en" URL

http://www.useragentstring.com/pages/useragentstring.php
5. cURL
--limit-rate
cURL
$ curl URL --limit-rate 20k

km
6.
--max-filesize
$ curl URL --max-filesize bytes

00

5.5 Gmail

161

7. cURL

cURL -uHTTPFTP
-u username:password

$ curl -u user:pass http://test_auth.com

-u username
$ curl -u user http://test_auth.com

8.
response header
HTTP

HTTP
HTTP Content-Length
Last-Modified

-I--head HTTP
$ curl -I http://slynux.org
HTTP/1.1 200 OK
Date: Sun, 01 Aug 2010 05:08:09 GMT
Server: Apache/1.3.42 (Unix) mod_gzip/1.3.26.1a mod_log_bytes/1.2 mod_bwlimited/1.4
mod_auth_passthrough/1.8 FrontPage/5.0.2.2635 mod_ssl/2.8.31 OpenSSL/0.9.7a
Last-Modified: Thu, 19 Jul 2007 09:00:58 GMT
ETag: "17787f3-3bb0-469f284a"
Accept-Ranges: bytes
Content-Length: 15280
Connection: close
Content-Type: text/html

5.4.5
5.13

5.5

Gmail
Gmailhttp://mail.google.comGoogle

162

RSS feed
RSS feed

5.5.1
GmailRSS feed
#!/bin/bash
#: Gmail
username='PUT_USERNAME_HERE'
password='PUT_PASSWORD_HERE'
SHOW_COUNT=5 #
echo
curl -u $username:$password --silent "https://mail.google.com/mail/feed/atom" | \
tr -d '\n' | sed 's:</entry>:\n:g' |\
sed -n 's/.*<title>\(.*\)<\/title.*<author><name>\([^<]*\)<\/name><email>
\([^<]*\).*/From: \2 [\3] \nSubject: \1\n/p' | \
head -n $(( $SHOW_COUNT * 3 ))

:
$ ./fetch_gmail.sh
From: SLYNUX [ slynux@slynux.com ]
Subject: Book release - 2
From: SLYNUX [ slynux@slynux.com ]
Subject: Book release - 1
.
5

Gmail

5.5.2
cURL RSS feed -u username:
password-u usercURL

tr -d '\n'\n

5.6

163

sed 's:</entry>:\n:g' </entry>

https://mail.google.com/mail/feed/atomRSS feed
XML <entry> TAGS </entry>

sed 's/.*<title>\(.*\)<\/title.*<author><name>\([^<]*\)<\/name><email>
\([^<]*\).*/Author: \2 [\3] \nSubject: \1\n/'

<title>\(.*\)<\/title <author><name>\([^<]*\)<\/ name>


<email>\([^<]*\)

Author: \2 [\3] \nSubject: \1\n

\1\2

SHOW_COUNT=5

headSHOW_COUNT*3SHOW_COUNT3

5.5.3

5.4curl
4.5sed

5.6

sedawk4
grep http://www.
johntorres.net/BoxOfficefemaleList.html

5.6.1
8

$ lynx -dump -nolist http://www.johntorres.net/BoxOfficefemaleList.html


grep -o "Rank-.*" | \
sed -e 's/ *Rank-\([0-9]*\) *\(.*\)/\1\t\2/' | \

SHOW_COUNT*3echo

| \

164

sort -nk 1 > actresslist.txt

# 3
1
Keira Knightley
2
Natalie Portman
3
Monica Bellucci

5.6.2
LynxHTML

HTMLlynx-nolist
sedRank
sed -e 's/ *Rank-\([0-9]*\) *\(.*\)/\1\t\2/'

5.6.3
4.5sed
5.3lynx

5.7

image crawler
HTML

5.7.1
Bash
#!/bin/bash
#:
#: img_downloader.sh
if [ $# -ne 3 ];
then
echo "Usage: $0 URL -d DIRECTORY"
exit -1
fi

5.7

165

for i in {1..4}
do
case $1 in
-d) shift; directory=$1; shift ;;
*) url=${url:-$1}; shift;;
esac
done

mkdir -p $directory;
baseurl=$(echo $url | egrep -o "https?://[a-z.]+")
echo Downloading $url
curl -s $url | egrep -o "<img src=[^>]*>" |
sed 's/<img src=\"\([^"]*\).*/\1/g' > /tmp/$$.list

sed -i "s|^/|$baseurl/|" /tmp/$$.list


cd $directory;
while read filename;
do
echo Downloading $filename
curl -s -O "$filename" --silent

done < /tmp/$$.list

$ ./img_downloader.sh http://www.flickr.com/search/?q=linux -d images

5.7.2
HTML <img> <img>
src="URL" URL
[ $# -ne 3 ] 3

3URL
while [ -n "$1" ]
do
case $1 in
-d) shift; directory=$1; shift ;;
*) url=${url:-$1}; shift;;
esac
done

166

whilecase$1
-d
-d
$ ./img_downloader.sh -d DIR URL

$ ./img_downloader.sh URL -d DIR

shiftshift$2 $1shift
$3 $1 $1

-d*)default match
-d$1="" $1=URL$1=URL
""urlurl=${url:-$1}urlURL
$1
egrep -o "<img src=[^>]*>" <img> [^>]*
> <img src="image.jpg" .... >
sed 's/<img src=\"\([^"]*\).*/\1/g' src="url"

<img> URL
http:// https://
URL / http://example.com/image.jpg
/image.jpg
/URLbase URL http://example.com/
image.jpgURLbaseurl "s|^/|$baseurl/|"
/tmp/$$.list / baseurl
"s|^/|$baseurl/|" /tmp/$$.list

whileURLcurlcurl

--silent

5.7.3
5.4curl
4.5sed
4.3grep

5.8

5.8

167

Web
<img>

Bash
<img>

3
5.8.1
for
BashcatconvertImage Magick
HTMLindex.html5-1

5.8.2

5-1

HTMLBash
#!/bin/bash
#: generate_album.sh
#:

168

echo "Creating album.."


mkdir -p thumbs
cat <<EOF1 > index.html
<html>
<head>
<style>
body
{
width:470px;
margin:auto;
border: 1px dashed grey;
padding:10px;
}
img
{
margin:5px;
border: 1px solid black;
}
</style>
</head>
<body>
<center><h1> #Album title </h1></center>
<p>
EOF1
for img in *.jpg;
do
convert "$img" -resize "100x" "thumbs/$img"
echo "<a href=\"$img\" ><img src=\"thumbs/$img\" title=\"$img\" /></a>" >>
index.html
done
cat <<EOF2 >> index.html
</p>
</body>
</html>
EOF2
echo Album generated to index.html

$ ./generate_album.sh
Creating album..
Album generated to index.html

5.9 Twitter

169

5.8.3

HTML
EOF1EOF1index.html
cat <<EOF1 > index.html
contents...
EOF1

HTMLCSS
for img in *.jpg;

convert "$img" -resize "100x" "thumbs/$img"100

<img>index.html
echo "<a href=\"$img\" ><img src=\"thumbs/$img\" title=\"$img\" /></a>" >> index.html

catHTML

5.8.4

1.6EOFstdin

5.9

Twitter

TwitterTwitter API

5.9.1

TwitterHTTPplain HTTP Authentication


OAuthauthenticate ourselvesOAuth
BashOAuth
(1) https://github.com/livibetter/bash-oauth/archive/master.zipbash-oauth

(2) bash-oauth-masterrootmake install-all


(3) https://dev.twitter.com/apps/newOAuth5-2

170

5-2

(4) Access typeRead and Write5-3

5-3

5.9 Twitter

171

(5) DetailsConsumer KeyConsumer Secret


5-4

5-4

5.9.2
Bash

#!/bin/bash
#: twitter.sh
#:twitter
oauth_consumer_key=YOUR_CONSUMER_KEY

oauth_consumer_secret=YOUR_CONSUMER_SECRET
config_file=~/.$oauth_consumer_key-$oauth_consumer_secret-rc
if [[ "$1" != "read" ]] && [[ "$1" != "tweet" ]];
then

172

echo -e "Usage: $0 tweet status_message\n

OR\n

$0 read\n"

exit -1;
fi
source TwitterOAuth.sh
TO_init
if [ ! -e $config_file ]; then
TO_access_token_helper
if (( $? == 0 )); then
echo oauth_token=${TO_ret[0]} > $config_file
echo oauth_token_secret=${TO_ret[1]} >> $config_file
fi
fi
source $config_file
if [[ "$1" = "read" ]];
then
TO_statuses_home_timeline '' 'shantanutushar' '10'
echo $TO_ret | sed 's/<\([a-z]\)/\n<\1/g' | \
grep -e '^<text>' -e '^<name>' | sed 's/<name>/\ - by /g' | \
sed 's$</*[a-z]*>$$g'
elif [[ "$1" = "tweet" ]];
then
shift
TO_statuses_update '' "$@"
echo 'Tweeted :)'
fi

$./twitter.sh read
Please go to the following link to get the PIN:
https://api.twitter.com/oauth/authorize?oauth_token=GaZcfsdnhMO4HiBQuUTdeLJAzeaUam
nOljWGnU
PIN: 4727143
Now you can create, edit and present Slides offline.
- by A Googler
$./twitter.sh tweet "I am reading Packt Shell Scripting Cookbook"
Tweeted :)
$./twitter.sh read | head -2
I am reading Packt Shell Scripting Cookbook
- by Shantanu Tushar Jha

5.10 Web

173

5.9.3

sourceTwitterOAuth.sh
TwitterTO_init
OAuthtokentoken
secretTO_access_token_helper
configsource

TO_statuses_home_timeline Twitter
TwitterXMLTO_retsedtag
<text><name>

TO_statuses_update

5.9.4
5.4sed

4.3grep

5.10

Web

APIAPImachine-readable
API

5.10.1
curlsedgrep
APIMerriam-WebsterAPI

(1) http://www.dictionaryapi.com/register/index.htmCollegiate Dictionary


Learners Dictionary5-5

174

5-5

(2) My KeysLearners Dictionary


5-6

5-6

5.11

175

5.10.2

#!/bin/bash
#: define.sh
#: dictionaryapi.com

apikey=YOUR_API_KEY_HERE
if [ $# -ne 2 ];
then
echo -e "Usage: $0 WORD NUMBER"
exit -1;
fi

curl --silent
http://www.dictionaryapi.com/api/v1/references/learners/xml/$1?key=$apikey | \
grep -o \<dt\>.*\</dt\> | \
sed 's$</*[a-z]*>$$g' | \
head -n $2 | nl

$ ./define.sh usb 1
1 :a system for connecting a computer to another device (such as a printer,
keyboard, or mouse) by using a special kind of cord a USB cable/port USB is an
abbreviation of "Universal Serial Bus."How it works...

5.10.3
API key$apikeycurlAPI
$1
<tag>grepsed
n1

5.10.4
4.5sed
4.3grep

5.11

176

HTTP

5.11.1
lynx curl lynx
-traversalcurl

5.11.2
curlBash
#!/bin/bash
#: find_broken.sh
#:
if [ $# -ne 1 ];
then
echo -e "$Usage: $0 URL\n"
exit 1;
fi

echo Broken links:


mkdir /tmp/$$.lynx
cd /tmp/$$.lynx
lynx -traversal $1 > /dev/null
count=0;
sort -u reject.dat > links.txt
while read link;
do
output=`curl -I $link -s | grep "HTTP/.*OK"`;
if [[ -z $output ]];
then
echo $link;
let count++
fi
done < links.txt
[ $count -eq 0 ] && echo No broken links found.

5.12

177

5.11.3

lynx -traversal URLreject.dat

sort -u
curl -IHTTP/1.0 200 OK

reject.datURL
lynxURL
lynxtraverse.errors
URLlynxHTTP 404(not found)URL
URLHTTP 403 Forbidden

4
5.11.4
5.3lynx
5.4curl

5.12

Web
change tracker

5.12.1
Bashdiff
curldiff

5.12.2
Bash

178

#!/bin/bash
#: change_track.sh
#:
if [ $# -ne 1 ];
then
echo -e "$Usage: $0 URL\n"
exit 1;
fi
first_time=0
#
if [ ! -e "last.html" ];
then
first_time=1
#
fi
curl --silent $1 -o recent.html
if [ $first_time -ne 1 ];
then
changes=$(diff -u last.html recent.html)
if [ -n "$changes" ];
then
echo -e "Changes:\n"
echo "$changes"
else
echo -e "\nWebsite has no changes"
fi
else
echo "[First run] Archiving.."
fi

cp recent.html last.html

track_changes.sh

$ ./track_changes.sh http://web.sarathlakshman.info/test.html
[First run] Archiving..


$ ./track_changes.sh http://web.sarathlakshman.info/test.html
Website has no changes

5.13 POST

179

$ ./test.sh http://web.sarathlakshman.info/test_change/test.html
Changes:
--- last.html 2010-08-01 07:29:15.000000000 +0200
+++ recent.html
2010-08-01 07:29:43.000000000 +0200
@@ -1,3 +1,4 @@
<html>
+added line :)
<p>data</p>
</html>

3
5.12.3
[ ! -e "last.html" ]; last.html
last.html

recent.htmldiff
recent.htmllast.html

5.12.4

5.4curl

5.13

POST

POSTGETHTTPGET
URL-
POSTPOST

POSTPOST
shellPOST
HTTP GETPOST

5.13.1
http://book.sarathlakshman.com/lsc/mlogs/
HOSTNAMEUSERSUBMIT

180

SUBMIT
curlwgetPOST

5.13.2
curlPOSTHTML

$ curl URL -d "postvar=postdata2&postvar2=postdata2"

$ curl http://book.sarathlakshman.com/lsc/mlogs/submit.php -d
"host=test-host&user=slynux"
<html>
You have entered :
<p>HOST : test-host</p>
<p>USER : slynux</p>
<html>

curl
-d POST -d GET
var=value&

wgetpost-data string
$ get http://book.sarathlakshman.com/lsc/mlogs/submit.php --post-data
"host=test-host&user=slynux" -O output.html
$ cat output.html
<html>
You have entered :
<p>HOST : test-host</p>
<p>USER : slynux</p>
<html>

cURL
-d& shell

5.13.3

HTML

5.13 POST

181

<form action="http://book.sarathlakshman.com/lsc/mlogs/submit.php" method="post" >

<input type="text" name="host" value="HOSTNAME" >


<input type="text" name="user" value="USER" >
<input type="submit" >
</form>

http://book.sarathlakshman.com/lsc/mlogs/submit.phpURL
SUBMITPOSTsubmit.php

5.13.4

5.4curl
5.2wget

182

tar

cpio

raync

gzip

Git

zip

fsarchiver

pbzip2

6.1

shell

shell

6.2

tar

tartar
tar
Tarballtar

6.2.1
Unixtartar

6.2 tar

183

Acdruxfv

6.2.2
tar
(1) tar

$ tar -cf output.tar [SOURCES]

$ tar -cf output.tar file1 file2 file3 folder1 ..

(2) -t
$ tar -tf archive.tar
file1
file2

(3) -v-vv

verbose

$ tar -tvf archive.tar


-rw-rw-r-- shaan/shaan
-rw-rw-r-- shaan/shaan

0 2013-04-08 21:34 file1


0 2013-04-08 21:34 file2

-f-f

$ tar -cvf output.tar file1 file2 file3 folder1 ..

7
6.2.3
-ccreat file-f
specify filename
SOURCE *.txt
taroutput.tar
tar

184

6.2.4
tar
1.
-r

$ tar -rvf original.tar new_file

$ tar -cf archive.tar hello.txt

$ tar -tf archive.tar


hello.txt

$ tar -rf archive.tar world.txt


$ tar -tf archive.tar
hello.txt
world.txt

2.

$ tar -xf archive.tar

-xexact
-xtar-C

$ tar -xf archive.tar -C /path/to/extraction_directory

$ tar -xvf file.tar file1 file4

file1file4

6.2 tar

185

3. tarstdinstdout

stdout
stdin

shellSecure ShellSSH

$ tar cvf - files/ | ssh user@example.com "tar xv -C Documents/"

filesstdout'-'

4.

-Atar
tarfile1.tarfile2.tarfile2.tar
file1.tar
$ tar -Af file1.tar file2.tar

$ tar -tvf file1.tar

5.

-u

$ tar -tf archive.tar


filea
fileb
filec

fileaarchive.tar
$ tar -uf archive.tar filea

filea

touchtar
$ tar -uvvf archive.tar filea
-rw-r--r-- slynux/slynux
0 2010-08-14 17:53 filea

186

$ tar -tf archive.tar


-rw-r--r-- slynux/slynux
-rw-r--r-- slynux/slynux
-rw-r--r-- slynux/slynux
-rw-r--r-- slynux/slynux

0
0
0
0

2010-08-14
2010-08-14
2010-08-14
2010-08-14

17:52
17:52
17:52
17:53

filea
fileb
filec
filea

fileatarfilea

6.
-d

$ tar -df archive.tar


afile: Mod time differs
afile: Size differs

7.
--delete
$ tar -f archive.tar --delete file1 file2 ..

$ tar --delete --file archive.tar [FILE LIST]

$ tar -tf archive.tar


filea
fileb
filec

filea
$ tar --delete --file archive.tar filea
$ tar -tf archive.tar
fileb
filec

8. tar
tar

6.2 tar

187

file.tar.gz

file.tar.bz2
file.tar.lzma

tar
-jbunzip2

-zgzip
--lzmalzma

tar
tar -a --auto-compress

$ tar acvf archive.tar.gz filea fileb filec


filea
fileb
filec
$ tar tf archive.tar.gz
filea
fileb
filec

9.
--exclude [PATTERN]

.txt
$ tar -cf arch.tar * --exclude "*.txt"

shell

-X

$ cat list
filea
fileb
$ tar -cf arch.tar * -X list

188

fileafileb
10.
tar
subversionGitmercurialcvs
.svn.gittar

tar --exclude-vcs
$ tar --exclude-vcs -czvvf source_code.tar.gz eye_of_gnome_svn

11.
totals
$ tar -cf arc.tar * --exclude "*.txt" --totals
Total bytes written: 20480 (20KiB, 12MiB/s)

6.2.5
6.4gzip

6.3

cpio

cpiotar
cpiotarRPM
FedoraLinuxinitramfs
cpio

6.3.1
cpiostdinstdoutstdout

cpio
(1)
$ touch file1 file2 file3

(2)
$ echo file1 file2 file3 | cpio -ov > archive.cpio

6.4 gzip

189

(3) cpio

$ cpio -it < archive.cpio

(4) cpio
$ cpio -id < archive.cpio

6.3.2

-o

-v

cpio/usr/somedir
/
/ test/file
testfiletest
cpiotar
/

cpio
-i
-t

-dcpio

6.4

gzip

gzipGNU/Linuxgzipgunzipzcat

gzip
targzipgzip

6.4.1
gzip

190

(1) gzip
$ gzip filename
$ ls
filename.gz

(2) gzip
$ gunzip filename.gz
$ ls
file

(3)
$ gzip -l test.txt.gz
compressed
uncompressed ratio uncompressed_name
35
6 -33.3% test.txt

(4) gzipstdinstdout
stdinstdout
$ cat file | gzip -c > file.gz

-cstdout
(5) gzip --fast --best

6.4.2
gzip

gzip
1.
gzipgzip
1
$ tar -czvvf archive.tar.gz [FILES]

$ tar -cavvf archive.tar.gz [FILES]

-a

6.4 gzip

191

tar
$ tar -cvvf archive.tar [FILES]

tar

$ gzip archive.tar

2
tartar
-r
FILE_LIST="file1

file2

file3

file4

file5"

for f in $FILE_LIST;
do
tar -rvf archive.tar $f
done

gzip archive.tar

gzip

$ tar -xavvf archive.tar.gz -C extract_directory

-a
2. zcatgzip

zcat.gzstdout

.gzstdout
$ ls
test.gz

$ zcat test.gz
A test file
# test"A test file"
$ ls
test.gz

3.
9

192

1
9

$ gzip -5 test.img

4. bzip2
bzip2gzipbzip2

gzipgzip
bzip2
$ bzip2 filename

bzip2
$ bunzip2 filename.bz2

tar.bz2tar.gz
$ tar -xjvf archive.tar.bz2

-jbzip2
5. lzma
lzmagzipbzip2

lzma
$ lzma filename

lzma
$ unlzma filename.lzma

tar--lzmatar
$ tar -cvvf --lzma archive.tar.lzma [FILES]

$ tar -cavvf archive.tar.lzma [FILES]

lzmatar

6.5 zip

193

$ tar -xvvf --lzma archive.tar.lzma -C extract_directory

-x--lzmalzma

$ tar -xavvf archive.tar.lzma -C extract_directory

6.4.3
6.2tar

6.5

zip

ZIPLinux
gzipbzip2Internet
zip

6.5.1
zip

(1) ZIP
$ zip archive_name.zip [SOURCE FILES/DIRS]

$ zip file.zip file

file.zip
(2)

$ zip -r archive.zip folder1 folder2

-r
(3) ZIP

$ unzip file.zip

unzipfile.zipunlzmagunzip

194

(1) -u
$ zip file.zip -u newfile

(2) -d
$ zip -d arc.zip file.txt

(3)
$ unzip -l archive.zip

6.5.2
zip
lzmagzipbzip2tarzip
tar

6.6

pbzip2

CPU
CPU

pbzip2

6.6.1
pbzip2

6.6.2
pbzip2
(1)
pbzip2 myfile.tar

pbzip2myfile.tarmyfile.tar.bz2

(2) tarpbzip2
tar cf myfile.tar.bz2 --use-compress-prog=pbzip2 dir_to_compress/

6.7

195

tar -c directory_to_compress/ | pbzip2 -c > myfile.tar.bz2

(3) pbzip2
tar.bz2

pbzip2 -dc myfile.tar.bz2 | tar x

pbzip2
pbzip2 -d myfile.tar.bz2

6.6.3
pbzip2bzip2pthreads

gzipbzip2pbzip2
tar

6.6.4

pbzip2

1.
pbzip2-p
-p

pbzip2 -p4 myfile.tar

pbzip24

2.
19

6.7

squashfs2GB~3GB
700MBLinux LiveCDLiveUSBsquashfsCD

196

Linux

squashfs

squashfs

6.7.1
squashfsgziplzmaLinux
squashfssqushfs-tools

6.7.2
squashfs
(1) squashfs
$ mksquashfs SOURCES compressedfs.squashfs

SOURCES

$ sudo mksquashfs /etc test.squashfs


Parallel mksquashfs: Using 2 processors
Creating 4.0 filesystem on test.squashfs, block size 131072.
[=======================================] 1867/1867 100%

(2) squashfs
# mkdir /mnt/squash
# mount -o loop compressedfs.squashfs /mnt/squash

/mnt/squashfs

6.7.3
squashfs

6.8 rsync

197

squashfs

squashfs
-e
$ sudo mksquashfs /etc test.squashfs -e /etc/passwd /etc/shadow

-epasswdshadow
-ef
$ cat excludelist
/etc/passwd
/etc/shadow

$ sudo mksquashfs /etc test.squashfs -ef excludelist

-wildcard

6.8

rsync

Web
rsync
cprsync
rsync
rsyncrsync

6.8.1

rsync
(1)

$ rsync -av source_path destination_path

$ rsync -av /home/slynux/data slynux@192.168.0.6:/home/backups/data

-a
-vstdout

198

(2)
$ rsync -av source_dir username@host:PATH

rsync

(3)
$ rsync -av username@host:PATH destination

rsyncSSHuser@host

userhost
IP PATH

OpenSSH
7.8
(4) rsync -z

$ rsync -avz source destination

(5)
$ rsync -av /home/test/ /home/backups

/home/testbackups

(6)
$ rsync -av /home/test /home/backups

/home/testbackups
/rsync
sourch_path
/rsyncsourch_path

6.8 rsync

199

test

$ rsync -av /home/test /home/backups/


$ rsync -av /home/test /home/backups

destination_path/rsync

/rsync

6.8.2

rsync
SSHrsync

/home/slynux/data()
slynux@192.168.0.6:/home/backups/data

/home/slynux/datarsyncslynux@192.168.0.6:/home/
backups/data IP 192.168.0.6 slynux
/home/backups/data

6.8.3
rsync

1. rsync
rsync

--exclude PATTERN

$ rsync -avz /home/code/some_code /mnt/disk/backup/code --exclude "*.txt"

.txt

--exclude-from FILEPATH

200

2. rsync
rsync
rsync --delete
$ rsync -avz SOURCE DESTINATION --delete

3.
cron

$ crontab -ev

0 */10 * * * rsync -avz /home/code user@IP_ADDRESS:/home/backups

crontabrsync10
*/10crontabhour position
/1010
*/10minutes postion
10

9.7crontab

6.9

Git

rsync

Git
Git

6.9.1

Git

6.9 Git

6.9.2

201

Git
(1)
$ cd /home/data/source

(2)
$ mkdir -p /home/backups/backup.git
$ cd /home/backups/backup.git
$ git init --bare

(1) Git

$ git config --global user.name "Sarath Lakshman"


$ git config --global user.email slynux@slynux.com

(2)
$ git init
Initialized empty Git repository in /home/backups/backup.git/

$ git commit --allow-empty -am "Init"


[master (root-commit) b595488] Init

(3) Git

$ git remote add origin user@remotehost:/home/backups/backup.git


$ git push origin master
Counting objects: 2, done.
Writing objects: 100% (2/2), 153 bytes, done.
Total 2 (delta 0), reused 0 (delta 0)
To user@remotehost:/home/backups/backup.git
* [new branch]
master -> master

(4) Git

$ git add *

202

$ git add *.txt


$ git add *.py

$ git rm file

$ git rm *.txt

(5)

$ git commit -m "Commit Message"

cron5

crontab
0 */5 * * *

/home/data/backup.sh

/home/data/backup.sh
#!/bin/ bash
cd /home/data/source
git add .
git commit -am "Backup taken at @ $(date)"
git push

(6)
$ git log

(7) 32ID
git checkoutID
ID 3131f9661ec1739f72c213ec5769bc0abefa85a9
$ git checkout 3131f9661ec1739f72c213ec5769bc0abefa85a9

$ git commit -am "Restore @ $(date) commit ID: 3131f9661ec1739f72c213ec5769


bc0abefa85a9"

6.10 fsarchiver

203

$ git log

(8)

$ git clone user@remotehost:/home/backups/backup.git

Git
Git/
Git

6.10

fsarchiver

fsarchiver

fsarchiver partimage

ext4 partimage

6.10.1
fsarchiver
http://www.fsarchiver.org/Installation

6.10.2
(1) /

fsarchiversavefs
fsarchiver savefs backup.fsa /dev/sda1

backup.fsa/dev/sda/

204

(2)
savefsfsarchiver
fsarchiver savefs backup.fsa /dev/sda1 /dev/sda2

(3)
fsarchiverrestfs
fsarchiver restfs backup.fsa id=0,dest=/dev/sda1

id=0
dest=/dev/sda1

(4)
restfs
fsarchiver restfs backup.fsa id=0,dest=/dev/sda1 id=1,dest=/dev/sdb1

iddestfsarchiver

6.10.3
tarfsarchiver
tarfsarchiver

/dev/sda1Linux/dev
sda1sdSATA disk
abc6-1

6-1

Linux

7.2

205

SSH

ping

SSH

iptables

SSH

7.1

TCP/IPIP
DNS

shellshell

7.2

IPGNU/Linux

206

7.2.1
Linuxeth0eth1
usb0wlan0USBLAN
ifconfigroutenslookuphost
ifconfig/sbin/ifconfig

7.2.2
(1)
$ ifconfig
lo
Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:6078 errors:0 dropped:0 overruns:0 frame:0
TX packets:6078 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:634520 (634.5 KB) TX bytes:634520 (634.5 KB)
wlan0
Link encap:EthernetHWaddr 00:1c:bf:87:25:d2
inet addr:192.168.0.82 Bcast:192.168.3.255 Mask:255.255.252.0
inet6addr: fe80::21c:bfff:fe87:25d2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:420917 errors:0 dropped:0 overruns:0 frame:0
TX packets:86820 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:98027420 (98.0 MB) TX bytes:22602672 (22.6 MB)

ifconfig

(2) IP
# ifconfig wlan0 192.168.0.80

root192.168.0.80IP
IP
# ifconfig wlan0 192.168.0.80

netmask 255.255.252.0

(3) IP

# dhclient eth0

7.2

207

7.2.3

1.

$ ifconfig | cut -c-10 | tr -d ' ' | tr -s '\n'


lo
wlan0

ifconfig10cut

10tr -d ' 'tr -s '\n'

2.IP
ifconfig

$ ifconfig iface_name

$ ifconfig wlan0
wlan0
Link encap:EthernetHWaddr 00:1c:bf:87:25:d2
inet addr:192.168.0.82 Bcast:192.168.3.255
Mask:255.255.252.0

IP

HWaddr 00:1c:bf:87:25:d2MAC

inet addr:192.168.0.82IP
Bcast:192.168.3.255

Mask:255.255.252.0

IPifconfigIP
$ ifconfig wlan0 | egrep -o "inet addr:[^ ]*" | grep -o "[0-9.]*"
192.168.0.82

egrep -o "inet addr:[^ ]*" inet addr:192.168.0.82inet


addr: [^ ]*
.

208

3. MAC

ifconfigHWaddr 00:1c:bf:87:25:d2

# ifconfig eth0 hw ether 00:1c:bf:87:25:d5

00:1c:bf:87:25:d5MACMAC
InternetInternet

4. DNS
IP202.11.32.75Internet
URLASCIIgoogle.com
IPIPwww.google.com
IPDNSgoogle.com
DNSIP
DNS
/etc/resolv.conf
$ cat /etc/resolv.conf
nameserver 8.8.8.8

# echo nameserver IP_ADDRESS >> /etc/resolv.conf

IPIPping

$ ping google.com
PING google.com (64.233.181.106) 56(84) bytes of data.
#64.233.181.106google.comIP

IPping
IPDNS
5. DNS
DNSDNSIP host
nslookupDNS

7.2

209

hostIPnslookuphost
DNS
$ host google.com
google.com has address
google.com has address
google.com has address
google.com has address
google.com has address
google.com has address

64.233.181.105
64.233.181.99
64.233.181.147
64.233.181.106
64.233.181.103
64.233.181.104

hostDNSDNS resource record)

$ nslookup google.com
Server:
8.8.8.8
Address: 8.8.8.8#53
Non-authoritative answer:
Name: google.com
Address: 64.233.181.105
Name: google.com
Address: 64.233.181.99
Name: google.com
Address: 64.233.181.147
Name: google.com
Address: 64.233.181.106
Name: google.com
Address: 64.233.181.103
Name: google.com
Address: 64.233.181.104
Server:

8.8.8.8

DNS
DNSIP /etc/hosts

# echo IP_ADDRESS symbolic_name >> /etc/hosts

# echo 192.168.0.9 backupserver >> /etc/hosts

backupserver192.168.0.9

210

6.

$ route
Kernel IP routing table
Destination
Gateway
192.168.0.0
*
link-local
*
default
p4.local

Genmask
Flags
255.255.252.0 U
255.255.0.0
U
0.0.0.0
UG

Metric
2
1000
0

Ref
0
0
0

UseIface
0wlan0
0wlan0
0wlan0

$ route -n
Kernel IP routing table
Destination
Gateway
192.168.0.0
0.0.0.0
169.254.0.0
0.0.0.0
0.0.0.0
192.168.0.4

Genmask
Flags Metric Ref
255.255.252.0
U
2
0
255.255.0.0
U
1000 0
0.0.0.0
UG
0
0

Use
0
0
0

Iface
wlan0
wlan0
wlan0

-n-nrouteIP

IPDNS

# route add default gw IP_ADDRESS INTERFACE_NAME

# route add default gw 192.168.0.1 wlan0

7.2.4
1.3PATH
4.3grep

7.3

ping

ping

7.3 ping

211

ping

7.3.1
pingInternet Control
Message ProtocolICMPechoecho

$ ping ADDRESS

ADDRESSIP
pingCtrl+Cping


$ ping 192.168.0.1
PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
64 bytes from 192.168.0.1: icmp_seq=1 ttl=64 time=1.44 ms
^C
--- 192.168.0.1 ping statistics --1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 1.440/1.440/1.440/0.000 ms
$ ping google.com
PING google.com (209.85.153.104) 56(84) bytes of data.
64 bytes from bom01s01-in-f104.1e100.net (209.85.153.104): icmp_seq=1 ttl=53
time=123 ms
^C
--- google.com ping statistics --1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 123.388/123.388/123.388/0.000 ms


$ ping 192.168.0.99
PING 192.168.0.99 (192.168.0.99) 56(84) bytes of data.
From 192.168.0.82 icmp_seq=1 Destination Host Unreachable
From 192.168.0.82 icmp_seq=2 Destination Host Unreachable

pingDestination Host Unreachable

212

ping
pingIP

7.3.2
ping
ping
1.
pingRound Trip TimeRTT

RTTping
--- google.com ping statistics --5 packets transmitted, 5 received, 0% packet loss, time 4000ms
rtt min/avg/max/mdev = 118.012/206.630/347.186/77.713 ms

RTT118.012msRTT206.630msRTT347.186mspingmdev
77.713msmean deviation
2.
pingechoCtrl+C -c
echo
-c COUNT

$ ping 192.168.0.1 -c 2
PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
64 bytes from 192.168.0.1: icmp_seq=1 ttl=64 time=4.02 ms
64 bytes from 192.168.0.1: icmp_seq=2 ttl=64 time=1.03 ms
--- 192.168.0.1 ping statistics --2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 1.039/2.533/4.028/1.495 ms

ping2echoping
IP
3. ping
ping00

7.4

213

$ ping domain -c2


if [ $? -eq 0 ];
then
echo Successful ;
else
echo Failure
fi

4. Traceroute

traceroute
traceroute
hop
metrictraceroute
$ traceroute google.com
traceroute to google.com (74.125.77.104), 30 hops max, 60 byte packets
1 gw-c6509.lxb.as5577.net (195.26.4.1) 0.313 ms 0.371 ms 0.457 ms
2 40g.lxb-fra.as5577.net (83.243.12.2) 4.684 ms 4.754 ms 4.823 ms
3 de-cix10.net.google.com (80.81.192.108) 5.312 ms 5.348 ms 5.327 ms
4 209.85.255.170 (209.85.255.170) 5.816 ms 5.791 ms 209.85.255.172
(209.85.255.172) 5.678 ms
5 209.85.250.140 (209.85.250.140) 10.126 ms 9.867 ms 10.754 ms
6 64.233.175.246 (64.233.175.246) 12.940 ms 72.14.233.114
(72.14.233.114) 13.736 ms 13.803 ms
7 72.14.239.199 (72.14.239.199) 14.618 ms 209.85.255.166
(209.85.255.166) 12.755 ms 209.85.255.143 (209.85.255.143) 13.803 ms
8 209.85.255.98 (209.85.255.98) 22.625 ms 209.85.255.110
(209.85.255.110) 14.122 ms
*
9 ew-in-f104.1e100.net (74.125.77.104) 13.061 ms 13.256 ms 13.484 ms

Linuxmtrtraceroute

7.4

shell

214

7.4.1
pingfping
fpingpingfpingLinux

7.4.2
shell
(1) 1
pingIP
#!/bin/bash
# : ping.sh
#192.168.0
for ip in 192.168.0.{1..255} ;
do
ping $ip -c 2 &> /dev/null ;
if [ $? -eq 0 ];
then
echo $ip is alive
fi
done

$ ./ping.sh
192.168.0.1 is alive
192.168.0.90 is alive

(2) fping

$ fping -a 192.160.1/24 -g 2> /dev/null


192.168.0.1
192.168.0.90

$ fping -a 192.168.0.1 192.168.0.255 -g

7.4

215

7.4.3

1pingforIP
IP192.168.0.{1..255}{start..end}shell
IP192.168.0.1192.168.0.2192.168.0.3192.168.0.255
ping $ip -c 2 &> /dev/nullpingIP-c 2echo

2&> /dev/nullstderrstdout /dev/null


$? 00
pingIP
pingIP
sequential programpingping
echo

7.4.4

1. ping
255
pingping( )& ( )shell
&

#!/bin/bash
# : fast_ping.sh
#192.168.0
for ip in 192.168.0.{1..255} ;
do
(
ping $ip -c2 &> /dev/null ;
if [ $? -eq 0 ];
then
echo $ip is alive
fi
)&
done
wait

216

for

waitwait

2. fping
fpingpingIP
fping
-aIP
-u
-g "IP/""IP"IP
$ fping -a 192.160.1/24 -g

$ fping -a 192.160.1 192.168.0.255 -g

2>/dev/nullnull

IPstdin
$
#
$
#

fping -a 192.168.0.1 192.168.0.5 192.168.0.6


IP
fping -a < ip.list
IP

7.4.5
1.6
1.17

7.5

SSH

SSHshell
SSHSecure Shell

7.5.1
GNU/Linux SSH openssh-server
openssh-clientSSH22

7.5 SSH

217

7.5.2

(1) SSH
$ ssh username@remote_host

username
remote_hostIP

$ ssh mec@192.168.0.1
The authenticity of host '192.168.0.1 (192.168.0.1)' can't be established.
RSA key fingerprint is 2b:b4:90:79:49:0a:f1:b3:8a:db:9f:73:2d:75:d6:f9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.1' (RSA) to the list of known hosts.
Password:
Last login: Fri Sep
mec@proxy-1:~$

3 05:15:21 2010 from 192.168.0.82

sshshell

SSHfingerprint verification
man-in-the-middle attack

SSH
SSH22SSH
SSH-p port_num
(2) 422SSH

$ ssh user@locahost -p 422

shellshellSSH
shell

SSH7.8

218

(3) shell
$ ssh user@host 'COMMANDS'

$ ssh mec@192.168.0.1 'whoami'


mec

$ ssh user@host "command1 ; command2 ; command3"

$ ssh mec@192.168.0.1
Password:
user: mec
OS: Linux

"echo user: $(whoami);echo OS: $(uname)"

echo user: $(whoami);


echo OS: $(uname)

COMMANDS="command1; command2; command3"


$ ssh user@hostname "$COMMANDS"

shellshell
(4) SSHshell
uptimeuptime

IP_LISTtest
#!/bin/bash
# : uptime.sh
# :
IP_LIST="192.168.0.1 192.168.0.5 192.168.0.9"
USER="test"
for IP in $IP_LIST;
do
utime=$(ssh ${USER}@${IP} uptime
echo $IP uptime: $utime
done

| awk '{ print $3 }' )

7.5 SSH

219

$ ./uptime.sh
192.168.0.1 uptime: 1:50,
192.168.0.5 uptime: 2:15,
192.168.0.9 uptime: 10:15,

2
7.5.3
ssh

1. SSH
SSHssh
-C
$ ssh -C user@hostname COMMANDS

2. shellstdin
shellstdin
$ echo 'text' | ssh user@remote_host 'echo'
text

#
$ ssh user@remote_host 'echo'

< file

echostdin

shellstdin
3.
GUI
ssh shellX
$DISPLAY
ssh user@host "export DISPLAY=:0 ; command1; command2"""

SSHX11
forwarding option
ssh -X user@host "command1; command2"

220

7.5.4
7.8

7.6

FTPSFTPRSYNCSCP

7.6.1
LinuxFTP
lftpSSHsftpRSYNCSSHrsync scp
SSH

7.6.2
File Transfer ProtocolFTP
lftpFTP
FTPFTPFTP21
FTP
$ lftp username@ftphost

lftp username@ftphost:~>

cd directory
lcd
mkdir
Is
get filename
lftp username@ftphost:~> get filename

put filename
lftp username@ftphost:~> put filename

7.6

221

quitlftp

lftp

7.6.3

1. FTP
ftpFTPlftplftpftp

FTP
#!/bin/bash
# : ftp.sh
# FTP
HOST='domain.com'
USER='foo'
PASSWD='password'
ftp -i -n $HOST <<EOF
user ${USER} ${PASSWD}
binary
cd /home/slynux
puttestfile.jpg
getserverfile.jpg
quit
EOF

<<EOF
DATA
EOF

stdin FTP1.6stdin

ftp -iuser ${USER} ${PASSWD}


binary
2. SFTPSecure FTPFTP
SFTPFTPSSHFTP
FTPOpenSSHSFTP

222

FTP

cd /home/slynux
put testfile.jpg
get serverfile.jpg

sftp
$ sftp user@domainname

lftpquitsftp
SSH22sftp
-oPort=PORTNO
$ sftp -oPort=422 user@slynux.org

-oPortsftp

3. rsync
rsync6.8
rsync
4. SCPSecure Copy Program
rcpSCPSSH

$ scp filename user@remotehost:/home/path

SSH7.8SSH
scpSSHscp

remotehostIPscp
$ scp SOURCE DESTINATION

SOURCEDESTINATIONusername@localhost:/path
$ scp user@remotehost:/home/path/filename filename

SSH22 -oPortsftp

7.7

223

5. SCP

scp -r
$ scp -r /home/slynux user@remotehost:/home/backups
# /home/slynux

scp -p

7.6.4
1.6EOF

7.7

LAN
WEPESSIDExtended Service Set Identification

ESSID

7.7.1

ifconfigIP
iwconfigiwlist

7.7.2

WEPWried Equivalent PrivacyLAN


#!/bin/bash
#: wlan_connect.sh
#: LAN
#
######### PARAMETERS ###########
IFACE=wlan0
IP_ADDR=192.168.1.5
SUBNET_MASK=255.255.255.0
GW=192.168.1.1
HW_ADDR='00:1c:bf:87:25:d2'
#
ESSID="homenet"
WEP_KEY=8b140b20e7
FREQ=2.462G
#################################

224

KEY_PART=""
if [[ -n $WEP_KEY ]];
then
KEY_PART="key $WEP_KEY"
fi
#
/sbin/ifconfig $IFACE down
if [ $UID -ne 0 ];
then
echo "Run as root"
exit 1;
fi
if [[ -n $HW_ADDR ]];
then
/sbin/ifconfig $IFACE hw ether $HW_ADDR
echo Spoofed MAC ADDRESS to $HW_ADDR
fi
/sbin/iwconfig $IFACE essid $ESSID $KEY_PART freq $FREQ
/sbin/ifconfig $IFACE $IP_ADDR netmask $SUBNET_MASK
route add default gw $GW $IFACE
echo Successfully configured $IFACE

7.7.3
ifconfigiwconfigrouteroot
root
LANessidessid
WEP WEP510
iwconfig
iwlist
# iwlist scan
wlan0
Scan completed :
Cell 01 - Address: 00:12:17:7B:1C:65
Channel:11
Frequency:2.462 GHz (Channel 11)
Quality=33/70 Signal level=-77 dBm

7.8 SSH

225

Encryption key:on
ESSID:"model-2"

Frequency:2.462 GHz (Channel 11)


WEPWEP
Wi-Fi2WPA2

7.7.4

1.17

7.8

SSH

SSHSSH

SSHSSH
SSH

7.8.1
SSH
ssh-keygen
~/.ssh/authorized_keys
~/.sshSSH
arthorized_keys /etc/ssh/sshd_config

7.8.2

SSH
(1) SSH
(2) ~/.ssh/authorized_keys

ssh-keygenSSHRSA
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/slynux/.ssh/id_rsa):
Created directory '/home/slynux/.ssh'.

226

Enter passphrase (empty for no passphrase):


Enter same passphrase again:
Your identification has been saved in /home/slynux/.ssh/id_rsa.
Your public key has been saved in /home/slynux/.ssh/id_rsa.pub.
The key fingerprint is:
f7:17:c6:4d:c9:ee:17:00:af:0f:b3:27:a6:9c:0a:05 slynux@slynux-laptop
The key's randomart image is:
+--[ RSA 2048]----+
|
.
|
|
o . .|
|
E
o o.|
|
...oo |
|
.S .+ +o.|
|
. . .=....|
|
.+.o...|
|
. . + o. .|
|
..+
|
+-----------------+

ssh-keygen
~/.ssh/id_rsa.pub ~/.ssh/id_rsaid_dsa.pubid_dsa
~/.ssh/authorized_keys

$ ssh USER@REMOTE_HOST "cat >> ~/.ssh/authorized_keys" < ~/.ssh/id_rsa.pub


Password:

SSH

$ ssh USER@REMOTE_HOST uname


Linux

Linuxssh-copy-id
authorized_keys
ssh-copy-id USER@REMOTE_HOST

7.9 SSH

7.9

227

SSH

IP192.168.1.28000
www.kernel.org80http://192.168.
1.2:8000Linux

7.9.1

shell
shellshell
(1) 8000www.kernel.org80

ssh -L 8000:www.kernel.org:80 user@localhost

user
(2) 8000www.kernel.org80

ssh -L 8000:www.kernel.org:80 user@REMOTE_MACHINE

REMOTE_MACHINEIPuser
SSH

6
7.9.2

1.

shell
ssh
ssh -fL 8000:www.kernel.org:80 user@localhost -N

-fssh-L-Nssh

2.
SSH

228

SSH

ssh -R 8000:localhost:80 user@REMOTE_MACHINE

800080
REMOTE_MACHINEIP
http://localhost
8000Web

7.10

SSHsshfssshfs

7.10.1
GNU/LinuxsshfssshfsFUSE

FUSEhttp://fuse.sourceforge.net/

7.10.2

# sshfs -o allow_other user@remotehost:/home/path /mnt/mountpoint


Password:

/home/path
/mnt/mountpoint

# umount /mnt/mountpoint

7.10.3
7.5ssh

7.11

229

7.11

Rootkit
Rootkit

7.11.1
lsofnetstat
GNU/Linux

7.11.2

8
lsof
laptop.local:41197->192.168.0.2:3128

laptop.local:41197192.168.0.2:312841197
3128

230

$ lsof -i | grep ":[0-9]\+->" -o | grep "[0-9]\+" -o

| sort | uniq

7.11.3
grep :[0-9]\+-> lsof
:34395->grep

7.11.4

netstat
netstatnetstat

netstat -tnp
$ netstat -tnp
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address
Foreign Address
PID/Program name
tcp
0
0 192.168.0.82:38163
192.168.0.2:3128
ESTABLISHED 2261/firefox-bin
tcp
0
0 192.168.0.82:38164
192.168.0.2:3128
WAIT
tcp
0
0 192.168.0.82:40414
193.107.206.24:422
ESTABLISHED 3836/ssh
tcp
0
0 127.0.0.1:42486
127.0.0.1:32955
ESTABLISHED 4022/GoogleTalkPlug
tcp
0
0 192.168.0.82:38152
192.168.0.2:3128
ESTABLISHED 2261/firefox-bin
tcp6
0
0 ::1:22
::1:39263
ESTABLISHED tcp6
0
0 ::1:39263
::1:22
ESTABLISHED 3570/ssh

7.12

State

TIME_

shellftpssh

7.13

231

7.12.1
TCP/IP
netcatnc

7.12.2
(1)

nc -l 1234

1234
(2)

nc HOST 1234

HOST
localhostIP
(3) 2
1

7.12.3

netcatshell

(1)
nc -l 1234 > destination_filename

(2)
nc HOST 1234 < source_filename

7.13

232

Linuxiptables

7.13.1
iptablesNetwork Address TranslationNAT
iwconfig

7.13.2
(1) eth0

(2) adhoc
IP10.99.66.55
255.255.0.016

(3) shell
#!/bin/bash
#: netsharing.sh
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -A FORWARD -i $1 -o $2 -s 10.99.0.0/16 -m conntrack
--ctstate NEW -j ACCEPT
iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A POSTROUTING -t nat -j MASQUERADE

(4)
./netsharing.sh eth0 wlan0

eth0wlan0
(5)

IP10.99.66.56
255.255.0.0

DHCPDNS
IPdnsmasqDHCPDNS

7.14 iptables

7.14

233

iptables

Linuxiptables

7.14.1
Linuxiptablesiptables

(1) IP
#iptables -A OUTPUT -d 8.8.8.8 -j DROP

iptablesPING 8.8.8.8

PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.


64 bytes from 8.8.8.8: icmp_req=1 ttl=56 time=221 ms
64 bytes from 8.8.8.8: icmp_req=2 ttl=56 time=221 ms
ping: sendmsg: Operation not permitted
ping: sendmsg: Operation not permitted

pingiptables
8.8.8.8

(2)
#iptables -A OUTPUT -p tcp -dport 21 -j DROP
$ ftp ftp.kde.org
ftp: connect: Connection timed out

7.14.2
iptablesLinuxiptables-A

chain
OUTPUToutgoing
-d-jiptablesDROP

234

-pTCP-dport
FTP

7.14.3
iptablesiptables

#iptables --flush

8.2

235

syslog

8.1

1CPU10
watch

logrotate

logging

8.2

shell

236

8.2.1
dfduLinuxdfdisk freedudisk

usage

8.2.2

$ du

FILENAME1 FILENAME2 ..

$ du file.txt
4

$ du -a DIRECTORY

-a

du DIRECTORY

-a

$
4
4
4
16

du -a test
test/output.txt
test/process_log.sh
test/pcpu.sh
test

du DIRECTORY
$ du test
16 test

8.2

237

8.2.3

du
1. KBMBblock
duKBMBGB
-h

du -h FILENAME

$ du -h test/pcpu.sh
4.0K test/pcpu.sh
#

4
# du -h DIRECTORY
$ du -h hack/
16K hack/

2.

du -c

$ du -c FILENAME1 FILENAME2..

du
4
4
8

-c process_log.shpcpu.sh
process_log.sh
pcpu.sh
total

$ du

-c DIRECTORY

$ du -c test/
16 test/
16 total

238

$ du -c *.txt
#

-c -a-h-c
-c

-ssummarize -h

$ du -s FILES(s)
$ du -sh DIRECTORY

$ du -sh slynux
680K slynux

3.
du

$ du -b FILE(s)

KB
$ du -k FILE(s)

MB
$ du -m FILE(s)


$ du -B BLOCK_SIZE FILE(s)

BLOCK_SIZE

$ du pcpu.sh
4 pcpu.sh
$ du -b pcpu.sh
439
pcpu.sh
$ du -k pcpu.sh
4 pcpu.sh
$ du -m pcpu.sh
1 pcpu.sh
$ du -B 4 pcpu.sh
1024 pcpu.sh

8.2

239

4.

(1)

$ du --exclude "WILDCARD" DIRECTORY

$ du --exclude "*.txt" FILES(s)


# .txt

(2)

$ du --exclude-from EXCLUDE.txt DIRECTORY


# EXCLUDE.txt

--max-depthdu1
2

$ du --max-depth 2 DIRECTORY

du
-xdu
du DIRECTORY
/mnt/sda1/mnt
/dev/sda1du
dudu -xdu -x /
/mnt/

5. 10
du
sort

240

$ du -ak SOURCE_DIR | sort -nrk 1 | head

-aduSOURCE_DIR-k
KB
sorthead10
$ du -ak /home/slynux | sort -nrk 1 | head -n 4
50220 /home/slynux
43296 /home/slynux/.mozilla
43284 /home/slynux/.mozilla/firefox
43276 /home/slynux/.mozilla/firefox/8c22khxc.default

$ find . -type f -exec du -k {} \; | sort -nrk 1 | head

finddudu
6.
dudfdf-h

$ df -h
Filesystem
/dev/sda1
none
none
none
none
none
none

8.3

Size
9.2G
497M
502M
502M
502M
502M
9.2G

Used Avail Use% Mounted on


2.2G 6.6G 25% /
240K 497M
1% /dev
168K 501M
1% /dev/shm
88K 501M
1% /var/run
0 502M
0% /var/lock
0 502M
0% /lib/init/rw
2.2G 6.6G 25% /var/lib/ureadahead/debugfs

8.3.1
(1) time
$ time COMMAND

8.3

241

COMMAND time
stderr

$ time ls
test.txt
next.txt
real
0m0.008s
user
0m0.001s
sys
0m0.003s

realusersys
time /usr/bin/time

shelltimetimeshell
shell time
time
/usr/bin/time

(2) -o filename
$ /usr/bin/time -o output.txt COMMAND

-o
-a-o
$ /usr/bin/time -a -o output.txt COMMAND

(3) -f
% realuser sys

real: %e

user: %U
sys: %S

$ /usr/bin/time -f "FORMAT STRING" COMMAND

$ /usr/bin/time -f "Time: %U" -a -o timing.log uname


Linux

242

%Uuser

>2>

$ /usr/bin/time -f "Time: %U" uname> command_output.txt 2>time.log


$ cat time.log
Time: 0.00
$ cat command_output.txt
Linux

(4) %Z
$ /usr/bin/time -f "Page size: %Z bytes" ls> /dev/null
Page size: 4096 bytes

ls
/dev/null
man time

8.3.2

Realwall clock time

time slice
I/O

UserCPU

SysCPU
library codeuser
CPU
time

8-1

8.4

243

8-1

%C

%D

KB

%E

real[:]:

%x

%k

%W

%Z

%P

CPUuser+system

%K

data+stack+textKB

%w

I/O

%c

8.4

8.4.1
whowusersuptimelastlastb

6
8.4.2
(1)
$ who
slynux
slynux

pts/0
tty7

2010-09-29 05:24 (slynuxs-macbook-pro.local)


2010-09-29 07:08 (:0)

TTY
X

TTYTeleTYperwriter
/dev/
/dev/pts/3tty

244

(2)
$ w
07:09:05 up 1:45, 2 users, load average:
USER
TTY
FROM
LOGIN@
IDLE JCPU
slynux
pts/0
slynuxs 05:24 0.00s 0.65s
slynux
tty7
:0
07:08 1:45m 3.28s

0.12, 0.06, 0.02


PCPU WHAT
0.11s sshd: slynux
0.26s gnome-session

15
15
TTY
CPUCPU
uptimeload average

9
(3)
$ users
slynux slynux slynux hacker

slynux3sortuniq

$ users | tr ' ' '\n' | sort | uniq


slynux
hacker

tr' ''\n'sortuniq
(4)
$ uptime
21:44:33 up

3:17,

8 users,

load average: 0.09, 0.14, 0.09

up

$ uptime | grep -Po '\d{2}\:\d{2}\:\d{2}'

grepperl3
(5)
$ last
slynux

tty7

:0

Tue Sep 28 18:27

still logged in

8.5 1 CPU 10
reboot
slynux

system boot
pts/0

245

2.6.32-21-generic Tue Sep 28 18:10 - 21:46 (03:35)


:0.0
Tue Sep 28 05:31 - crash (12:39)

lasttty

last/var/log/wtmp-f

$ last -f /var/log/wtmp

(6)

$ last USER

(7) reboot session


$ last reboot
reboot
system boot
reboot
system boot

2.6.32-21-generi Tue Sep 28 18:10 - 21:48


2.6.32-21-generi Tue Sep 28 05:14 - 21:48

(03:37)
(16:33)

(00:00)
(00:00)

(8)
# lastb
test
slynux

tty8
tty8

:0
:0

Wed Dec 15 03:56 - 03:56


Wed Dec 15 03:55 - 03:55

rootlastb

8.5

1 CPU 10

CPUCPU
CPUCPUCPU

8.5.1
psCPU
CPUps
CPU10ps9

8.5.2
CPUshell

246

#!/bin/bash
#: pcpu_usage.sh
#:1CPU
SECS=3600
UNIT_TIME=60
#SECS
#UNIT_TIME
STEPS=$(( $SECS / $UNIT_TIME ))
echo Watching CPU usage... ;
for((i=0;i<STEPS;i++))
do
ps -eocomm,pcpu | tail -n +2 >> /tmp/cpu_usage.$$
sleep $UNIT_TIME
done
echo
echo CPU eaters :
cat /tmp/cpu_usage.$$ | \
awk '
{ process[$1]+=$2; }
END{
for(i in process)
{
printf("%-20s %s\n",i, process[i]) ;
}
}' | sort -nrk 2 | head
rm /tmp/cpu_usage.$$
#

$ ./pcpu_usage.sh
Watching CPU usage...
CPU eaters :
Xorg
20
firefox-bin
15
bash
3
evince
2
pulseaudio
1.0
pcpu.sh
0.3
wpa_supplicant 0

8.6 watch
wnck-applet
watchdog/0
usb-storage

247

0
0
0

8.5.3
ps -eo comm,pcpucommcommand name

pcpuCPUCUP usage in percent


CPU
CPU60
forps -eo comm,pcpu | tail -n +2CPU
/tmp/cpu_usage.$$ 60sleep 60
pstail -n +2 ps COMMAND %CPU
cpu_usage.$$ $$ ID ID1345
/tmp/cpu_usage.1345 /tmp

160
awkCPUCPU
CPUhead10

8.5.4

4.6awk
3.13tail

8.6

watch

watchdu

8.6.1
watch

$ watch COMMAND

$ watch ls

248

$ watch 'COMMANDS'

$ watch 'ls -l | grep "^d"'


#

2
-n SECOND
$ watch -n 5 'ls -l'
#5ls -l

8.6.2
watch
watch
watch

d
$ watch -d 'COMMANDS'

8.7

8.7.1
inotifywaitLinux

inotify-toolsinotifyLinux
GNU/Linuxinotify

8.7.2
shell
#/bin/bash
#: watchdir.sh

8.8 logrotate

249

#:
path=$1
#

inotifywait -m -r -e create,move,delete $path

-q

$ ./watchdir.sh .
./ CREATE new
./ MOVED_FROM new
./ MOVED_TO news
./ DELETE news

8.7.3
-m
-r-e
-q

8-2

8.8

8-2

access

modify

attrib

move

create

open

close

delete

logrotate

Linux

rotation
strip

250

8.8.1
logrotateLinux

SIZE
logrotate
100KBSIZE = 100k
logfile_name.1logfile_name.1SIZE
logrotatelogfile_name.2
logrotate logrotate
logfile_name.1. gzlogfile_name.2.gz logrotate

8.8.2
logrotate/etc/logrotate.d

/var/log/program.log
$ cat /etc/logrotate.d/program
/var/log/program.log {
missingok
notifempty
size 30k
compress
weekly
rotate 5
create 0600 root root
}

/var/log/program.log

8.8.3
8.3

8.9 syslog

251

8-3

missingok

notifempty

size 30k

1M1MB

compress

gzip

weekly

weeklyyearlydaily

rotate 5

5
program.log.1.gzprogram.log.2.gzprogram.log.5.gz

create 0600 root root

8-3logrotat
logrotatehttp://linux.die.net/man/8/logrotate

8.9

syslog

/var/log
Linux
sylogdsyslog /var/log
syslogsyslogd

8.9.1

loggersyslogd
Linux8-4

8-4

/var/log/boot.log

/var/log/httpd

Apache Web

/var/log/messages

/var/log/auth.log

/var/log/dmesg

/var/log/mail.log

/var/log/Xorg.0.log

252

8.9.2
logger
(1) /var/log/message
$ logger LOG_MESSAGE

$ logger This is a test log line


$ tail -n 1 /var/log/messages
Sep 29 07:47:44 slynux-laptop slynux: This is a test log line

/var/log/messageslogger
/var/log/messages
(2) tag
$ logger -t TAG This is a message
$ tail -n 1 /var/log/messages
Sep 29 07:48:42 slynux-laptop TAG: This is a message

Syslog/var/loglogger

syslogdTAG
/etc/rsyslog.d/

(3)
$ logger -f /var/log/source.log

8.9.3
3.13headtail

8.10

SSHInternet
shell

8.10

253

IP
IP

8.10.1
shellSSH
/var/log/auth.log

hostIP

8.10.2

#!/bin/bash
#:intruder_detect.sh
#:auth.log
AUTHLOG=/var/log/auth.log
if [[ -n $1 ]];
then
AUTHLOG=$1
echo Using Log file : $AUTHLOG
fi
LOG=/tmp/valid.$$.log
grep -v "invalid" $AUTHLOG > $LOG
users=$(grep "Failed password" $LOG | awk '{ print $(NF-5) }' | sort | uniq)

printf "%-5s|%-10s|%-10s|%-13s|%-33s|%s\n" "Sr#" "User" "Attempts" "IP address"


"Host_Mapping" "Time range"

ucount=0;

ip_list="$(egrep -o "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" $LOG | sort | uniq)"


for ip in $ip_list;
do
grep $ip $LOG > /tmp/temp.$$.log

254

for user in $users;


do
grep $user /tmp/temp.$$.log> /tmp/$$.log
cut -c-16 /tmp/$$.log > $$.time
tstart=$(head -1 $$.time);
start=$(date -d "$tstart" "+%s");
tend=$(tail -1 $$.time);
end=$(date -d "$tend" "+%s")
limit=$(( $end - $start ))
if [ $limit -gt 120 ];
then
let ucount++;

IP=$(egrep -o "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+" /tmp/$$.log | head -1 );


TIME_RANGE="$tstart-->$tend"
ATTEMPTS=$(cat /tmp/$$.log|wc -l);
HOST=$(host $IP | awk '{ print $NF }' )

printf "%-5s|%-10s|%-10s|%-10s|%-33s|%-s\n" "$ucount" "$user" "$ATTEMPTS" "$IP"


"$HOST" "$TIME_RANGE";
fi
done
done

rm/tmp/valid.$$.log/tmp/$$.log $$.time/tmp/temp.$$.log 2>/dev/null

8.10.3
intruder_detect.shauth.log

8.11

255

/var/log/auth.log
Failed password for invalid user bob from
203.83. 248.32 port 7016 ssh2auth.log
invalidgrep-v
SSH
sshd[21197]: Failed password for bob1 from 203.83.248.32 port 50035 ssh2
failed password
IPIPIP
IPegrepforIPgrep
6bob1awk
6NFNF -56
sortuniq
for
16cut

head -1
tail -1tstarttends
dateUnix1.10
Unix
startend
2120

IPegrep
wcIPIPhost

bug

8.11

IP

256

80%ALERTSAFE

8.11.1

SSH

8.11.2
disklog
SSH7.8
testshell
#!/bin/bash
#: disklog.sh
#:

logfile="diskusage.log"
if [[ -n $1 ]]
then
logfile=$1
fi
if [ ! -e $logfile ]
then
printf "%-8s %-14s %-9s %-8s %-6s %-6s %-6s %s\n" "Date" "IP address" "Device"
"Capacity" "Used" "Free" "Percent" "Status" > $logfile
fi
IP_LIST="127.0.0.1 0.0.0.0"
#IP
(
for ip in $IP_LIST;
do
#slynux
ssh slynux@$ip 'df -H' | grep ^/dev/ > /tmp/$$.df
while read line;

8.11

257

do
cur_date=$(date +%D)
printf "%-8s %-14s " $cur_date $ip
echo $line | awk '{ printf("%-9s %-8s %-6s %-6s %-8s",$1,$2,$3,$4,$5); }'
pusg=$(echo $line | egrep -o "[0-9]+%")
pusg=${pusg/\%/};
if [ $pusg -lt 80 ];
then
echo SAFE
else
echo ALERT
fi

done< /tmp/$$.df
done
) >> $logfile

croncrontab
10

00 10 * * * /home/path/disklog.sh /home/user/diskusg.log

crontab -e

$ ./disklog.sh

8.11.3
disklog.sh
-e logfile
IP IP_LIST
IP_LISTtestSSHfor
IPsshdf -H
whileawk
egrep%
8080SAFT80ALERT

258

shell()

8.11.4
9.7crontab

8.12

Web

8.12.1
last/var/log/wtmp

8.12.2

#!/bin/bash
#: active_users.sh
#:
log=/var/log/wtmp
if [[ -n $1 ]];
then
log=$1
fi
printf "%-4s %-10s %-10s %-6s %-8s\n" "Rank" "User" "Start" "Logins" "Usage hours"
last -f $log | head -n -2
cat /tmp/ulog.$$ |
(
while read user;
do

> /tmp/ulog.$$

cut -d' ' -f1 | sort | uniq> /tmp/users.$$

8.12

259

grep ^$user /tmp/ulog.$$ > /tmp/user.$$


minutes=0
while read t
do
s=$(echo $t | awk -F: '{ print ($1 * 60) + $2 }')
let minutes=minutes+s
done< <(cat /tmp/user.$$ | awk '{ print $NF }' | tr -d ')(')

firstlog=$(tail -n 1 /tmp/user.$$ | awk '{ print $5,$6 }')


nlogins=$(cat /tmp/user.$$ | wc -l)
hours=$(echo "$minutes / 60.0" | bc)
printf "%-10s %-10s %-6s %-8s\n"
done< /tmp/users.$$

$user "$firstlog" $nlogins $hours

) | sort -nrk 4 | awk '{ printf("%-4s %s\n", NR, $0) }'


rm /tmp/users.$$ /tmp/user.$$ /tmp/ulog.$$

$ ./active_users.sh
Rank User
Start
1
easyibaa
Dec 11
2
demoproj
Dec 10
3
kjayaram
Dec 9
4
cinenews
Dec 11
5
thebenga
Dec 10
6
gateway2
Dec 11
7
soft132
Dec 12
8
sarathla
Nov 1
9
gtsminis
Dec 11
10
agentcde
Dec 13

Logins
531
350
213
85
54
52
49
45
41
39

Usage hours
349
230
55
139
35
34
25
29
26
32

8.12.3
active_users.sh
wtmplast -fcut
sortuniqgrep

awk

awktr -d<( COMMANDS )


while
dateseconds

260

sort-nr
-k4
sortawkawk

8.13

Linux
powertop

8.13.1
powertopLinux

8.13.2
powertop
(1) powertop
# powertop

powertop
8-1

8-1

8.14

261

(2) HTML

# powertop --html

powertop PowerTOP.html
HTMLWeb

(3)
powertopTunables
powertop
BadGood8-2

8-2

powertop

8.14

topI/O
iotop

8.14.1
iotopLinux

262

8.14.2
iotopI/O
(1)
# iotop -o

iotop-oI/O

(2) shell
# iotop -b -n 2

iotopshell

(3)
# iotop -p PID

PIDiotop
LinuxPID
iotoppidof
# iotop p 'pidof cp'

8.15

8.15.1
fsckLinux

8.15.2
fsck
(1) fsck
# fsck /dev/sdb3
fsck from util-linux 2.20.1

8.15

263

e2fsck 1.42.5 (29-Jul-2012)


HDD2 has been mounted 26 times without being checked, check forced.
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
HDD2: 75540/16138240 files (0.7% non-contiguous), 48756390/64529088 blocks

(2) /etc/fstab
# fsck -A

/etc/fstabfstab

(3) fsck

# fsck -a /dev/sda2

(4) fsck
# fsck -AN
fsck from util-linux
[/sbin/fsck.ext4 (1)
[/sbin/fsck.ext4 (1)
[/sbin/fsck.ext3 (1)

2.20.1
-- /] fsck.ext4 /dev/sda8
-- /home] fsck.ext4 /dev/sda7
-- /media/Data] fsck.ext3 /dev/sda6

fsck

8.15.3
fsckfsck

fsckfsck.fstypefstype
ext4fsckfsck.ext4

fsck
fsck.ext4
fsckfsck
fsckfsck
fsck

264

BashMySQL

/proc

cron

9.1

GNU/Linux

9.2

running instance

IDPIDPID
CPU

9.2.1
toppspgrep

9.2

265

9.2.2
ps

PIDTTYCPU

$ ps
PID TTY
1220 pts/0
1242 pts/0

TIME CMD
00:00:00 bash
00:00:00 ps

pspsTTY

IDPIDTTY
CMD

-ffull
$ ps -f
UID
slynux
slynux

PID
1220
1587

PPID
1219
1220

C STIME TTY
0 18:18 pts/0
0 18:59 pts/0

TIME CMD
00:00:00 -bash
00:00:00 ps -f

ps
-eevery-axall
-x-apsTTYps

ps eps efps -axps axf


$ ps -e | head
PID TTY
TIME CMD
1 ?
00:00:00 init
2 ?
00:00:00 kthreadd
3 ?
00:00:00 migration/0
4 ?
00:00:00 ksoftirqd/0
5 ?
00:00:00 watchdog/0
6 ?
00:00:00 events/0
7 ?
00:00:00 cpuset
8 ?
00:00:00 khelper
9 ?
00:00:00 netns

head10
psPIDps

266

-o
-o

ps
$ ps [OTHER OPTIONS] -o parameter1,parameter2,parameter3 ..

-o,

-o-e
-oe-o
-e-e

commCOMMANDpcpuCPU
$ ps -eo comm,pcpu | head
COMMAND
%CPU
init
0.0
kthreadd
0.0
migration/0
0.0
ksoftirqd/0
0.0
watchdog/0
0.0
events/0
0.0
cpuset
0.0
khelper
0.0
netns
0.0

9.2.3
-o9-1
9-1

pcpu

CPU

pid

ID

ppid

ID

pmem

comm

9.2

267

cmd

user

nice

time

CPU

etime

tty

TTY

euid

ID

stat

9.2.4

1. top
topCPU

$ top

CPU9-1

9-1

shell
shell||,&&,&,
;,;;,|,|&,(,)

268

2. ps
--sortps+-

$ ps [OPTIONS] --sort -paramter1,+parameter2,parameter3..

CPU10
$ ps -eo comm,pcpu --sort -pcpu | head
COMMAND
%CPU
Xorg
0.1
hald-addon-stor 0.0
ata/0
0.0
scsi_eh_0
0.0
gnome-settings- 0.0
init
0.0
hald
0.0
pulseaudio
0.0
gdm-simple-gree 0.0

CPUhead10
greppsBash

$ ps -eo comm,pid,pcpu,pmem | grep bash


bash
1255 0.0 0.3
bash
1680 5.5 0.3

3. ID
PIDps
pgrepps
$ ps -C COMMAND_NAME

$ ps -C COMMAND_NAME -o pid=

-opid =
ps PID=
$ ps -C bash -o pid=
1255
1680

9.2

269

BashID

pgrepID
$ pgrep COMMAND
$ pgrep bash
1255
1680

pgrep Bash pgrep


ashpgrep basps

3
pgrep

$ pgrep COMMAND -d DELIMITER_STRING


$ pgrep bash -d ":"
1255:1680

$ pgrep -u root,slynux COMMAND

rootslynux

$ pgrep -c COMMAND

4. IDIDps
ps/IDps

-u EUSER1,EUSER2
-U RUSER1,RUSER2

$ ps -u root -U root -o user,pcpu

root IDIDCPU

-o-e-eo
-o

270

5. TTYps
TTYps -tTTY
$ ps -t TTY1, TTY2 ..

$ ps -t pts/0,pts/1
PID TTY
TIME
1238 pts/0
00:00:00
1835 pts/1
00:00:00
1864 pts/0
00:00:00

CMD
bash
bash
ps

6.
ps L ps
NLWPNLPNLWPNLPps
ID
$ ps -eLf

$ ps -eLf --sort -nlwp | head


UID
PID PPID
LWP C NLWP STIME TTY
root
647
1
647 0
64 14:39 ?
console-kit-daemon --no-daemon
root
647
1
654 0
64 14:39 ?
console-kit-daemon --no-daemon
root
647
1
656 0
64 14:39 ?
console-kit-daemon --no-daemon
root
647
1
657 0
64 14:39 ?
console-kit-daemon --no-daemon
root
647
1
658 0
64 14:39 ?
console-kit-daemon --no-daemon
root
647
1
659 0
64 14:39 ?
console-kit-daemon --no-daemon
root
647
1
660 0
64 14:39 ?
console-kit-daemon --no-daemon
root
647
1
662 0
64 14:39 ?
console-kit-daemon --no-daemon
root
647
1
663 0
64 14:39 ?
console-kit-daemon --no-daemon

TIME CMD
00:00:00 /usr/sbin/
00:00:00 /usr/sbin/
00:00:00 /usr/sbin/
00:00:00 /usr/sbin/
00:00:00 /usr/sbin/
00:00:00 /usr/sbin/
00:00:00 /usr/sbin/
00:00:00 /usr/sbin/
00:00:00 /usr/sbin/

10
7.
-ops

9.2

271

:
-f

ps ef

ps

-e u

ps

ps

-e ww

8.

ps

$ ps -eo cmd e

$ ps -eo pid,cmd e | tail -n 3


1162 hald-addon-acpi: listening on acpid socket /var/run/acpid.socket
1172 sshd: slynux [priv]
1237 sshd: slynux@pts/0
1238 -bash USER=slynux LOGNAME=slynux HOME=/home/slynux
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
MAIL=/var/mail/slynux SHELL=/bin/bash SSH_CLIENT=10.211.55.2 49277 22
SSH_CONNECTION=10.211.55.2 49277 10.211.55.4 22 SSH_TTY=/dev/pts/0 TERM=xterm-color
LANG=en_IN XDG_SESSION_COOKIE=d1e96f5cc8a7a3bc3a0a73e44c95121a-1286499339.
592429-1573657095

apt-getHTTP
Internethttp_proxy=host:port
apt-get

crontab
GUI
crontab

00 10 * * * /usr/bin/windowapp

DISPLAY
windowappps -C windowapp -eo cmd e
crontab

00 10 * * * DISPLAY=:0 /usr/bin/windowapp

DISPLAY=:0 ps

272

9. whichwhereisfilewhatis

which

which

PATH
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

PATH /home/slynux/bin
PATH
$ export PATH=$PATH:/home/slynux/bin
# /home/slynux/binPATH

which
$ which ls
/bin/ls

whereis

whereiswhich

$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz

file

file
$ file FILENAME

$ file /bin/ls
/bin/ls: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked
(uses shared libs), for GNU/Linux 2.6.15, stripped

whatis

whatis

9.3
$ whatis ls
ls (1)

273

- list directory contents

apropos

apropos COMMAND

15
15
uptime
$ uptime
12:40:53 up

6:16,

2 users,

load average: 0.00, 0.00, 0.00

9.2.5

9.7

9.3

Unix

9.3.1
Linux
signal
handlershellkill
Ctrl+CCtrl+Zkilltrap

274

9.3.2
(1)
$ kill -l

signal number
(2)
$ kill PROCESS_ID_LIST

killTERMIDID

(3) kill
$ kill -s SIGNAL PID

SIGNAL

SIGHUP 1hangup detection

SIGINT 2Ctrl + C
SIGKILL 9
SIGTERM 15
SIGTSTP 20Ctrl + Z

(4)
$ kill -s SIGKILL PROCESS_ID

$ kill -9 PROCESS_ID

9.3.3

1. kill
killIDkill

killall
$ killall process_name

9.3

275

$ killall -s SIGNAL process_name

$ killall -9 process_name

$ killall -9 gedit

$ killall -u USERNAME process_name

killall-i
pkillkillpkillID
$ pkill process_name
$ pkill -s SIGNAL process_name

SIGNALpkillpkillkill

pkill
2.

traptrap

trap 'signal_handler_function_name' SIGNAL LIST

SIGNAL LIST

SIGINTshell

#/bin/bash
#sighandle.sh
#:
function handler()
{
echo Hey, received signal : SIGINT
}
echo My process ID is $$
#$$/ID
trap 'handler' SIGINT

276

#handlerSIGINT
while true;
do
sleep 1
done

Ctrl+C
Ctrl+CSIGINT
while
event loop
killID
$ kill -s SIGINT PROCESS_ID

PROCESS_IDpsID

Ctrl+C

trapkill -l

9.4

9.4.1
wall

Linux
/dev/pts/

9.4.2

9.4

277

$ cat message | wall

$ wall< message
Broadcast Message from slynux@slynux-laptop
(/dev/pts/1) at 12:54 ...

This is a message

message outline

root

$ mesg y

$ mesg n

#/bin/bash
#message_user.sh
#:
USERNAME=$1
devices=`ls /dev/pts/* -l | awk '{ print $3,$10 }' | grep $USERNAME | awk '{ print $2 }'`
for dev in $devices;
do
cat /dev/stdin > $dev
done

./message_user.sh USERNAME < message.txt


#stdinUSERNAME

$ cat message.txt
A message to slynux. Happy Hacking!
#./message_user.sh slynux < message.txt
#rootmessage_user.sh

slynux

278

9.4.3
/dev/pts
ls lawkgrep
USERNAME$devices
for/dev/stdin
TTY

9.5

LinuxCPU
Linux

(1)
$ hostname

$ uname -n

(2) Linux
$ uname -a

(3)
$ uname -r

(4)
$ uname -m

(5) CPU
$ cat /proc/cpuinfo

$ cat /proc/cpuinfo | sed -n 5p

9.6 proc

279

cpuinfo5

(6)
$ cat /proc/meminfo

$ cat /proc/meminfo | head -1


MemTotal:
1026096 kB

(7)
$ cat /proc/partitions

$ fdisk -l

#root

(8)
$ lshw

9.6

#root

proc

GNU/Linux/procpseudo filesystem

/proc
/proccat

/procID
BashID4295pgrep bash/proc/4295
/proc/PID

environcat /proc/4295/environ

cwdworking directory
exe

280

$ readlink /proc/4295/exe
/bin/bash

fd

9.7

cron

GNU/Linux
croncron
croncron
ISP

Internet
cronInternet

9.7.1
GNU/Linuxcroncron
crontabjobcron

9.7.2
croncron

(1) cron2test.sh
02 * * * * /home/slynux/test.sh

(2) 567
00 5,6,7 * * /home/slynux/test.sh

(3) script.sh
00 */12 * * 0 /home/slynux/script.sh

(4) 2
00 02 * * * /sbin/shutdown -h

9.7 cron

281

(5) croncrontab

-ecron
$ crontab -e
02 02 * * * /home/slynux/script.sh

crontab -evicron
cron
(6) crontab

task.croncron
crontab
$ crontab task.cron

inlinecron

crontab<<EOF
02 * * * * /home/slynux/script.sh
EOF

croncrontab<<EOFEOF

9.7.3
cron6

059
023
131
112

06

5
**cron

510
"5,10"
*/55cron

282

croncron
croncrontab
rootcrontab
croncron
PATH

9.7.4
crontab

1.
cron

HTTPhttp_proxy
crontab<<EOF
http_proxy=http://192.168.0.3:3128
00 * * * * /home/slynux/download.sh
EOF

2.
cron
/etc/rc.d

crontab
@reboot command

root
rootcrontab
3. cron
-lcron
$ crontab -l
02 05 * * * /home/user/disklog.sh

9.8 Bash MySQL

283

crontab -lcron

-ucron
$ crontab -l -u slynux
09 10 * * * /home/slynux/test.sh

-uroot

4. cron
-rcron
$ crontab -r

cron
# crontab -u slynux -r

root

9.8

Bash MySQL

MySQLPHPPythonC++
shellMySQL
CSVComma Separated ValueMySQL
shell
BashMySQL

CSV

7
9.8.1
MySQLmysql-servermysql-clientLinux
MySQLMySQL

9.8.2
sortawkSQL

284

#!/bin/bash
#create_db.sh
#MySQL
USER="user"
PASS="user"
mysql -u $USER -p$PASS <<EOF 2> /dev/null
CREATE DATABASE students;
EOF
[ $? -eq 0 ] && echo Created DB || echo DB already exist
mysql -u $USER -p$PASS students <<EOF 2> /dev/null
CREATE TABLE students(
id int,
name varchar(100),
mark int,
dept varchar(4)
);
EOF
[ $? -eq 0 ] && echo Created table students || echo Table students already exist
mysql -u $USER -p$PASS students <<EOF
DELETE FROM students;
EOF

#!/bin/bash
#write_to_db.sh
#: CSVMySQLdb
USER="user"
PASS="user"
if [ $# -ne 1 ];
then
echo $0 DATAFILE
echo
exit 2
fi
data=$1

9.8 Bash MySQL

285

while read line;


do
oldIFS=$IFS
IFS=,
values=($line)
values[1]="\"`echo ${values[1]} | tr ' ' '#' `\""
values[3]="\"`echo ${values[3]}`\""

query=`echo ${values[@]} | tr ' #' ', ' `


IFS=$oldIFS

mysql -u $USER -p$PASS students <<EOF


INSERT INTO students VALUES($query);
EOF
done< $data
echo Wrote data into DB

#!/bin/bash
#read_db.sh
#:

USER="user"
PASS="user"
depts=`mysql -u $USER -p$PASS students <<EOF | tail -n +2
SELECT DISTINCT dept FROM students;
EOF`
for d in $depts;
do

echo Department : $d
result="`mysql -u $USER -p$PASS students <<EOF
SET @i:=0;
SELECT @i:=@i+1 as rank,name,mark FROM students WHERE dept="$d" ORDER BY mark DESC;
EOF`"

echo "$result"
echo
done

286

CSVstudentdata.csv
1,Navin M,98,CS
2,Kavya N,70,CS
3,Nawaz O,80,CS
4,Hari S,80,EC
5,Alex M,50,EC
6,Neenu J,70,EC
7,Bob A,30,EC
8,Anu M,90,AE
9,Sruthi,89,AE
10,Andrew,89,AE

$ ./create_db.sh
Created DB
Created table students
$ ./write_to_db.sh studentdat.csv
Wrote data into DB
$ ./read_db.sh
Department : CS
rank name mark
1 Navin M 98
2 Nawaz O 80
3 Kavya N 70
Department
rank name
1 Hari S
2 Neenu J
3 Alex M
4 Bob A

: EC
mark
80
70
50
30

Department : AE
rank name mark
1 Anu M
90
2 Sruthi
89
3 Andrew
89

9.8.3
create_db.shstudents
studentsMySQL
USERPASSmysqlMySQL-u

9.9

287

-pPASSWORDmysql
use database_nameSQL
mysqlstdinstdin
<<EOF<<EOFEOFmysql
CREATE DATABASEstderr /dev/null
stderr /dev/null
$?mysql

write_to_db.shCSVwhileCSV

SQLCSV
array=(val1 val2 val3)IFS
IFSIFS=,
id name mark department id mark name
department
nameIFS
name#
\" trname #
#

read_db.sh
while
SET @i:=0SQLSQL constructi=0
i

9.9

GNU/Linux
shellshell

8
9.9.1

#!/bin/bash
#user_adm.sh

288

#:
function usage()
{
echo Usage:
echo Add a new user
echo $0 -adduser username password
echo
echo Remove an existing user
echo $0 -deluser username
echo
echo Set the default shell for the user
echo $0 -shell username SHELL_PATH
echo
echo Suspend a user account
echo $0 -disable username
echo
echo Enable a suspended user account
echo $0 -enable username
echo
echo Set expiry date for user account
echo $0 -expiry DATE
echo
echo Change password for user account
echo $0 -passwd username
echo
echo Create a new user group
echo $0 -newgroup groupname
echo
echo Remove an existing user group
echo $0 -delgroup groupname
echo
echo Add a user to a group
echo $0 -addgroup username groupname
echo
echo Show details about a user
echo $0 -details username
echo
echo Show usage
echo $0 -usage
echo
exit
}
if [ $UID -ne 0 ];
then
echo Run $0 as root.
exit 2

9.9

289

fi

case $1 in
-adduser) [ $# -ne 3 ] && usage ; useradd $2 -p $3 -m ;;
-deluser) [ $# -ne 2 ] && usage ; deluser $2 --remove-all-files;;
-shell)
[ $# -ne 3 ] && usage ; chsh $2 -s $3 ;;
-disable) [ $# -ne 2 ] && usage ; usermod -L $2 ;;
-enable) [ $# -ne 2 ] && usage ; usermod -U $2 ;;
-expiry) [ $# -ne 3 ] && usage ; chage $2 -E $3 ;;
-passwd) [ $# -ne 2 ] && usage ; passwd $2 ;;
-newgroup) [ $# -ne 2 ] && usage ; addgroup $2 ;;
-delgroup) [ $# -ne 2 ] && usage ; delgroup $2 ;;
-addgroup) [ $# -ne 3 ] && usage ; addgroup $2 $3 ;;
-details) [ $# -ne 2 ] && usage ; finger $2 ; chage -l $2 ;;
-usage) usage ;;
*) usage ;;
esac

# ./user_adm.sh -details test


Login: test
Name:
Directory: /home/test
Shell: /bin/sh
Last login Tue Dec 21 00:07 (IST) on pts/1 from localhost
No mail.
No Plan.
Last password change
: Dec 20, 2010
Password expires
: never
Password inactive
: never
Account expires
: Oct 10, 2010
Minimum number of days between password change
: 0
Maximum number of days between password change
: 99999
Number of days of warning before password expires : 7

9.9.2
user_adm.shusage()
-usageusage()
caseuser_adm.sh
-adduser-deluser-shell-disable-enableexpiry-passwd
-newgroup-delgroup-addgroup-details -usage*)
usage()[ $# -ne 3 ] &&
usageusage()
ID0root

290

ID0ID0root,
root

-useradd

useradd
useradd USER -p PASSWORD

-mhome-c FULLNAME
-deluser

deluser
deluser USER

--remove-all-fileshome
-shell

chshshell
chsh USER -s SHELL

-disable-enable

usermodusermod -L USERusermod
-U USER
-expiry

chage
chage -E DATE

-m MIN_DAYSMIN_DAYS

-M MAX_DAYS

-W -WARN_DAYS

-passwd

passwd
passwd USER

9.10

291

-newgroupaddgroup

addgroup
addgroup GROUP

addgroup USER GROUP


-delgroup

delgroup

delgroup GROUP

-details

figer USERhomeshell

chage -l

9.10

JPEGPNG

9.10.1
convertImagemagick
GNU/Linux
Imagemagickwww.imagemagick.org

9.10.2

$ convert INPUT_FILE OUTPUT_FILE

292

$ convert file1.png file2.png

WIDTHHEIGHT
$ convert image.png -resize WIDTHxHEIGHT image.png

$ convert image.png -resize 1024x768 image.png

WIDTHHEIGHT

$ convert image.png -resize WIDTHx image.png

$ convert image.png -resize 1024x image.png

$ convert image.png -resize "50%" image.png

#!/bin/bash
#image_help.sh
#:
if [ $# -ne 4 -a $# -ne 6 -a $# -ne 8 ];
then
echo Incorrect number of arguments
exit 2
fi
while [ $# -ne 0 ];
do
case $1 in
-source) shift; source_dir=$1 ; shift ;;
-scale) shift; scale=$1 ; shift ;;
-percent) shift; percent=$1 ; shift ;;
-dest) shift ; dest_dir=$1 ; shift ;;
-ext) shift ; ext=$1 ; shift ;;
*) echo Wrong parameters; exit 2 ;;
esac;
done

9.10

293

1
for img in `echo $source_dir/*` ;
do
source_file=$img
if [[ -n $ext ]];
then
dest_file=${img%.*}.$ext
else
dest_file=$img
fi
if [[ -n $dest_dir ]];
then
dest_file=${dest_file##*/}
dest_file="$dest_dir/$dest_file"
fi
if [[ -n $scale ]];
then
PARAM="-resize $scale"
elif [[ -n $percent ]];
then
PARAM="-resize $percent%"
fi

echo Processing file : $source_file


convert $source_file $PARAM $dest_file
done

sample_dir20%
$ ./image_help.sh -source sample_dir -percent 20%
Processing file :sample/IMG_4455.JPG
Processing file :sample/IMG_4456.JPG
Processing file :sample/IMG_4457.JPG
Processing file :sample/IMG_4458.JPG

1024
$ ./image_help.sh -source sample_dir -scale 1024x

-ext pngPNG

$ ./image_help.sh -source sample -scale 50% -ext png -dest newdir


# newdir

294

9.10.3
image_help.sh-source-percent-scale
-ext-dest
-source
-percent-scale
-percent-scale
-ext-ext

-dest

468
whilecase$#
shift
$1$2$3$1case
$1Cswitchcase
case ;; percent scale
source_dirextdest_dirfor

ext -ext
source_file.extensionsource_file.$ext-dest

convert-resize widthx -resize perc%


convert

9.10.4
2.12

9.11

GUI
GUI

9.12

295

9.11.1

ImageMagick

2
9.11.2
ImageMagickimport
(1)

$ import -window root screenshot.png

(2)
$ import screenshot.png

(3)
$ import -window window_id screenshot.png

xwininfo window_id
window_idimport-window

9.12

shell
Konsoletab
shell
ssh
shell

9.12.1
GNU screen
screen

9.12.2
screenshellscreenscreen

296

shell
shellCtrl+A+C
screenCtrl+A+"
/next/previous
Ctrl+ACtrl+NCtrl+ACtrl+P

screenscreenscreenscreen
detachingattachingCtrl+ACtrl+Dscreen
screen
screen r d

screenscreenscreen

screen r d PID

PIDscreenPID

IT

IT

DRM-free

PDF

ituring.com.cn