หาเป้าหมายและทำ command อื่นต่อ

January 25th, 2012

บางครั้งต้องการหาไฟล์หรือ ไดเรคทอรี ซึ่งไม่รู้อยู่ ณ จุดไหน ถ้าเจอแล้วต้องทำอะไรต่อ

เราสามารถทำได้โดยใช้คำสั่ง Find

การค้นหา directory หรือ ชื่อไฟล์ใน directory ที่ต้องการค้น
Linux command “.” หมายถึง current directory ในการกรณีไม่ใส่ path ในการค้นหา

find [pathnames] [conditions] 
 
find . -name "[file name or directory name]"
ตัวอย่าง
find  /home/data/  -name "*.log" 
find . -name "*data*.*"

ค้นหาคำ

ค้นหาในคำ <div id="newuser"> ทุกไฟล์ใน  /export/web/code/
find  /export/web/code/ -exec grep -l "<div id=\"newuser\">"  {} \;
 
ค้นหาคำ "postcode" ในทุกๆ ไฟล์ในไดเรคทอรีปัจจุบัน
find . -exec grep -l "postcode" {} \;  
 
ค้นหาคำ "postcode" ในทุกๆ ไฟล์ในไดเรคทอรีนั้น
find /home/username/HEAD/production/cust/templates  -exec grep -l "postcode" {} \;

หาและแทนที่

ใช้ Find หาไฟล์ และใช้คำสั่ง sed ในการหาคำและ replace
syntax
find [directory] -name [specify name] -exec sed -i "s/[find string]/[replace with]/g" {} \;
เช่น
find . -name "data.txt" -exec sed -i "s/cat/dog/g" {} \;
หาไฟล์ data.txt ในไดเรคทอรีปัจจุบัน และ replace cat ด้วย dog

หาและลบ

บางครั้งเราต้องการลบไฟล์ที่ไม่จำเป็นออก เช่น log เก่าๆ ของงานต่างๆ
หลักการคือ หาไฟล์ หรือหาไดเรคทอรี แล้วถ้าเจอก็ลบซะ
เราต้องรู้การใช้คำสั่ง 2 คำสั่ง คือ find และ rm แต่จะให้ทำงานพร้อมไปกับหาเจอเลย โดยที่เราไม่ต้องตามไปลบออกที่หลัง
 
find . -name *_11*.log -exec rm {} \;
หาไฟล์อะไรก็ได้ที่มี _11 และนามสกุล log ถ้าหาเจอแล้วลบ
 
find . -type d -exec rm -r {} \;
หาไดเรคทอรี แล้วลบ
 
find . -type f -mtime +0 -exec rm {} \;
หาไฟล์ที่มีการเปลี่ยนแปลง เจอแล้วลบ

Find and Sort by last modified

It's useful when we would like to check the latest update. Go to directory you would like to check, the run command follow instruction below:  
find . -type f -mtime -[day] -ls | sort -k8,9r
// syntax find = look at present date to previous date [day] and sort with column 8 and 9
 
find . -type f -mtime -1 -ls | sort -k8,9r
// check yesterday to today and sort by month and day
 
find . -type f -mtime -3 -exec ls --full-time  {} \; | sort -k6,7

Find and Get the size of all directories in the current directory

find . -maxdepth 1 -type d -print | xargs du -sk | sort -rn
// maxdepth 1 = only category under current directory
// -type d = only directory
// xargs = pass list of directory to du command

Find, then copy to one directory

find [path-looking] -name "[keyword]" -exec cp [--parents] -r {} [destination]\;
 path-looking: directory or folder that we would like to look for (e.g. production/generic/wireframe/USdummycontent)
 keyword: wording to specific to find (e.g. data.rec, ae, wz*.*)
  --parents: append source path to DIRECTORY
  -r: copy directories recursively
  destination: directory or folder you would like to copy to
 
 Example:
find production/generic/site/language/  -name "fr" -exec cp --parents -r {} only_th/ \;
 (copy all fr folders to only_th folder)

Find, then copy to the same directory

find [path] -name "[keyword]" | awk '{print "cp -r "$1" "$1";"}' | sed "s/[keyword]\;/[newkeyword]/g" | sh
 
Ex:find . -name "en" | awk '{print "cp -r "$1" "$1";"}' | sed "s/en\;/ae/g" | sh

เรื่องของ atime mtime และ ctime ของไฟล์บน Unix

January 25th, 2012

บางครั้งเราเอาไฟล์ไปตั้งไว้บน Server นานๆ จนลืมไป แล้วต้องการรู้ว่าเราตั้งไว้นานแล้วหรือยัง และถูกเปลี่ยนไปเมื่อไหร เก่า หรือใหม่อย่างไร
อีกประการ ยังถูกใช้โดยโปรแกรม Backup เพื่อเลือกไฟล์สำหรับการทำ Backup แบบ Incremental คือ เลือกเฉพาะไฟล์ที่เพิ่มขึ้นใหม่ หรือไฟล์ที่ถูกเปลี่ยนแปลงเท่านั้น
เวลาของไฟล์บน Unix มีอยู่ 3 แบบ คือ atime, ctime, mtime ในที่นี้จะอธิบายการเปลี่ยนแปลงของค่าเวลาต่างๆ ดังนี้

ใช้คำสั่ง date เพื่อดูเวลาที่ทดสอบ

[root@SkyIT /time]$ date
Wed Jan 25 16:42:41 ICT 2012

สร้างไฟล์เปล่าๆ ด้วยคำสั่ง touch

[root@SkyIT /time]$ touch test1.txt

ใช้คำสั่ง ls -l เพื่อดูข้อมูลไฟล์เบื้องต้น

[root@SkyIT /time]$ ls -l test1.txt
-rw-r--r-- 1 root root 0 Jan 25 17:02 test1.txt

ใช้คำสั่ง stat เพื่อดูข้อมูลไฟล์โดยละเอียด

[root@SkyIT /time]$ stat test1.txt
  File: `test1.txt'
  Size: 0               Blocks: 0          IO Block: 8192   regular empty file
Device: 1540000h/22282240d      Inode: 204499      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2012-01-25 17:02:21.684449000 +0700
Modify: 2012-01-25 17:02:21.684449000 +0700
Change: 2012-01-25 17:02:21.684455000 +0700

หมายเหตุ เวลาที่แสดงจากคำสั่ง ls -l เป็นเวลา Modify (mtime)

เวลา Access (atime)

เมื่อไฟล์ถูก access หรือดูเนื้อหาภายในไฟล์ เวลา Access (atime) จะถูกปรับปรุงโดยอัตโนมัติ

เช่น ใช้คำสั่ง cat เพื่อเรียกดูไฟล์ เวลา Access (atime) ก็จะถูกเปลี่ยนไป ตามเวลาที่ access

[root@SkyIT /time]$ date
Wed Jan 25 17:04:43 ICT 2012
[root@SkyIT /time]$ cat test1.txt
[root@SkyIT /time]$ ls -l test1.txt
-rw-r--r-- 1 root root 0 Jan 25 17:02 test1.txt
[root@SkyIT /time]$ stat test1.txt
  File: `test1.txt'
  Size: 0               Blocks: 0          IO Block: 8192   regular empty file
Device: 1540000h/22282240d      Inode: 204499      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2012-01-25 17:04:59.253304000 +0700
Modify: 2012-01-25 17:02:21.684449000 +0700
Change: 2012-01-25 17:02:21.684455000 +0700

สังเกตว่า เวลาของไฟล์ จากการรันคำสั่ง ls -l ไม่เปลี่ยนแปลง เพราะ mtime ไม่เปลี่ยน

เวลา Change (ctime)

เมื่อมีการเปลี่ยนแปลงข้อมูลเกี่ยวกับ Inode ของไฟล์ เช่นการเปลี่ยน Owner, Group, Permission เวลา ctime จะถูกปรับปรุง

ตัวอย่างการเปลี่ยน permission ทำให้เวลา ctime เปลี่ยนแปลง

[root@SkyIT /time]$ date
Wed Jan 25 17:05:55 ICT 2012
[root@SkyIT /time]$ chmod 600 test1.txt
[root@SkyIT /time]$ ls -l test1.txt
-rw------- 1 root root 0 Jan 25 17:02 test1.txt
[root@SkyIT /time]$ stat test1.txt
  File: `test1.txt'
  Size: 0               Blocks: 0          IO Block: 8192   regular empty file
Device: 1540000h/22282240d      Inode: 204499      Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2012-01-25 17:04:59.253304000 +0700
Modify: 2012-01-25 17:02:21.684449000 +0700
Change: 2012-01-25 17:06:06.641615000 +0700

สังเกตว่า เวลา Access (atime), Modify (mtime) หรือเวลาจากคำสั่ง ls -l ไม่เปลี่ยนแปลงไป เพราะคำสั่ง chmod เปลี่ยนแค่ข้อมูลใน Inode

เวลา Modify (mtime)

เมื่อเนื้อหาในไฟล์ถูกแก้ไข เวลา Modify (mtime) จะถูกปรับปรุง ตัวอย่างเช่น

[root@SkyIT /time]$ date
Wed Jan 25 17:08:37 ICT 2012

ใช้คำสั่ง echo และ shell redirection “>” เพิ่มเนื้อหาเข้าไปในไฟล์

[root@SkyIT /time]$ echo "add more content to file" >> test1.txt
[root@SkyIT /time]$ ls -l test1.txt
-rw------- 1 root root 25 Jan 25 17:09 test1.txt
[root@SkyIT /time]$ stat test1.txt
  File: `test1.txt'
  Size: 25              Blocks: 2          IO Block: 8192   regular file
Device: 1540000h/22282240d      Inode: 204499      Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2012-01-25 17:04:59.253304000 +0700
Modify: 2012-01-25 17:09:24.682162000 +0700
Change: 2012-01-25 17:09:24.682162000 +0700

เมื่อเนื้อหาในไฟล์ถูกเปลี่ยน เวลา Change (ctime) ก็จะถูกปรับไปด้วย

Create virtual host on Apache

September 21st, 2011

การสร้าง Virtual Host บน Apache

สภาพแวดล้อมที่ทดสอบ
– AppServ 2.5.10 for Windows
– Window XP SP3

โดยปกติเราจะทำเว็บจะเข้าไปสร้างที่ โฟลเดอร์ C:\AppServ\www แต่แนะนำให้ทำที่ D:\AppServ\www จะป้องกันข้อมูลหายอันเกิดจากลง Windows ใหม่โดยลืม Backup Web App ที่เราได้ทำไป

เวลาเราต้องการทดสอบ Web App ที่เราได้ develop ไปแล้ว ก็เปิดเบราเซอร์ http://localhost/MyWebApp ซึ่งมันเป็นโฟลเดอร์ย่อย ที่เราไปสร้างไว้
D:\AppServ\www\MyWebApp

ถ้าเราต้องการให้ url ของเราในเครื่องของเรา จาก http://localhost/MyWebApp ให้เป็น http://www.MyWebApp.com จะทำได้มั้ย มีวิธีมั้ย?
คำตอบ มีครับโดยการใช้วิธี virtual host และยังสามารถสร้าง App ของเราไว้ใน ไดรว์ไหนก็ได้

ขั้นตอนการทำ virtual host มีทั้งหมด 3 ขั้นตอน
1. แก้ที่ไฟล์ hosts จะอยู่ใน C:\Windows\System32\drivers\etc\
เป็นการบอกเส้นทางให้เบราเซอร์รู้ว่ามันจะไปทางไหน ในไฟล์ host จะเก็บ ชื่อ URL และ IP ไว้ เมื่อเบราเซอร์เห็นว่า URL นี่มัน IP 127.0.0.1 มันอยู่ในเครื่องเราเอง ก็ไม่จำเป็นต้องออกไปสู้โลกภายนอก แต่ถ้า url ที่เราพิมพ์มาไม่เจอในไฟล์ host ก็จะส่งคำร้องออกไปสู่วงแลน ถ้ายังไม่มีอีกก็จะส่งออกไปสู้ผู้ให้บริการอินเตอร์เน็ตที่กำลังเชื่อมต่ออยู่ต่อไป

แก้ไขไฟล์ hosts กำหนดเส้นทาง
1.1 เปิดไฟล์ hosts ขึ้นมาด้วยโปรแกรม Notepad
1.2 เพิ่มชื่อเว็บไซต์ที่เราต้องการ หรือสมมติเข้าไป และให้ชี้กลับมาที่เครื่องเราเอง จะใช้ .com ก็ได้ครับ หรือจะใช้ดอท อะไรก็ได้ครับ

127.0.0.1	www.MyWebApp.loc

1.3 Save ไฟล์ Host

2. แก้ที่ไฟล์ httpd.conf จะอยู่ใน D:\AppServ\Apache2.2\conf\
เป็นการเปิดปลักอิน mod_vhost_alias ที่ถูกปิดไว้ และกำหนดสิทธิ์ในการเข้าใช้งาน virtual host
2.1 เปิดไฟล์ httpd.conf ขึ้นมาด้วยโปรแกรม Notepad
2.2 ที่บรรทัด 203 ให้เอาเครื่องหมาย # ออก

#LoadModule vhost_alias_module modules/mod_vhost_alias.so

แก้ไขเป็น

LoadModule vhost_alias_module modules/mod_vhost_alias.so

2.3 ที่บรรทัด 561 ให้เอาเครื่องหมาย # ออก

# Virtual hosts
#Include conf/extra/httpd-vhosts.conf

แก้ไขเป็น

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

2.4 เพิ่มสิทธิ์ในการเข้าใช้งานโฟลเดอร์ MyWebApp บนไดร์ D: ต่อท้ายไฟล์ httpd.conf ได้เลย

<Directory "E:/Workspace/MyWebApp">
    Options Indexes FollowSymLinks MultiViews ExecCGI
    AllowOverride All
    Allow from all
</Directory>

2.5 Save ไฟล์ httpd.conf

3. แก้ที่ไฟล์ httpd-vhosts.conf จะอยู่ใน D:\AppServ\Apache2.2\conf\extra
เป็นการกำหนดที่เก็บ App ของเราในที่นี้ชื่อ MyWebApp เพื่อ กำหนดให้ url www.MyWebApp.loc ชี้ไปที่ Web App ของเรา
3.1 เปิดไฟล์ httpd-vhosts.conf ขึ้นมาด้วยโปรแกรม Notepad
3.2 เพิ่มข้อมูลข้างล่างต่อท้ายไฟล์ได้เลย

<VirtualHost *:80>
    ServerAdmin webmaster@MyWebApp.loc
    DocumentRoot "E:/Workspace/MyWebApp"
    ServerName MyWebApp.loc
    ServerAlias www.MyWebApp.loc
    ErrorLog "logs/MyWebApp.loc-error.log"
    CustomLog "logs/MyWebApp.loc-access.log" common
</VirtualHost>

– DocumentRoot : คือที่เก็บไฟล์
– ServerName : ชื่อโดเมน ต้องเป็นชื่อเดียวกับที่อยู่ในไฟล์ hosts
– ServerAlias : ชื่อเสมือนโดเมน มันจะได้เข้าได้ทั้งมี www และ ไม่มี

3.3 Save ไฟล์ httpd-vhosts.conf

หลังจากทำมาจนครบ 3 ขั้นตอนแล้วให้ Restart Apache ก่อน โดยเข้าไปที่
C:\Documents and Settings\\Start Menu\Programs\AppServ\Control Server by Service\
ดับเบิลคลิก Apache Restart

ปล.
ก่อนจะทดลองทำ Virtual Host แนะนำให้ Back up ไว้ก่อนก็ดีครับ ทั้ง 3 ไฟล์ที่กล่าวมาข้างต้น