Skip to content

Instantly share code, notes, and snippets.

@itbakery
Created May 15, 2015 18:34
Show Gist options
  • Save itbakery/7ffcccec9ca0ad908bb3 to your computer and use it in GitHub Desktop.
Save itbakery/7ffcccec9ca0ad908bb3 to your computer and use it in GitHub Desktop.
lpi1-topic103.md

###Topic 103: GNU and unix Commands 103.1 การใช้งาน Command Line เรียนรู้การใช้งานคำสั่งใน linux ผ่านทาง Linux Shells ในที่นี้จะใช้ bash shells, shell ซึ่งเป็นโปรแกรมที่สามารถรองรับคำสั่ง command ได้

การใช้งาน shell environment ทดลองคำสั่ง ดู password

# cat /etc/passwd 
root:x:0:0:root:/root:/bin/bash 			<-- root login แล้วไปใช้ bash shell
bin:x:1:1:bin:/bin:/sbin/nologin 
daemon:x:2:2:daemon:/sbin:/sbin/nologin 
adm:x:3:4:adm:/var/adm:/sbin/nologin 
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 
sync:x:5:0:sync:/sbin:/bin/sync 
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 
halt:x:7:0:halt:/sbin:/sbin/halt 

shell environment ในระดับ global จะอยู่ที่ file /etc/profile ลอง run คำสั่ง vi /etc/profile และลองทดสอบด้วยการปรับแต่งค่า shell [root@centos ~]# กรณีเริ่มต้นใน /root

cd /root
vi .bashrc
# Source global definitions 
if [ -f /etc/bashrc ]; then 
        . /etc/bashrc 
fi 

จะไปเรียก /etc/bashrc

vi /etc/bashrc

สามารถเรียกดูค่าใน environment ได้ด้วยคำสั่ง env

# env 
HOSTNAME=centos.localdomain 
SELINUX_ROLE_REQUESTED= 
TERM=xterm-256color 
SHELL=/bin/bash 
HISTSIZE=1000 
SSH_CLIENT=192.168.122.1 53275 22 
SELINUX_USE_CURRENT_RANGE= 
SSH_TTY=/dev/pts/0 
...
...
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin <--ค่าใน variable

ตัวแปร PATH มีความสำคัญมาก เมื่อพิมพ์คำสั่งที่ shell นั้น shell จะไปหาว่าคำสั่งที่พิมพ์นั้นอยู่ในค่าของ PATH หรือไม่ หากไม่มีจะต้องสั่ง Command ด้วย absolute path

# cat << EOF > /root/testrun.sh
date
echo "test run"
EOF

# bash /root/testrun.sh 			// ต้องสั่งแบบ absolute path
Tue Oct 21 17:50:18 ICT 2014 
test run 

# chmod +x testrun.sh

การใช้งาน PATH ลองพิมพ์ echo $PATH เพื่อดูค่าของ path

# echo $PATH 
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

การใช้งาน environment variables การตั้งค่า environment variables สามารถทำได้โดยการใช้เครื่องหมาย '=' ใน Terminal เหมือนกับการกำหนด ตัวแปร และสามารถเรียกใช้งานโดยการใช้ $ นำหน้าค่าตัวแปรนั้น และใช้คำสั่ง export เพื่อให้ ABC เป็นค่า environment variables

# ABC=123   				   // set ค่าvariable
# export ABC	                       // ระบุให้ ABC เป็นตัวแปร environment variables
# env 
ABC=123 
...
# echo $ABC 				   // การใช้งานตัวแปรใช้ $ นำหน้า
123 

# unset ABC         		   // ยกเลิก environment variables

การเพิ่มตัวแปร PATH

#PATH=$PATH:/root
#export PATH
#echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/root 

ทำให้สามารถสั่ง testrun.sh ที่ตำแหน่งใดก็ได้โดยไม่ต้องระบุแบบ absolute path เพื่อความสะดวก

cd /
# testrun.sh 
Tue Oct 28 19:16:07 ICT 2014 
test run 

และเมื่อต้องการจะทำให้การแก้ไขยังคงอยู่หลังจากการ reboot ให้เพิ่มเติมใน ~/.bash_profile

# User specific environment and startup programs 

PATH=$PATH:$HOME/bin 

export PATH 

การใช้งาน history เมื่อต้องการเรียกดูคำสั่งย้อนหลังที่ได้สั่งไปแล้ว พิมพ์คำสั่ง history

# history 
    1  redhat 
    2  clear 
    3  echo $PATH 
    4  ABC=123 
    5  echo $ABC 
    6  env | grep ABC 
    7  export ABC 
    8  env 
    9  ls 
   10  PATH=$PATH:/root 
   11  export PATH 

การใช้ completion เพื่อความรวดเร็วในการใช้งานคำสั่ง linux มีตัวช่วยที่เรียกว่า completion ด้วยการพิมพ์อักษรแล้วตามด้วย tab 2 ครั้งshellจะทำการ list คำสั่งที่ขึ้นต้นเหมือนกันจาก folder ที่ระบุใน ตัวแปร path ทั้งหมดออกมา ลองพิมพ์ te แล้ว กด double tab

# te (tab)(tab)
tee               testrun.sh        texconfig         texhash           
telinit           testsaslauthd     texconfig-dialog  texlinks          
test              tex               texconfig-sys     texsis   

คำสั่ง uname เพื่อดูข้อมูลของระบบปฎิบัติการ

# uname -a 
Linux localhost.localdomain 3.10.0-229.1.2.el7.x86_64 #1 SMP Fri Mar 27 03:04:26 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

**คำสั่ง man ** เป็นคำสั่งที่ใช้เรียกดูคู่มือการใช้งานแต่ละคำสั่ง เช่น

# man yum install
# man history  					// ออก ด้วย q
# mandb
# man -k  <keyword>

option -k ใช้ค้นหา ใน man ด้วย keyword หลังจากสร้าง ฐานข้อมูล ด้วยคำสั่ง mandb

# man -k  _selinux
pam_selinux          (8)  - PAM module to set the default security context
$ man -k selabel
selabel_db           (5)  - userspace SELinux labeling interface: DB objects contexts backend
selabel_file         (5)  - userspace SELinux labeling interface: file contexts backend
selabel_media        (5)  - userspace SELinux labeling interface: media contexts backend
selabel_x            (5)  - userspace SELinux labeling interface: X Window System contexts backend

คำสั่ง exec [program] เป็นคำสั่งที่ใช้เมื่อต้องการสั่งให้โปรแกรมอื่นทำงานโดยให้ออกไปทำงานนอก shell ปัจจุบัน เช่น exec ls แต่่ก่อนที่จะสั่ง ls ให้จะออกจาก shell ก่อน เมื่อ ls ทำงานเสร็จก็จะกลับมาใน shell ปัจจุบัน ให้สังเกตดูว่าสีจะไม่เหมือนกัน เราสามารถดู process id ของ shell ปัจจุบันด้วย echo ตัวแปร $$ และสั่งคำสั่ง exec ksh เพื่อออกไปใช้งาน shell ksh และกลับมายัง process ของ bash เดิม (ksh - The Original ATT Korn Shell)

$ echo $SHELL
/bin/bash
$ sudo yum install ksh
$ echo $$
4314
$ bash
$ echo $$
5565
$ exec /bin/ksh
$ echo $$
5565
$ exit
$ echo $$
4314

ให้สังเกตลำดับของเลข pid ที่ปรากฏอยู่

###103.2 การใช้งาน text streams ร่วมกับการใช้งาน filters เป็นการใช้งานคำสั่ง ที่อยู่ใน package coreutils ได้แก่คำสั่ง ดังต่อไปนี้

command Description
#cat cat - concatenate files and print on the standard output
#cut cut - remove sections from each line of files
#expand expand - convert tabs to spaces
#fmt fmt - simple optimal text formatter
#head head - output the first part of files
#od od - dump files in octal and other formats
#join join - join lines of two files on a common field
#nl nl - number lines of files
#paste paste - merge lines of files
#pr pr - convert text files for printing
#sed sed - stream editor for filtering and transforming text
#sort sort - sort lines of text files
#split split - split a file into pieces
#tail tail - output the last part of files
#tr tr - translate or delete characters
#unexpand unexpand - convert spaces to tabs
#uniq uniq - report or omit repeated lines
#wc wc - print newline, word, and byte counts for each file

** การใช้งานคำสั่ง cat**

การเชื่อม files จำนวนหลาย files เข้าด้วยการ และพิมพ์ออกไปยัง output ที่ต้องการ เช่น การแสดงผลหน้าจอ หรือ นำผลลัพท์ ที่ได้ ไปเก็บใน file ใหม่ file เดียว

# cat file1.txt file2.txt 
file1 
file1 
file1 
file2 
file2 
file2 

redirect output ไปยังfile3.txt

# cat file1.txt file2.txt > file3.txt
# cat file3.txt 
file1 
file1 
file1 
file2 
file2 
file2 

การใช้คำสั่ง cut เมื่อต้องการตัดคำ เช่น ต้องการตัวอักษร ตำแหน่ง 2,3,4,5 เท่านั้น cut จะนับตำแหน่งแรก เป็น 1

# cat testcut.txt 
Please note these words are not saved anywhere and 
will disappear when you leave this page 
So if you like any of your randomnly generated words 
then write them down somewhere 

# cut -c 2,3,4,5 testcut.txt 
leas 
ill 
o if 
hen 

การใช้งานคำสั่ง expand ใช้แทนค่า tab (กด tab key) ด้วย space

# cat textexpand.txt 
convert		tabs 		in each FILE to 	spaces 
# expand textexpand.txt 
convert         tabs            in each FILE to     spaces     // replace tab ด้วย space

การใช้งานคำสั่ง fmt เมื่อต้องการ format text เพื่อตัดอักขระพิเศษออกไป

# cat textformat.txt 
convert	 
  tabs 	in each FILE to 	 
     spaces 
# fmt textformat.txt 
convert tabs 	in each FILE to spaces 

# fmt -w 5 textformat.txt 			// เอกสารที่มีขนาดกว้าง 5 ตัวอักษร
convert 
tabs 
in 
each 
FILE 
to 
spaces 

การใช้งานคำสั่ง head เพื่อดูข้อมูลของ file

# head /var/log/messages 
Oct 28 19:20:01 centos rsyslogd: [origin software="rsyslogd" swVersion="5.8.10" x-pid="960" x-info="http://www.rsyslog.com"] rsyslogd was HUPed 
Oct 28 19:23:49 centos dhclient[900]: DHCPREQUEST on eth0 to 192.168.122.1 port 67 (xid=0x29666cc6) 
Oct 28 19:23:49 centos dhclient[900]: DHCPACK from 192.168.122.1 (xid=0x29666cc6) 
Oct 28 19:23:51 centos dhclient[900]: bound to 192.168.122.46 -- renewal in 1394 seconds. 
Oct 28 19:32:04 centos yum[1208]: Installed: man-1.6f-32.el6.x86_64 
Oct 28 19:33:01 centos yum[1239]: Installed: apr-util-ldap-1.3.9-3.el6_0.1.x86_64 
Oct 28 19:33:02 centos yum[1239]: Installed: httpd-tools-2.2.15-31.el6.centos.x86_64 
Oct 28 19:33:04 centos yum[1239]: Installed: httpd-2.2.15-31.el6.centos.x86_64 
Oct 28 19:35:15 centos yum[1303]: Installed: httpd-manual-2.2.15-31.el6.centos.noarch 
Oct 28 19:47:05 centos dhclient[900]: DHCPREQUEST on eth0 to 192.168.122.1 port 67 (xid=0x29666cc6)

ระบุจำนวนบรรทัด ด้วย -n

# head -n 4 /var/log/messages 
Oct 28 19:20:01 centos rsyslogd: [origin software="rsyslogd" swVersion="5.8.10" x-pid="960" x-info="http://www.rsyslog.com"] rsyslogd was HUPed 
Oct 28 19:23:49 centos dhclient[900]: DHCPREQUEST on eth0 to 192.168.122.1 port 67 (xid=0x29666cc6) 
Oct 28 19:23:49 centos dhclient[900]: DHCPACK from 192.168.122.1 (xid=0x29666cc6) 
Oct 28 19:23:51 centos dhclient[900]: bound to 192.168.122.46 -- renewal in 1394 seconds.

การใช้งานคำสั่ง tail เมื่อต้องการดูส่วนท้ายของ file

# tail /var/log/messages 
Oct 28 19:35:15 centos yum[1303]: Installed: httpd-manual-2.2.15-31.el6.centos.noarch 
Oct 28 19:47:05 centos dhclient[900]: DHCPREQUEST on eth0 to 192.168.122.1 port 67 (xid=0x29666cc6) 
Oct 28 19:47:05 centos dhclient[900]: DHCPACK from 192.168.122.1 (xid=0x29666cc6) 
Oct 28 19:47:07 centos dhclient[900]: bound to 192.168.122.46 -- renewal in 1368 seconds. 
Oct 28 20:09:55 centos dhclient[900]: DHCPREQUEST on eth0 to 192.168.122.1 port 67 (xid=0x29666cc6) 
Oct 28 20:09:55 centos dhclient[900]: DHCPACK from 192.168.122.1 (xid=0x29666cc6) 
Oct 28 20:09:57 centos dhclient[900]: bound to 192.168.122.46 -- renewal in 1364 seconds. 
Oct 28 20:32:41 centos dhclient[900]: DHCPREQUEST on eth0 to 192.168.122.1 port 67 (xid=0x29666cc6) 
Oct 28 20:32:41 centos dhclient[900]: DHCPACK from 192.168.122.1 (xid=0x29666cc6) 
Oct 28 20:32:43 centos dhclient[900]: bound to 192.168.122.46 -- renewal in 1588 seconds. 

การใช้งานคำสั่ง od เพื่อการแสดงผลในลักษณะ เลขฐาน 8 (octal) แสดง \t \n

# od textformat.txt 
0000000 067543 073156 071145 004564 060564 071542 064440 020156 
0000020 060545 064143 043040 046111 020105 067564 071440 060560 
0000040 062543 005163 
0000044 
# od -c textformat.txt 
0000000   c   o   n   v   e   r   t  \t   t   a   b   s       i   n    
0000020   e   a   c   h       F   I   L   E       t   o       s   p   a 
0000040   c   e   s  \n 
0000044 

การใช้งานคำสั่ง join Join จะทำการรวม file บน field รวมกัน

# cat 1.txt 
10 blue 
20 red 
30 purple 
40 orange 
50 black 
# cat 2.txt 
10 socks 
20 shirts 
30 pants 
40 hats 
50 gloves 
# join 1.txt 2.txt
10 blue socks 
20 red shirts 
30 purple pants 
40 orange hats 
50 black gloves 

การใช้งานคำสั่ง nl print file พร้อมกับ หมายเลข บรรทัด

# nl 1.txt 
     1	10 blue 
     2	20 red 
     3	30 purple 
     4	40 orange 
     5	50 black 

การใช้งานคำสั่ง paste เป็นการรวม file นำบรรทัดแต่ละบรรทัดมาต่อกัน

# paste 1.txt 2.txt 
10 blue	10 socks 
20 red	20 shirts 
30 purple	30 pants 
40 orange	40 hats 
50 black	50 gloves 

การใช้งานคำสั่ง pr เป็นการแปลง text file ให้เหมาะสมสำหรับการ print

# pr 1.txt 
2014-10-28 21:06                      1.txt                       Page 1       

10 blue 
20 red 
30 purple 
40 orange 
50 black 

# pr /var/log/messages

การใช้งานคำสั่ง sed stream editer ที่ใช่บ่อยและมีประโยชน์อย่างมาก เช่น sed ตามด้วย option -e เพื่อทำการแก้ไข และตามด้วย 's/original/replace' แต่จะไม่ทำการแก้ไข file โดยตรง

# sed -e 's/blue/green/' 1.txt 
10 green 
20 red 
30 purple 
40 orange 
50 black 

การใช้งานคำสั่ง sort ต้องการเรียงบรรทัดใหม่เป็นลำดับตามตัวอักษร

# cat 1.txt 
60 gray 
10 blue 
20 red 
30 purple 
40 orange 
50 black 
# sort 1.txt 
10 blue 
20 red 
30 purple 
40 orange 
50 black 
60 gray 
# sort -r 1.txt                	//reverse sort
60 gray 
50 black 
40 orange 
30 purple 
20 red 
10 blue 
# sort -R 1.txt 			//random sort
40 orange 
30 purple 
10 blue 
50 black 
20 red 
60 gray 

การใช้งานคำสั่ง split เพื่อแบ่ง file ออกเป็นส่วน แต่ละส่วน และสร้าง file ใหม่ โดยสามารถระบุจำนวนบรรทัดได้ เช่น ต้องการให้แบ่ง 2 บรรทัด

# split -l 2 1.txt 
[root@centos ~]# cat xaa 
60 gray 
10 blue 
[root@centos ~]# cat xab 
20 red 
30 purple 
[root@centos ~]# cat xac 
40 orange 
50 black 

สามารถแบ่งออกได้เป็นจำนวน byte

# split -b 5 1.txt            // แต่ละfileจะมี 5อักษร (1 byte = 1 character)

การใช้งานคำสั่ง tr ทำการแปลง file จาก set (A-Z) หนึ่ง ไปอีก set (a-z) หนึ่ง

# echo "HELLO" | tr -t A-Z a-z 
hello 
# echo "HELLO" | tr  -t L l 
HellO 
# echo "HELLO" | tr  -d L 			//ตัดอักษร L
HEO 
# echo "HELLO" | tr  -s  L 			//ตัดอักษรซ้ำ
HELO 

การใช้งานคำสั่ง unexpand convert space ให้กลายเป็น tabs

# cat textformat.txt 
convert	tabs in each FILE to spaces
# unexpand -a  textformat.txt 
convert tabs in each FILE to spaces              //ช่องว่างที่มีจะเป็นtabไม่ใช่ space

การใช้งานคำสั่ง uniq เพื่อรายงานว่ามีบรรทัดใน file ที่ซ้ำกันหรือไม่ และแสดงผลเฉพาะที่บรรทัดที่มีความแตกต่างกัน มี option -u , -c

# cat 1.txt 
10 blue 
20 red 
20 red 
20 red 
30 purple 
40 orange 
40 orange 
40 orange 
50 black 
# uniq -c 1.txt          //countบรรทัดที่ซ้ำกัน
      1 10 blue 
      3 20 red 
      1 30 purple 
      3 40 orange 
      1 50 black 
# uniq -u 1.txt         //display uniq
10 blue 
30 purple 
50 black 
# uniq -d 1.txt 
20 red 
40 orange 

การใช้งานคำสั่ง wc word count นับจำนวนที่เกี่ยวข้องกับตัวอักษร แสดงผลเป็นแบ่งเป็น column ตามตัวอย่าง

# wc 1.txt 
 9 18 78 1.txt 

9 บรรทัด (line), 18 คำ (word), 78 ตัวอักษร (bytecount)

# wc -w 1.txt 			//only words count
18 1.txt 
wc
# wc  1.txt 2.txt    		//compare file
  9  18  78 1.txt 
  6  10  47 2.txt 
 15  28 125 total 

###103.3 เรียนรู้ การใช้งาน คำสั่งที่เกี่ยวข้องกับ File System พื้นฐาน การ copy , move, delete การใช้คำสั่ง cp , cp -R (copy folder , files หลาย ๆ files) การใช้คำสั่ง mv สำหรับการย้าย การใช้คำสั่ง rmสำหรับการ ลบ

# ls 
folder1
# cp folder1 folder2 
cp: omitting directory `folder1' 

# cp -R  folder1 folder2			//copy folder 3 ways -R, -r , --recursive  

# ls fol* 
folder1: 
123.txt  456.txt 

folder2: 
123.txt  456.txt 

# mv 123.txt abc.txt             	//mv เปลี่ยนชื่อ FILE
# mv abc.txt ../folder2 
# ls ../folder2 
123.txt  456.txt  abc.txt 

# mv folder2 folder3 			// เปลี่ยนชื่อfolder
# ls 	
folder1  folder3 

# mv folder1 folder3			// ย้ายfolder1 ไปยังfolder3	
# ls folder3/ 
123.txt  456.txt  abc.txt  folder1

# cd folder3/ 
# ls 
123.txt  456.txt  abc.txt  folder1 
# rm abc.txt 
rm: remove regular file `abc.txt'? Y 
# rm folder1/ 
rm: cannot remove `folder1/': Is a directory  // rm ไม่สามารถที่จะลบ folder ได้โดยตรง ต้องใช้ -r

# rm -r folder1 
rm: descend into directory `folder1'? Y

การสร้าง file สามารถทำได้โดยใช้คำสั่ง touch จะได้เป็น file ที่มีขนาด 0 byte

# ls -l 789.txt 
-rw-r--r--. 1 root root 0 Oct 28 23:21 789.txt
# mv 789.txt xyz.txt				//เปลี่ยนชื่่อ

การสร้าง directory สามารถทำได้โดยใช้คำสั่ง mkdir folder, และ rmdir folder เมื่อต้องการลบ

# mkdir folder
# ls 
drwxr-xr-x. 2 root root 4096 Oct 28 23:45 folder 
# rmdir folder

การใช้งานคำสั่ง file เป็นคำสั่งที่ใช้บอกชนิดของ file

# file 1.txt 
1.txt: ASCII text 
# touch empty.txt
# file empty.txt 
empty.txt: empty 
# file iproute-2.6.32-32.el6_5.src.rpm 
iproute-2.6.32-32.el6_5.src.rpm: RPM v3.0 src

การใช้งาน wildcards หรือเรียกว่า file globbing เพื่อสะดวกในการใช้งานมีการใช้อักษรแทนความหมายต่างๆ โดยจำเป็นต้องพิมพ์ทั้งหมด

  •     all   *.txt  *.rpm
    

? any single character แสดง file ที่มี จำนวนตัวอักษร เท่ากับจำนวน ? ! not [ac] a หรือ c [a-c] a,b,c

# rm -rf x*
# rm -rf *.rpm
# touch bag.txt bat.txt cat.txt sat.txt 
# ls * 						// แสดงทุกอย่าง
bag.txt  bat.txt  cat.txt  sat.txt 
# ls ???.??? 
bag.txt  bat.txt  cat.txt  sat.txt 
# ls ??? 
ls: cannot access ???: No such file or directory
# ls *.? 
ls: cannot access *.?: No such file or directory
# ls *.??? 
bag.txt  bat.txt  cat.txt  sat.txt
# ls [ab]* 						//ขึ้นต้นด้วย a หรือ b
bag.txt  bat.txt 
# ls [a-m] 	  					//ขึ้นต้นด้วย a b c d e .... ถึง
# ls [a-m]* 
bag.txt  bat.txt  cat.txt
# ls [!a-m]* 
sat.txt 

การใช้งานคำสั่ง find ใช้ในการค้นหา file สามารถกำหนดเงื่อนไขการค้นหาจาก type, mtime, ctime, size, name เป็นต้น

เตรียม folder และ file เพื่อการทดสอบ

# mkdir test
# cd test
# touch file.txt
# mkdir folder
# ln -s file.txt symlink.lnk
# touch 1.txt 2.txt 3.txt
# zip file.zip 1.txt 2.txt 3.txt 
  adding: 1.txt (stored 0%) 
  adding: 2.txt (stored 0%) 
  adding: 3.txt (stored 0%) 

หลังจากการเตรียม

# ls -l 
total 8 
-rw-r--r--. 1 root root    0 Oct 29 22:05 1.txt 
-rw-r--r--. 1 root root    0 Oct 29 22:05 2.txt 
-rw-r--r--. 1 root root    0 Oct 29 22:05 3.txt 
-rw-r--r--. 1 root root    0 Oct 29 22:03 file.txt 
-rw-r--r--. 1 root root  436 Oct 29 22:05 file.zip 
drwxr-xr-x. 2 root root 4096 Oct 29 22:03 folder 
lrwxrwxrwx. 1 root root    8 Oct 29 22:04 symlink.lnk -> file.txt 

ทดสอบคำสั่ง find รูปแบบ find ตามด้วย location เช่น .(dot) หรือ / (root)

# find .           			        //ค้นหาทุกอย่างแบบ recursive
. 
./folder 
./folder/4.txt 				 //ค้นหาในfolder 
./folder/5.txt 
./symlink.lnk 
./3.txt 
./1.txt 
./file.zip 
./file.txt 
./2.txt

ลองใช้ option -name , -size กำหนด หน่วยของขนาด b ,c,w, k, M, G (ดู man find)

# find . -name "fo*" 
./folder 
# find . -name "[a-z]*.*" 
./symlink.lnk 
./file.zip 
./file.txt 
# find . -name "[a-m]*.*" 
./file.zip 
./file.txt 

# find . -size +5M  //ค้นหา file ใน Current directory ที่มีขนาดมากกว่า >5 Megabyte(เครื่องหมาย +)
# find . -size -5M  //ค้นหา file ใน Current directory ที่มีขนาดมากกว่า <5 Megabyte(เครื่องหมาย -)
# zip log.zip /var/log/*.log
# find . -size +1k 
. 
./folder 
./log.zip 
# cp log.zip folder/backup.zip
# find . -size +1k  -name log*.* 
./log.zip 

ใช้คำสั่ง find ค้นหาด้วย ชนิด l = symbolic , f = file , d = directory

# find . -type l 
./symlink.lnk 
# find . -type d 
. 
./folder 
# find . -type f 
./folder/backup.zip 
./folder/4.txt 
./folder/5.txt 
./3.txt 
./1.txt 
./file.zip 
./file.txt 
./log.zip 
./2.txt 
# find . -type f -size +1k 
./folder/backup.zip 
./log.zip 

ใช้คำสั่ง find ค้นหาบนเงื่อนไขของเวลา TimeStamp มีเวลาอยู่ 3 ชนิด ที่สามารถค้นหาได้ ( + greater than มากกว่า, - less than น้อยกว่า) -atime +5 (file ที่มีการ accessมากกว่า 5 วันก่อน) เช่น มีการใช้คำสั่ง grep, sort, cat , head, tails -ctime +5 (file ที่มีการเปลี่ยนแปลง attribute มากกว่า 5 วันก่อน) เช่น มีการเปลี่ยนแปลง permission, เปลี่ยนแปลงเจ้าของ, หรือมีการย้ายไฟล์
-mtime +5 (file content มีการเปลี่ยนแปลงแก้ไขภายใน มากกว่า 5 วัน)

# find . -ctime +5
# find . -mtime +5
# find . -atime +5

การใช้งานคำสั่งสำหรับการบีบอัด (archiving data) tar, cpio, dd , gzip, bzip2 cpio ทำหน้าที่ อ่านไฟล์ ที่ส่งเข้ามาทาง standard input และcopy ไป ไปเก็บไว้ยัง archives ผ่านทาง standard output โดย คำสั่ง cpio จะทำการอ่านชื่อไฟล์ จาก list ของชื่อไฟล์ โดยในละบรรทัดของ list files จะมีเพียงชื่อเดียว เราสามารถเตรียม รายชื่อไฟล์ได้โดยใช้คำสั่ง ls หรือ คำสั่ง find ที่เราได้เรียนผ่านมา

archieve

# ls 
1.txt  2.txt  3.txt  file.txt  file.zip  folder  log.zip  symlink.lnk 
# ls | cpio -o > ../ls_archive.cpio 
79 blocks 

# find . -name "*.zip" 
./folder/backup.zip 
./file.zip 
./log.zip 
# find . -name "*.zip"  | cpio -o > ../find_archive.cpio 
155 blocks 

# less archive.cpio

แตกไฟล์ ใช้ option -i และ -d ให้สร้าง folder ด้วย หากภายใน cpio มี folder

# mkdir extract
# cd extract/
# cpio -id <  ../find_archive.cpio
# ls 
file.zip  folder  log.zip 

# ls -l 
total 48 
-rw-r--r--. 1 root root   436 Oct 30 01:03 file.zip 
drwxr-xr-x. 2 root root  4096 Oct 30 01:03 folder 
-rw-r--r--. 1 root root 39357 Oct 30 01:03 log.zip 

การใช้งานคำสั่ง dd ใช้สำหรับการ convert และ copy file จาก device เช่น disk /dev/sda, usb /dev/sdb ไปยัง image file

$ df -h 
Filesystem               Size  Used Avail Use% Mounted on 
...
/dev/sdb1                3.7G   84M  3.4G   3% /run/media/admin/CATVOIP 

# dd if=/dev/sdb  of=drive.img
7821312+0 records in 
7821312+0 records out 
4004511744 bytes (4.0 GB) copied, 273.487 s, 14.6 MB/s
$ file drive.img 
drive.img: ; partition 1 : ID=0x83, start-CHS (0x3,41,9), end-CHS (0x335,24,12), startsector 2048, 7819264 sectors 

restore drive.img ที่ backup ไว้ (แต่ file image ที่ได้เป็นแบบ byte ต่อ byte ไม่ได้มีการบีบอัด)

dd if=drive.img  of=/dev/sdb

**การใช้งาน gzip และ bzip2 ** เพื่อทำการบีบอัด file image ที่ได้ทำการ backup ไว้

ls -l drive.img 
-rw-r--r--. 1 root root 4004511744 Oct 30 01:37 drive.img 
gzip  drive.img 
ls -l drive.img.gz 
-rw-r--r--. 1 admin admin 3188329427 Oct 30 01:37 drive.img.gz 

ลองเปรียบเทียบขนาด file ที่ได้ extract คืน ด้วยคำสั่ง gunzip

$ gunzip drive.img.gz

สามารถใช้ bzip2 เพื่อทำการ backup ได้ และใช้คำสั่ง bunzip2 เพื่อการคืนค่า

**การใช้งานคำสั่ง tar ** คำสั่ง tar จะทำการรักษาโครงสร้างของ folder (tar cvf name.tar folders) โดยมีความหมายของ c = compress v = verbose f=file z=ชนิดการบีบอัดเป็น gzip ตัวอย่างนี้จะทำการ บีบอัดfile ด้วย option z (gzip) และ option j สำหรับการบีบอัด แบบ bzip2 และ x = extract backup (แทน c) ทั้ง folder ที่มีชื่อว่า test

# tar cvf archive.tar test
# tar cvzf archive.tar.gz test
# tar cvjf archive.tar.bz test

# ls -l archive.tar* 
-rw-r--r--. 1 root root 174080 Oct 30 02:18 archive.tar 
-rw-r--r--. 1 root root  53736 Oct 30 02:27 archive.tar.bz 
-rw-r--r--. 1 root root 154379 Oct 30 02:24 archive.tar.gz

###103.4 เรียนรู้ การใช้งาน streams, pipes, และ redirects เรียนรู้การใช้งาน redirect streams และเชื่อมต่อ streams ไปยัง process ที่มีหน้าที่จัดการข้อมูลที่เป็น text หน้าที่การใช้งานคือการ redirect standard output, standard errors, การ piping ส่ง output ที่ได้จากคำสั่งหนึ่งไปเป็น input ของอีกคำสั่งหนึ่งเสมือนหนึ่งว่า output ที่ส่งมานั้น เป็น Argument ของอีกคำสั่งและส่ง output ที่ได้นั้นในรูปแบบของ file หรือ stdout

การใช้งาน stdin, stdout , stderr ในความหมายทางโปรแกรมคอมพิวเตอร์ standard streams หมายถึงการที่มีการเชื่อมต่อ (connections) ช่องทางการส่งข้อมูล (communication channels) ระหว่างโปรแกรมผ่านทางระหว่าง input และ output ( I/O) สามารถแบ่ง I/O connections ออกได้เป็น3ชนิด คือ Standard input(stdin), Standard output (stdout) และ Standard error (stderr) โดย I/O เริ่มต้นจากการเชื่อมต่อผ่านการรับข้อมูลเข้า input เช่น keyboard และ output เช่น screen แต่ standard streams จะทำงานเป็นเบื้องหลังการส่งข้อมูลดังกล่าว คือเมื่อมีคำสั่งเกิดขึ้นใน SHELL การเชื่อมต่อ stream จะเชื่อมต่อกับ text terminal ที่เชื่อมกับ shell
io การ redirection (< input ,> output, >> append) คือ function ที่ใช้สำหรับ การ redirect input หรือ output ตามที่ต้องการ เช่น
command1 > file1 หมายถึงให้ทำการ run คำสั่ง command1 แล้วเอาผลลัพท์ที่ได้ไปเก็บไว้ใน file1 แทนที่จะแสดงผลที่ได้ออกทางหน้าจอ

# ls
# ls > ls.txt
# cat ls.txt 
anaconda-ks.cfg 
install.log 
install.log.syslog 
ls.txt 
results 

command1 < files1 หมายถึงการสั่งให้ command 1 ทำงาน และ ให้เอา file1 เป็น input แทนที่จะรับคำสั่งผ่านทาง keyboard (standard output)

# wc * 
   33   115  1129 anaconda-ks.cfg 
  218   449  9458 install.log 
   41   369  3161 install.log.syslog 
    0     0     0 results 
  292   933 13748 total 
# wc < anaconda-ks.cfg 
  33  115 1129 

command1 < infile > outfile ทำการรับ input file และ output file ในบรรทัดเดียว การใช้งาน redirect มีประโยชน์และมีความสะดวกในการใช้งานอย่างยิ่ง ในตัวอย่างต่อไป
1. ทำการ redirect inline file โดยการใช้งาน Here Document (HereDocument ใช้แทน file) เป็นstandard input โดยใช้ '<<'

tr a-z A-Z << END_TEXT
one two three
uno dos tres
END_TEXT
  1. หากต้องการ อ่านค่าจาก string แล้ว redirect มาเป็น standard input โดยใช้ '<<<'
# tr a-z A-Z <<< 'one two three'
ONE TWO THREE

# FOO='one two three'
# tr a-z A-Z <<< $FOO
 # tr a-z A-Z <<< 'one
 two three'
ONE
TWO THREE

การใช้ file handle กับ standard streams unix shells ได้พัฒนามาจาก Bourne shell ซึ่งมีการใช้หมายเลข 0, 1, 2 แทน stdin, stdout, stderr สามารถที่เลือกredirect อย่างใดอย่างหนึ่งได้

handle Name Descriptor
0 Stdin Standard input
1 Stdout Standard output
2 Stderr Standard error
ตัวอย่างเช่น
command1  2> file1

หมายถึงการสั่งให้ command1 ให้ทำงานและให้นำ Standard Error ที่ได้ไปเก็บไว้ใน file1

find / -name .profile > results 2>&1  | less

2>&1 หมายถึงการ redirect stderr ไปยัง stdout ( แต่ 2>1 จะหมายถึง การredirect stderr ไปยัง file ชื่อ 1)

# ls 1> ls.txt 
# cat ls.txt 
anaconda-ks.cfg 
install.log 
install.log.syslog 
ls.txt 
results 

# ls tom 
ls: cannot access tom: No such file or directory
# ls tom 2> ls.txt
# cat ls.txt 
ls: cannot access tom: No such file or directory

สามารถเขียนเป็นบรรทัดเดียวได้ เช่น

# ls tom >> result.txt  2>> errors.txt
# ls
.. 
-rw-r--r--. 1 root root    0 Oct 30 12:24 result.txt              //size 0, NO RESULT
-rw-r--r--. 1 root root   49 Oct 30 12:24 errors.txt    

ในทางกลับกัน

# ls  >> result.txt 2>> errors.txt
-rw-r--r--. 1 root root    0 Oct 30 12:27 errors.txt 		//NO ERROR
-rw-r--r--. 1 root root   76 Oct 30 12:27 result.txt 

รวม stderr และ stdout เข้าด้วยกัน 2>&1

# ls tom > output.txt                     //no stdout  
# ls tom > output.txt 2>&1		   // merge stdout and stdin						

การใช้งาน pipe (แทนด้วย) " | "
การเชื่อม programs หลายโปรแกรมให้สามารถทำงานต่อกัน โดยนำ stdout ของโปรแกรมหนึ่ง มาเป็น stdin ของอีกโปรแกรมหนึ่ง

command1 | command 2

command1 จะทำงาน และส่ง output ที่ได้ไปให้กับ command 2 เป็น input วิธีการนี้เรียกว่า "piping" แทนด้วยเครื่องหมาย " | "
terminal

การใช้งานคำสั่ง xargs เป็นคำสั่งที่ใช้ส่งค่าจาก standard output ที่รับมาจาก standard input หนึ่ง ไปให้อีกคำสั่งหนึ่ง
จากตัวอย่าง คำสั่ง grep มีความสามารถที่จะรับค่าจาก stdin

# ls 
anaconda-ks.cfg  errors.txt  install.log  install.log.syslog
# ls | grep an 
anaconda-ks.cfg 

แต่ไม่ใช่ว่าทุกคำสั่งจะสามารถรับค่าจาก stdin เช่น คำสั่ง echo จะไม่รับค่าจาก stdin

# echo "Hello" 
Hello 
# ls | echo             	// echo not except stdin ทำให้พิมพ์ค่าว่างออกมา

จึงต้องใช้คำสั่ง xargs ช่วยรับค่า จาก stdout ของ ls

# ls | xargs echo 
anaconda-ks.cfg errors.txt install.log install.log.syslog

สามารถที่จะ limit output แต่ละบรรทัด -n options ตามด้วยจำนวน items ในแต่ละบรรทัด

# echo a b c d e f| xargs 
a b c d e f 
# echo a b c d e f| xargs -n 3
a b c 
d e f
# echo a b c d e f| xargs -n 2
a b 
c d 
e f 

**การใช้งานคำสั่ง tee ** คำสั่ง tee เป็น โปรแกรมที่ใช้สำหรับการ redirect เหมือนกับ pipe แต่จะสามารถ redirect ไปได้หลาย destination tee

# ls -l  | tee  xyz 
total 20 
-rw-------. 1 root root 1129 Oct 30 11:56 anaconda-ks.cfg 
-rw-r--r--. 1 root root    0 Oct 30 12:27 errors.txt 
-rw-r--r--. 1 root root 9458 Oct 30 11:56 install.log 
-rw-r--r--. 1 root root 3161 Oct 30 11:55 install.log.syslog 
-rw-r--r--. 1 root root    0 Oct 30 13:04 xyz             
# ls -l 
total 24 
-rw-------. 1 root root 1129 Oct 30 11:56 anaconda-ks.cfg 
-rw-r--r--. 1 root root    0 Oct 30 12:27 errors.txt 
-rw-r--r--. 1 root root 9458 Oct 30 11:56 install.log 
-rw-r--r--. 1 root root 3161 Oct 30 11:55 install.log.syslog 
-rw-r--r--. 1 root root  281 Oct 30 13:06 xyz   

คำสั่งนี้จะทำการ redirect ผลที่ได้ stdout จาก ls -l ไปยัง file xyz และอีก destination หนึ่งคือแสดงผลออกทางหน้าจอ

###103.5 สร้าง (create) และ ตรวจสอบการทำงาน (monitor) และ การ kill process การตรวจสอบการทำงาน (monitor) และบริหารจัดการ กับ Process สามารถสั่งให้ทำงานในโหมด foreground และ background สั่งให้ทำงานแบบ nohug เพื่อให้ processทำงานอยู่แม้มีการ logout แล้ว ทำให้ process มีความคงอยู่ (persistent) สามารถตรวจสอบการทำงานของ process ที่ยังทำงาน การติดต่อกับ process ที่กำลังทำงาน และการเรียง Sort

การทำงาน foreground และ background เมื่อทดลองสั่งคำสั่ง sleep 1000 จะไม่สามารถใช้งานได้ (ยกเลิกด้วย ctrl-c) เนื่องจากเป็นการทำงานใน mode foreground ให้สั่งใหม่โดยพิมพ์ sleep 1000 ตามด้วยเครื่องหมาย & (ampersand) เพื่อทำให้คำสั่งนั้นดังกล่าวทำงานในโหมด background ทุกคำสั่งที่ทำงานในโหมด background จะมีหมายเลขประจำ โดยใช้คำสั่ง jobs เพื่อต้องการดูคำสั่งทั้งหมดใน backgroundหากต้องการให้คำสั่งใดทำงานบน foreground ใช้คำสั่ง fg และส่งกลับไปยัง background ด้วย ctrl-z

[root@centos65 ~]# sleep 1000 & 
[1] 1159 					[1] job id,   1159 – process id
[root@centos65 ~]# sleep 2000 & 
[2] 1160 
[root@centos65 ~]# jobs 
[1]-  Running                 sleep 1000 & 
[2]+  Running                 sleep 2000 & 
[root@centos65 ~]# fg 2  		// 1) Ctrl + Z send command to foreground and suspended

[root@centos65 ~]# jobs 
[1]-  Running                 sleep 1000 & 
[2]+  Stopped                 sleep 2000       // 2) job number 2 suspended

[root@centos65 ~]# bg 2                        // 3) start background process again 
[2]+ sleep 2000 & 
[root@centos65 ~]# jobs 
[1]-  Running                 sleep 1000 & 
[2]+  Running                 sleep 2000 & 
[root@centos65 ~]# jobs -l 
[1]-  1159 Running            sleep 1000 & 
[2]+  1160 Running            sleep 2000 & 

สามารถหยุด kill process ได้โดย คำสั่ง kill -9 ทำให้ state เปลี่ยนไปเป็น killed

[root@centos65 ~]# jobs -l 
[2]-  1160 Running            sleep 2000 & 
[3]+  1177 Stopped            ping google.com 
[root@centos65 ~]# kill -9 1177 
[root@centos65 ~]# jobs -l 
[2]-  1160 Running            sleep 2000 & 
[3]+  1177 Killed             ping google.com
[root@centos65 ~]# fg 3 
-bash: fg: 3: no such job 
[2]+  Done                    sleep 2000 

การใช้งานคำสั่ง ps
คำสั่ง ps เป็นคำสั่งสำหรับดู process ที่ทำงานอยู่

  1. ดู process ทั้งหมด
[root@centos65 ~]# ps  aux			// BSD style ไม่มีเครื่องหมาย (- , dash)
[root@centos65 ~]# ps -ef				// Unix / Linux style มีเครื่องหมาย (- , dash)

a ดูprocess ทั้งหมด ที่ run อยู่ u , -f แสดง detail information x ดู process ที่อยู่ นอก terminal

[root@centos65 ~]# sleep 2000 & 
[1] 1324 
[root@centos65 ~]# jobs 
[1]+  Running                 sleep 2000 & 

เปิดอีก terminal หนึ่ง แล้วพิมพ์ jobs จะไม่สามารถแสดงผล process เนื่องจากเป็นการทำงานคนละ terminal แต่สามารถใช้คำสั่ง ps aux เพื่อดู process ที่ run อยู่ได้

[root@centos65 ~]# jobs					 
[root@centos65 ~]# ps aux | grep sleep 
root      1324  0.0  0.0 100908   600 pts/0    S    13:21   0:00 sleep 2000 
root      1326  0.0  0.0 103244   852 pts/1    S+   13:23   0:00 grep sleep 
[root@centos65 ~]# kill -9 1324 
[root@centos65 ~]# killall sleep
  1. แสดงprocess โดย filter ด้วยผู้ใช้งาน user โดยใช้ "-u" options
[root@centos65 ~]# ps -f -u postfix 
UID        PID  PPID  C STIME TTY          TIME CMD 
postfix   1093  1086  0 11:12 ?        00:00:00 qmgr -l -t fifo -u 
postfix   1280  1086  0 12:52 ?        00:00:00 pickup -l -t fifo -u 
  1. แสดง process โดย filter ด้วย ชื่อคำสั่ง โดยใช้ "-C" options
[root@centos65 ~]# ps -C bash 
  PID TTY          TIME CMD 
 1129 tty1     00:00:00 bash 
 1147 pts/0    00:00:00 bash 
 1213 pts/1    00:00:00 bash 
  1. แสดง process โดย filter ด้วย process idโดยใช้ "-p" options [root@centos65 ~]# ps -f -p 1205,1147,1209
UID        PID  PPID  C STIME TTY          TIME CMD 
root      1147  1143  0 11:13 pts/0    00:00:00 -bash 
root      1205     1  0 12:01 ?        00:00:00 /usr/sbin/anacron -s 
root      1209  1008  0 12:01 ?        00:00:00 sshd: root@pts/1 
  1. เรียง process ด้วย cpu และ memory
[root@centos65 ~]# ps aux --sort=-pcpu,+pmem 
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND 
root         1  0.0  0.1  19232  1484 ?        Ss   11:12   0:00 /sbin/init 
root         2  0.0  0.0      0     0 ?        S    11:12   0:00 [kthreadd] 
root         3  0.0  0.0      0     0 ?        S    11:12   0:00 [migration/0] 
root         4  0.0  0.0      0     0 ?        S    11:12   0:00 [ksoftirqd/0] 
root         5  0.0  0.0      0     0 ?        S    11:12   0:00 [migration/0] 
root         6  0.0  0.0      0     0 ?        S    11:12   0:00 [watchdog/0] 
  1. แสดงผลเป็น hierarchy ในรูปแบบ tree
[root@centos65 ~]# ps -ef –forest
root       356     1  0 11:12 ?        00:00:00 /sbin/udevd -d 
root      1121   356  0 11:12 ?        00:00:00  \_ /sbin/udevd -d 
root      1122   356  0 11:12 ?        00:00:00  \_ /sbin/udevd -d 
root       891     1  0 11:12 ?        00:00:00 /sbin/dhclient -1 -q -lf /var/lib/dhclient/dhclie 
root       935     1  0 11:12 ?        00:00:00 auditd 
root       951     1  0 11:12 ?        00:00:00 /sbin/rsyslogd -i /var/run/syslogd.pid -c 5 
root      1008     1  0 11:12 ?        00:00:00 /usr/sbin/sshd 
root      1143  1008  0 11:13 ?        00:00:00  \_ sshd: root@pts/0 
root      1147  1143  0 11:13 pts/0    00:00:00  |   \_ -bash 
root      1321  1147  0 13:18 pts/0    00:00:00  |       \_ ps -ef --forest 
root      1209  1008  0 12:01 ?        00:00:00  \_ sshd: root@pts/1 
root      1213  1209  0 12:01 pts/1    00:00:00      \_ -bash 
  1. ปรับแต่งแสดงผล ps ตามที่เราต้องการ
[root@centos65 ~]# ps -eo user,pid,pcpu,nice,command 
USER       PID %CPU  NI COMMAND 
root         1  0.0   0 /sbin/init 
root         2  0.0   0 [kthreadd] 
root         3  0.0   - [migration/0] 
root         4  0.0   0 [ksoftirqd/0] 
root         5  0.0   - [migration/0] 
root         6  0.0   - [watchdog/0]

**การใช้งาน nohup ** หากต้องการให้ process หนึ่งๆ คงอยู่เสมอ แม้จะทำการ logout หรือออกจาก ssh shell เมื่อมีการทำงานแบบ remote จะใช้คำสั่ง nohup (ไม่รับสัญญาณ hang up ขณะที่ logout) ให้เปิด terminalสั่งคำสั่ง nohup แล้วจึงปิด terminal หลังจากนั้นให้เปิด terminal ขึ้นมาใหม่

[root@centos65 ~]# nohup sleep 2222 & 
[1] 1347

login เข้ามาใหม่

[root@centos65 ~]# ps aux | grep sleep 
root      1347  0.0  0.0 100908   600 ?        S    13:31   0:00 sleep 2222 
root      1365  0.0  0.0 103244   852 pts/1    S+   13:33   0:00 grep sleep 

การใช้งานคำสั่ง top

[root@centos65 ~]# top

เพื่อดูการทำงานทั้งหมด และโดย default จะเรียงตามการใช้งาน %cpu

top


การใช้งาน top ใช้ <,> เพื่อให้ top เรียงค่าตามเงื่อนไข เริ่มที่ %CPU กด k เมื่อเราต้องการ หยุด process จะมีคำว่า PID to kill: รอรับค่า ของ process id

top1 top1

ต่อมาจะถาม ว่าkill ด้วย signal อะไร Kill PID with signal [15] signal [15] เป็นการ หยุด process แต่หากเป็น signal 9 จะหมายถึง force และหยุดทั้งหมด เหมือน killall

การใช้งานคำสั่ง uptime แสดงผลเป็นระยะเวลาที่เครื่องทำงานอยู่และแสดง load average ในเวลา 1 , 5 ,15 นาที ที่ผ่านมา

# uptime 
 13:51:27 up  2:38,  3 users,  load average: 0.00, 0.00, 0.00  

การใช้งานคำสั่ง free ดูสถานะการใช้งาน memory

[root@centos65 ~]# free 
             total       used       free     shared    buffers     cached 
Mem:       1020404     139292     881112          0       7464      40936 
-/+ buffers/cache:      90892     929512 
Swap:       835576          0     835576 

ผลลัพท์ที่ออกมา มีความหมายดังนี้ line1: แสดง ปริมาณ memory เช่น total available RAM, Used RAM , Free RAM line2: แสดง buffer/cache line3: แสดง สถานะของ swap

###103.6 ปรับแต่ง ค่าความสำคัญ ในการรัน process priorities (modify process execution priorities) การใช้งานคำสั่ง nice ใช้สำหรับการตรวจสอบ และเปรียบเทียบระดับการใช้งาน CPU ของ process ด้วยคำสั่ง nice โดยมีระดับช่วงของ niceness จาก -20 เป็นระดับ priority สูงสุด จนถึง 19 เป็นระดับ priority ต่ำสุด สามารถสั่งโดยกำหนดระดับความสำคัญได้ เพื่อไม่ให้เกิดการรบกวน process อื่น

ทดลอง (#info nice)

$ nice -n 19 tar cvzf archive.tgz largefile
[root@centos65 ~]# nice 
0 
[root@centos65 ~]# nice -n 10 nice 
10 

โดยปรกติแล้ว คำสั่งจะทำงานที่ nice ระดับ 0

[root@centos65 ~]# sleep 5000 & 
[1] 1556 
[root@centos65 ~]# ps -eo user,nice,comm | grep sleep 
root       0 sleep 

**** nice -n -20 จะต้องใช้ สิทธิ root

[root@centos65 ~]# nice -n -20  sleep  5000  & 
[1] 1587 
[root@centos65 ~]# ps  -eo  user,pid,nice,comm | grep sleep 
root      1587 -20 sleep 

การใช้งานคำสั่ง renice เพื่อเปลี่ยนค่า priority

[root@centos65 ~]# renice 15  -p 1587 
1587: old priority -20, new priority 15 
[root@centos65 ~]# ps  -eo  user,pid,nice,comm | grep sleep 
root      1587  15 sleep 

การใช้งานคำสั่ง renice ให้กำหนดค่า niceness เริ่มต้นของแต่ละคน เดิมนั้นจะมีค่า เป็น "0"

[root@centos65 ~]# useradd  admin 
[root@centos65 ~]# passwd admin 
[root@centos65 ~]# su - admin 
[admin@centos65 ~]$ 
[admin@centos65 ~]$ sleep  2000 & 
[1] 1637
[admin@centos65 ~]$ ps  -eo  user,pid,nice,comm | grep sleep 
root      1587  15 sleep 
admin     1637   0 sleep 
[admin@centos65 ~]$ 
[admin@centos65 ~]$ sudo renice 19 -u admin 
500: old priority 0, new priority 19

สั่ง sleep อีกครั้ง จะได้ว่าค่าของ niceness จะเป็น 19 แล้ว

[admin@centos65 ~]$ sleep 2222 & 
[1] 1705 
[admin@centos65 ~]$  ps  -eo  user,pid,nice,comm | grep sleep 
root      1587  15 sleep 
admin     1637  19 sleep 
admin     1705  19 sleep 

การใช้งาน คำสั่ง pstree แสดง process ในรูปแบบ tree

[root@centos65 ~]# pstree 
init─┬─auditd───{auditd} 
     ├─crond 
     ├─dhclient 
     ├─login───bash 
     ├─master─┬─pickup 
     │        └─qmgr 
     ├─5*[mingetty] 
     ├─rsyslogd───3*[{rsyslogd}] 
     ├─sshd─┬─sshd───bash───pstree 
     │      └─sshd───bash───top 
     └─udevd───2*[udevd] 
...

###103.7 ค้นหาคำ ที่ต้องการใน file ด้วยการใช้ regular expression (Regex) การใช้งาน regex และการจัดการ file และ text โดยการใช้ regular expression รวมถึงการสร้าง regular expression อย่างง่ายที่มีองค์ประกอบของสัญลักษณ์ (notational elements) และสามารถใช้ expression tools เพื่อค้นหาทั้ง file system

การใช้งานคำสั่ง grep, fgrep, egrep, sed, regex

[admin@centos65 ~]$ cat file1.txt 
blades 
books 
Books 
book 
kitten 
doogy 
purple 
fa 
ship 
plenty 
[admin@centos65 ~]$ cat file2.txt 
chicken 
fish 
shirmp 
turkey 
fist 
foot 
arm 
lemon 
angry 
[admin@centos65 ~]$ $ grep oo file1.txt 
books 
Books 
book 
doogy 
[admin@centos65 ~]$ grep -n oo file1.txt 
2:books 
3:Books 
4:book 
6:doogy 
[admin@centos65 ~]$ grep boo  file1.txt 
books 
book 
[admin@centos65 ~]$ grep -i boo  file1.txt        //case sensitive
books 
Books 
book 

ใช้ร่วมกับคำสั่งผลลัพท์จาก ls และ ใช้ รวมกับ pipe

[admin@centos65 ~]$ ls | grep ile 
file1.txt 
file2.txt 
[admin@centos65 ~]$ cat file1.txt | grep oo 
books 
Books 
book 
doogy 

ใช้ร่วมกับอักขระ ^ (เริ่มต้นบรรทัด begin of line) , $ (สิ้นสุดบรรทัดend of line) . (dot , any character อักษรอะไรก็ได้)

[admin@centos65 ~]$ grep ^ple file1.txt 
plenty 
[admin@centos65 ~]$ grep ple$ file1.txt 
purple 
[admin@centos65 ~]$ grep .ple file1.txt 
purple 

การใช้งาน egrep (Extension grep) egrep สามารถใช้งานได้เหมือนกับ grep แต่ต่างกันตรงที่ทำงานในระดับที่ซับซ้อนได้มากขึ้น โดยสามารถกำหนดเงื่อนไขเพื่อขยายความสามารถในการค้นหา เราต้องกำหนด pattern ภายใน format '( | )' เช่น
'^(b|d)' จะหมายถึง ว่าค้นหา บรรทัดที่ขึ้นต้นด้วย ตัวอักษร b หรือ d

[admin@centos65 ~]$ egrep .ple file1.txt 
purple 
[admin@centos65 ~]$ egrep '^(b|d)'  file1.txt 
blades 
books 
book 
doogy 
[admin@centos65 ~]$ egrep '^(b|d)oo'  file1.txt 
books 
book 
doogy 

สามารถใช้งานร่วมกับ range [ ]

$ egrep '^[a-k]'  file1.txt 
blades 
books 
book 
kitten 
doogy 
fa 

สามารถใช้งานร่วมกับ range [ ]

$ egrep '^[a-k]'  file1.txt 
blades 
books 
book 
kitten 
doogy 
fa 

สามารถใช้งานร่วมกับ '^([ ]|[ ])'

[admin@centos65 ~]$ egrep '^([a-k]|[A-K])' file1.txt 
blades 
books 
Books 
book 
kitten 
doogy 
fa 

การใช้งาน fgrep (fast grep) การใช้งาน fgrep (grep -F) มีความแตกต่างกัน จาก grep และ egrep (grep -E) fgrep ทำหน้าค้นหาคำใน file ความหมาย -E , --extended-regexp, -F , --Fixed-strings , fgrep ค้นหาคำ string จะไม่ได้รับ pattern ทำให้สะดวกในการใช้งาน

[admin@centos65 ~]$ fgrep book file1.txt 
books 
book 

**การใช้งาน sed ** sed คือ stream editor สำหรับแก้ไข ออกมาทางหน้าจอ แต่จะไม่แก้ไข file หากต้องการ แก้ไข file จะต้อง redirect ด้วย > หรือ ใช้ option -i

[admin@centos65 ~]$ sed -e 's/oo/00/' file1.txt 
blades 
b00ks 
B00ks 
b00k 
kitten 
d00gy 
purple 
fa 
ship 
plenty 
[admin@centos65 ~]$ sed -e 's/oo/00/' file1.txt  > newfile.txt 

สามารถใช้ extend regular extression โดยการใช้ option -r -r, --regexp-extended use extended regular expressions in the script.

[admin@centos65 ~]$ sed -re 's/^(B|b)/C/' file1.txt 
Clades 
Cooks 
Cooks 
Cook 
kitten 
doogy 
purple 
fa 
ship 
plenty

###103.8 การใช้งาน vi เพื่อแก้ไข file การใช้งานพื้นฐาน vi ได้แก่ inserting editing deleting copying และ finding text สำหรับผู้ดูแลระบบแล้ว vi ถือว่าเป็น editor ที่มีประโยชน์อย่างยิ่ง ดังนั้นการเรียนรู้จนมีความเข้าใจที่ถูกต้องนั้นถือได้ว่า ได้ประโยชน์อย่างแท้จริงvi มี 2 mode คือ 1. command mode สำหรับส่งคำสั่งไปยัง vi เพื่อทำการแก้ไขและจัดการ text และ 2. insert mode สำหรับพิมพ์อักษร ในโหมด command mode จะไม่สามารถพิมพ์ได้ สามารถใช้ได้เพียง command เท่านั้น หากต้องการใช้งานใน insert mode ให้กด i,I (i ใหญ่) ,a,A,o,O และหากต้องการกลับไปยัง command mode ให้กด ESC

การใช้งาน keyboard i,I (i ใหญ่) ,a,A,o,O การใช้งานจะยึดเอาตำแหน่งของ cursor ปัจจุบันเป็นหลัก โดยคำนึงถึงหลักการเคลื่อนที่เป็นสำคัญ

กด i เป็นการ insert หน้า cursor กดI (shift-i) เป็นการ insert ตำแหน่งแรกของบรรทัด
กดa เป็นการ insert หลัง cursor (a = append) กด A เป็นการ insert ตำแหน่ง สุดท้ายของ บรรทัด
กด o เป็นการ insert บรรทัดล่าง (next-line) กด O เป็นการ insert บรรทัดบน (previous line)

การเคลื่อนที่ ของ cursor เมื่ออยู่ใน command mode
keystroke

**** หากกด w จะเป็นการข้ามไปคำถัดไป ตรงกันข้ามหากกด b จะย้อนหลัง กดesc เข้าcommand mode รอรับคำสั่ง ที่บรรทัดล่างสุดของ vi
ก่อนสั่งคำสั่ง ให้พิมพ์ เครื่องหมาย : แล้วตามตัวคำสั่ง เช่น ต้องการ save ไฟล์ที่ทำการแก้ไข ให้กด :wq

flow

การ Copy Cut Paste copy/paste ทั้งบรรทัด อยู่ใน command mode นำ cursor มาที่บรรทัดที่ต้องการ copy แล้ว กด yyจากนั้นย้าย cursor ไปยังตำแหน่งต้องการแล้วกด p (put) เพื่อวางบรรทัดที่ copy มาไว้ที่บรรทัดล่างจาก cursor

copy/paste บางส่วน การcopy บางส่วนในแต่ละบรรทัด สามารถทำได้โดยการทำงานใน visual mode โดยเลื่อน cursor มาที่จะเริ่มต้นของส่วนที่ต้องการ COPY แล้ว กด "v" (v เล็ก) แล้วใช้ key hjkl เพื่อทำการเลื่อน ในขณะที่เลื่อนไปนั้น VI จะแสดงผลเป็นการ Highlight เมื่อเลื่อนมาถึง จุดสุดท้ายแล้ว ให้กด y เพื่อออกจาก visual mode ส่วนการ paste ให้ใช้ p เหมือนกับการ COPY ด้วย yy โดย vi จะเอาคำที่เรา copy มา ต่อท้ายจาก cursor เพิ่มเติมการใช้งาน visual mode v (v เล็ก) เป็นการใช้งาน visual mode ระดับ character นับเป็นตัวอักษร V (V ใหญ่) เป็นการใช้งาน visual mode ระดับ บรรทัด เลือกทั้งบรรทัด Ctrl-v เป็นการใช้งาน ในระดับ block-visual mode (rectangles of text) ctrlv การลบ delete ใน vi และ นำไปเก็บที่ register รอการ put ลบ บรรทัด ทั้งบรรทัด กด dd บนบรรทัดที่ cursor อยู่
ลบ คำ ให้นำ cursor มาวางไว้ที่อักษรแรกของคำ แล้ว กด dw ลบ 1 ตัวอักษรให้ กด dl และสามารถ กด p เมื่อต้องการ put

คำสั่ง change 'c' ต้องการเปลี่ยนหรือแทนที่คำให้นำ cursor วางที่อักษรตัวแรก แล้ว กด 'cw'

การค้นหาคำใน vi backword search กด '?' แล้วคำที่ต้องการ ค้นหา ให้กด 'n' สำหรับค้นหาคำถัดไปแบบย้อนหลัง และ 'N' จะกลับทิศทาง forward search กด '/' แล้วตามด้วยคำที่ต้องการค้นหา ให้กด 'n' สำหรับค้าหาคำถัดไปแบบไปข้างหน้า และเมื่อ กด 'N' จะกลับทิศทางกัน

การ save กด ' : colon' แล้ว ตามด้วย คำสั่ง :e! ยกเลิกการแก้ไขทั้งหมด :w เหมือน save as :q ออกจาก vi แต่ไม่ save :wq! ต้องการ save และ ออกจาก vi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment