Linux : Cammand Linux ค้นหาไฟล์หรือ Directory
200910 Jun
Find
ค้นหา directory หรือ ชื่อไฟล์ใน directory ที่ต้องการค้น ทำไมช่างลำบากยากเข็นกับ command ตัวของกระผมเองยังจำไม่ได้หมดเลยทำไว้เป็นชุดคำสั่งไว้สำหรับกันลืมแล้วก้อเอาแบ่งปันความรู้ให้กับท่านอื่นๆ ที่ใช้ Linux command "." หมายถึง current directory ในการกรณีไม่ใส่ path ในการค้นหา
find . -name "[file name or directory name]" -print
Ex: find /home/kritr/sitename/ -name "*templates.*.*" -print
Ex: find . -name "*xapian*.*" -print
// look up in current directory and subdirectory to find out where is filename or directory contain the word "xapian" in.
Ex: find /home/kritr/sitename/ -name "*templates.*.*" -print
Ex: find . -name "*xapian*.*" -print
// look up in current directory and subdirectory to find out where is filename or directory contain the word "xapian" in.
Find wording
find /export/web/code/ -exec grep -l "<div id=\"newuser\">" {} \;
// ค้นหาในคำ <div id="newuser"> ทุกไฟล์ใน /export/web/code/
find . -exec grep -l "postcode" {} \;
// find "postcode" wording in every file, in current directory.
find /home/username/HEAD/production/cust/templates -exec grep -l "postcode" {} \;
//find "postcode" wording in every file, in specific directory
// ค้นหาในคำ <div id="newuser"> ทุกไฟล์ใน /export/web/code/
find . -exec grep -l "postcode" {} \;
// find "postcode" wording in every file, in current directory.
find /home/username/HEAD/production/cust/templates -exec grep -l "postcode" {} \;
//find "postcode" wording in every file, in specific directory
Find and Replace wording
It seems like find wording command, but we use "sed" command instead.
find [directory] -name [specify name] -exec sed -i "s/[find string]/[replace with]/g" {} \;
// syntax
find . -name "data.rec" -exec sed -i "s/containsproducts/categorylist/g" {} \;
// find file name is "data.rec" in current directory and replace string "containsproducts" with "categorylist"
find [directory] -name [specify name] -exec sed -i "s/[find string]/[replace with]/g" {} \;
// syntax
find . -name "data.rec" -exec sed -i "s/containsproducts/categorylist/g" {} \;
// find file name is "data.rec" in current directory and replace string "containsproducts" with "categorylist"
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 . -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
// 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)
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
Ex:find . -name "en" | awk '{print "cp -r "$1" "$1";"}' | sed "s/en\;/ae/g" | sh




