Linux command: find & locate — locating files on Linux
Published in
5 min readFeb 12, 2019
การค้นหาไฟล์ใน Linux สามารถใช้ได้ทั้งคำสั่ง find
และ locate
แต่ทั้ง 2 คำสั่งก็มีรายละเอียดปลีกย่อยให้สามารถเลือกใช้ได้มากมาย
คำสั่ง find
find
คือคำสั่งที่ใช้สำหรับค้นหาข้อมูลในลินุกซ์ สามารถพิมพ์คำสั่ง find
เพื่อค้นหาข้อมูลได้ดังตัวอย่างต่อไปนี้
$ find
.
./tmp1
./tmp1/index.htm
./tmp1/index.html
./html
./html/index.html
- เมื่อพิมพ์คำสั่ง
find
เบื้องต้นโปรแกรมจะค้นหาข้อมูลที่ต้องการจากไดเรกทอรีปัจจุบันก่อน (Current Directory) ดังนั้น เมื่อพิมพ์คำสั่งfind
ผลลัพธ์ที่ได้จึงแสดงข้อมูลไฟล์และไดเรอทอรีทั้งหมดที่อยู่ในไดเรกทอรีปัจจุบัน ดังผลลัพธ์ข้างต้น - สามารถพิมพ์คำสั่ง
tree
เพื่อตรวจสอบข้อมูลไฟล์และไดเรกทอรีที่มีอยู่ในไดเรกทอรีปัจจุบัน ผลลัพธ์แสดงดังต่อไปนี้
$ tree
.
├── html
│ └── index.html
└── tmp1
├── index.htm
└── index.html2 directories, 3 files
การค้นหาข้อมูลจากไดเรกทอรีปัจจุบัน
- คำสั่ง
find .
เป็นการค้นหาข้อมูลจากไดเรกทอรีปัจจุบัน ซึ่งเครื่องหมาย full stop (.
) ใช้แทนไดเรกทอรีปัจจุบัน ซึ่งได้ผลลัพธ์เช่นเดียวกับคำสั่งfind
ผลลัพธ์ที่ได้แสดงดังต่อไปนี้
$ find .
.
./tmp1
./tmp1/index.htm
./tmp1/index.html
./html
./html/index.html
การค้นหาข้อมูลโดยระบุไดเรกทอรี
- คำสั่ง
find
สามารถระบุไดเรกทอรีที่จะค้นหาได้ ดังตัวอย่างต่อไปนี้
$ find tmp1/
tmp1/
tmp1/index.htm
tmp1/index.html
การค้นหาข้อมูลจากทั้งฮาร์ดดิส
- หากต้องการค้นหาข้อมูลจากทั้งฮาร์ดดิสสามารถทำได้โดยใช้เครื่องหมาย slash (`/’) แสดงดังต่อไปนี้
$ find //initrd.img
/media
/media/kaveepoj
/media/kaveepoj/Ubuntu 17.10 amd641
/media/kaveepoj/Ubuntu 17.10 amd641/.disk
/media/kaveepoj/Ubuntu 17.10 amd641/.disk/base_installable
/media/kaveepoj/Ubuntu 17.10 amd641/.disk/casper-uuid-generic
/media/kaveepoj/Ubuntu 17.10 amd641/.disk/cd_type
/media/kaveepoj/Ubuntu 17.10 amd641/.disk/info
/media/kaveepoj/Ubuntu 17.10 amd641/.disk/release_notes_url
/media/kaveepoj/Ubuntu 17.10 amd641/boot
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub/efi.img
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub/font.pf2
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub/grub.cfg
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub/loopback.cfg
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub/x86_64-efi
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub/x86_64-efi/acpi.mod
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub/x86_64-efi/adler32.mod
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub/x86_64-efi/ahci.mod
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub/x86_64-efi/all_video.mod
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub/x86_64-efi/aout.mod
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub/x86_64-efi/appleldr.mod
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub/x86_64-efi/archelp.mod
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub/x86_64-efi/ata.mod
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub/x86_64-efi/at_keyboard.mod
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub/x86_64-efi/backtrace.mod
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub/x86_64-efi/bfs.mod
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub/x86_64-efi/bitmap.mod
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub/x86_64-efi/bitmap_scale.mod
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub/x86_64-efi/blocklist.mod
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub/x86_64-efi/boot.mod
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub/x86_64-efi/bsd.mod
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub/x86_64-efi/bswap_test.mod
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub/x86_64-efi/btrfs.mod
/media/kaveepoj/Ubuntu 17.10 amd641/boot/grub/x86_64-efi/bufio.mod
...
- หากต้องการดูตัวเลือก (option) ของคำสั่ง
find
สามารถพิมพ์คำสั่งfind --help
จากนั้นโปรแกรมจะแสดงตัวเลือกทั้งหมด แสดงดังต่อไปนี้
$ find --help
Usage: find [-H] [-L] [-P] [-Olevel] [-D debugopts] [path...] [expression]default path is the current directory; default expression is -print
expression may consist of: operators, options, tests, and actions:
operators (decreasing precedence; -and is implicit where no others are given):
( EXPR ) ! EXPR -not EXPR EXPR1 -a EXPR2 EXPR1 -and EXPR2
EXPR1 -o EXPR2 EXPR1 -or EXPR2 EXPR1 , EXPR2
positional options (always true): -daystart -follow -regextypenormal options (always true, specified before other expressions):
-depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf
--version -xdev -ignore_readdir_race -noignore_readdir_race
tests (N can be +N or -N or N): -amin N -anewer FILE -atime N -cmin N
-cnewer FILE -ctime N -empty -false -fstype TYPE -gid N -group NAME
-ilname PATTERN -iname PATTERN -inum N -iwholename PATTERN -iregex PATTERN
-links N -lname PATTERN -mmin N -mtime N -name PATTERN -newer FILE
-nouser -nogroup -path PATTERN -perm [-/]MODE -regex PATTERN
-readable -writable -executable
-wholename PATTERN -size N[bcwkMG] -true -type [bcdpflsD] -uid N
-used N -user NAME -xtype [bcdpfls] -context CONTEXTactions: -delete -print0 -printf FORMAT -fprintf FILE FORMAT -print
-fprint0 FILE -fprint FILE -ls -fls FILE -prune -quit
-exec COMMAND ; -exec COMMAND {} + -ok COMMAND ;
-execdir COMMAND ; -execdir COMMAND {} + -okdir COMMAND ;Valid arguments for -D:
exec, help, opt, rates, search, stat, time, tree
Use '-D help' for a description of the options, or see find(1)Please see also the documentation at http://www.gnu.org/software/findutils/.
You can report (and track progress on fixing) bugs in the "find"
program via the GNU findutils bug-reporting page at
https://savannah.gnu.org/bugs/?group=findutils or, if
you have no web access, by sending email to <bug-findutils@gnu.org>.
การค้นหาไฟล์โดยไม่สนใจตัวอักษร (ignore)
- การค้นหานั้นสามารถกำหนดตัวเลือกให้ค้นหาโดยไม่สนใจตัวอักษรไม่ว่าจะเป็นตัวอักษรตัวเล็กหรือตัวใหญ่ โดยใช้ตัวเลือก
iname
ดังตัวอย่างต่อไปนี้
$ find -iname INDEX.html
./tmp1/index.html
./html/index.html
- จากตัวอย่างข้างต้นเมื่อใช้ตัวเลือก
iname
แม้ในไดเรกทอรีจะไม่มีไฟล์ชื่อINDEX.html
แต่ก็ยังค้นเจอไฟล์index.html
การค้นหาข้อมูลโดยค้นหาจากชื่อ
- หากต้องการค้นหาไฟล์หรือไดเรอทอรีโดยค้นหาจากชื่อสามารถทำได้โดยใช้ตัวเลือก
-name
ผลลัพธ์ที่ได้แสดงดังต่อไปนี้
$ find -name index.html
./tmp1/index.html
./html/index.html
หรือ
$ find . -name index.html
./tmp1/index.html
./html/index.html
- ทั้งสองคำสั่ง
find -name index.html
และfind . -name index.html
มีผลลัพธ์ที่เหมือนกัน ดังนั้นหากต้องการจะค้นหาไฟล์จากไดเรกทอรีปัจจุบันสามารถละทิ้งเครื่องหมาย.
ได้ - ดังนั้น หากต้องการค้นหาไฟล์
index.html
จากทั้งฮาร์ดดิส สามารถทำได้โดยใช้คำสั่งดังต่อไปนี้
$ find / -name index.html
ผลลัพธ์ที่ได้แสดงดังต่อไปนี้
find: ‘/proc/10902/fd’: Permission denied
find: ‘/proc/10902/map_files’: Permission denied
find: ‘/proc/10902/fdinfo’: Permission denied
find: ‘/proc/10902/ns’: Permission denied
find: ‘/proc/14908/task/14908/fd’: Permission denied
find: ‘/proc/14908/task/14908/fdinfo’: Permission denied
find: ‘/proc/14908/task/14908/ns’: Permission denied
find: ‘/proc/14908/fd’: Permission denied
find: ‘/proc/14908/map_files’: Permission denied
find: ‘/proc/14908/fdinfo’: Permission denied
find: ‘/proc/14908/ns’: Permission denied
find: ‘/proc/15359/task/15359/fd’: Permission denied
find: ‘/proc/15359/task/15359/fdinfo’: Permission denied
find: ‘/proc/15359/task/15359/ns’: Permission denied
find: ‘/proc/15359/fd’: Permission denied
find: ‘/proc/15359/map_files’: Permission denied
find: ‘/proc/15359/fdinfo’: Permission denied
find: ‘/proc/15359/ns’: Permission denied
find: ‘/proc/15360/task/15360/fd’: Permission denied
- จากผลลัพธ์ข้างต้น แสดงให้เห็นว่า
user
ไม่มีสิทธิ์ในการเข้าถึงข้อมูล ดังนั้นจะต้องใช้คำสั่งsudo
เข้ามาช่วยในการให้สิทธิ์การเข้าถึงข้อมูล ตัวอย่างคำสั่งแสดงดังต่อไปนี้
$ sudo find / -name index.html
[sudo] password for kaveepoj:
/usr/share/apache2/default-site/index.html
/usr/share/gtk-doc/html/totem/index.html
/usr/share/transmission/web/index.html
/usr/share/cups/doc-root/es/index.html
/usr/share/cups/doc-root/ja/index.html
/usr/share/cups/doc-root/ru/index.html
/usr/share/cups/doc-root/de/index.html
/usr/share/cups/doc-root/pt_BR/index.html
/usr/share/cups/doc-root/index.html
/usr/share/doc/doc-base/doc-base.html/index.html
/usr/share/doc/dbconfig-common/dbapp-policy.html/index.html
- จากผลลัพธ์ข้างต้นแสดงให้เห็นถึงการค้นหาไฟล์ที่ชื่อ
index.html
ที่ค้นหาจากทั้งฮาร์ดดิส
การค้นหาข้อมูลโดยใช้งานร่วมกับ wildcard
- การค้นหาข้อมูลนั้นสามารถค้นหาได้ทั้งจากไฟล์และไดเรกทอรี อีกทั้งยังสามารถใช้เครื่องหมาย
wildcard
เช่น*
และ?
ในการค้นหาได้ด้วย - เริ่มต้นด้วยการพิมพ์คำสั่ง
tree
เพื่อตรวจสอบข้อมูลในไดเรกทอรีปัจจุบัน
$ tree
.
├── html
│ └── index.html
└── tmp1
├── index.htm
└── index.html2 directories, 3 files
- จากนั้นพิมพ์คำสั่งดังต่อไปนี้เพื่อค้นหาข้อมูลจากไดเรกทอรีปัจจุบัน
$ sudo find -name i*.h???
./tmp1/index.html
./html/index.html
จากตัวอย่างข้างต้น มีความหมายดังนี้
i*
หมายถึง ขึ้นต้นด้วยตัวอักษรi
และตามด้วยตัวอักษรกี่ตัวก็ได้ (*
).h???
หมายถึง มีนามสกุลขึ้นต้นด้วยตัวอักษรh
และตามด้วยตัวอักษรอีก 3 ตัว???
- ดังนั้น
i*.h???
จึงหมายถึง ไฟล์ที่ขึ้นต้นด้วยตัวอักษรi
ที่มีนามสกุลทั้งหมด 4 ตัวและขึ้นต้นด้วยตัวอักษรh
การค้นหาไดเรกทอรี
- สำหรับตัวอย่างการค้นหาไดเรกทอรีนั้น สามารถทดสอบโดยการสร้างไดเรกทอรีชื่อ
test
โดยใช้คำสั่งmkdir
แสดงดังตัวอย่างต่อไปนี้
$ mkdir test
- ตรวจสอบข้อมูลไฟล์และไดเรกทอรีโดยใช้คำสั่ง
tree
$ tree
.
├── html
│ └── index.html
├── test
└── tmp1
├── index.htm
└── index.html3 directories, 3 files
- จากนั้นทดสอบการค้นหาไดเรกทอรีด้วยคำสั่งดังต่อไปนี้
$ find te*
test
- จากตัวอย่างข้างต้นเป็นการค้นหาไดเรกทอรีที่ขึ้นต้นด้วยตัวอักษร
te
และตามด้วยตัวอักษรใด ๆ ก็ได้ (*
) จากนั้นสามารถทดสอบการค้นหาไดเรกทอรีด้วยคำสั่งfind t*
แสดงดังตัวอย่างต่อไปนี้
$ find t*
test
tmp1
tmp1/index.htm
tmp1/index.html
- จากผลลัพธ์ข้างต้น เป็นการค้นหาไดเรกทอรีที่ขึ้นต้นด้วยตัวอักษร
t
และตามด้วยตัวอักษรใด ๆ ก็ได้ (*
)
การค้นหาไฟล์และไดเรกทรอรี
- การค้นหาและแสดงเฉพาะไฟล์ที่อยู่ในไดเรกทอรีสามารถทำได้โดยใช้คำสั่งดังต่อไปนี้
$ find -type f
./tmp1/index.htm
./tmp1/index.html
./html/index.html
- สำหรับการค้นหาและแสดงเฉพาะไดเรกทอรีทั้งหมดที่อยู่ในไดเรกทอรีปัจจุบัน สามารถทำได้โดย
$ find -type d
.
./tmp1
./html
./test
- ตัวเลือก
-type
หมายถึงประเภทของข้อมูล ดังนั้นf
จึงหมายถึงไฟล์ และd
หมายถึงไดเรกทอรี
คำสั่ง locate
locate
คือคำสั่งสำหรับการค้นหาไฟล์ สามารถใช้คำสั่งlocate --help
เพื่อดูตัวอย่างการใช้โปรแกรม ผลลัพธ์แสดงดังต่อไปนี้
$ locate --help
Usage: locate [OPTION]... [PATTERN]...
Search for entries in a mlocate database. -A, --all only print entries that match all patterns
-b, --basename match only the base name of path names
-c, --count only print number of found entries
-d, --database DBPATH use DBPATH instead of default database (which is
/var/lib/mlocate/mlocate.db)
-e, --existing only print entries for currently existing files
-L, --follow follow trailing symbolic links when checking file
existence (default)
-h, --help print this help
-i, --ignore-case ignore case distinctions when matching patterns
-l, --limit, -n LIMIT limit output (or counting) to LIMIT entries
-m, --mmap ignored, for backward compatibility
-P, --nofollow, -H don't follow trailing symbolic links when checking file
existence
-0, --null separate entries with NUL on output
-S, --statistics don't search for entries, print statistics about each
used database
-q, --quiet report no error messages about reading databases
-r, --regexp REGEXP search for basic regexp REGEXP instead of patterns
--regex patterns are extended regexps
-s, --stdio ignored, for backward compatibility
-V, --version print version information
-w, --wholename match whole path name (default)Report bugs to mitr@redhat.com.
การค้นหาไฟล์ด้วยคำสั่ง locate
- หากต้องการค้นหาไฟล์ชื่อ
index.html
สามารถใช้คำสั่งดังต่อไปนี้
$ locate index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/soy/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/sparql/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/spreadsheet/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/sql/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/stex/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/stylus/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/swift/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/tcl/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/textile/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/tiddlywiki/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/tiki/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/toml/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/tornado/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/troff/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/ttcn/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/ttcn-cfg/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/turtle/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/twig/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/vb/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/vbscript/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/velocity/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/verilog/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/vhdl/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/xml/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/xquery/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/yaml/index.html
/usr/share/webmin/filemin/unauthenticated/js/lib/codemirror/mode/z80/index.html
/usr/share/webmin/inittab/help/index.html
/usr/share/webmin/time/help/index.html
/var/www/html/index.html
- โดยที่คำสั่ง
locate
จะค้นหาจากทั้งฮาร์ดดิส ซึ่งแตกต่างจากคำสั่งfind
ที่จะเริ่มต้นค้นหาจากไดเรกทอรีปัจจุบัน
นับจำนวนของไฟล์ที่ค้นพบ
- คำสั่ง
locate
สามารถนับจำนวนของไฟล์หรือไดเรกทอรีที่ค้นพบโดยใช้ตัวเลือก (option)-c
แสดงดังต่อไปนี้
$ locate -c index.html
163
ผลลัพธ์ข้างต้นแสดงว่ามีไฟล์ index.html
ทั้งสิ้น 163 ไฟล์
กำหนดจำนวนของผลลัพธ์จากการค้นหา
- คำสั่ง
locate
สามารถกำหนดให้แสดงจำนวนของผลลัพธ์ที่ได้จากการค้นหาโดยใช้ตัวเลือกn
ตัวอย่างเช่น
$ locate -n 5 index.html
/home/kaveepoj/.config/google-chrome/Default/Extensions/pkedcjkdefgpdelpbcmbmeomcjbeemfm/6518.129.0.1_0/cast_setup/index.html
/home/kaveepoj/Desktop/linux_ws/html/index.html
/home/kaveepoj/Desktop/linux_ws/tmp1/index.html
/home/kaveepoj/snap/chromium/396/.config/chromium/Default/Extensions/pkedcjkdefgpdelpbcmbmeomcjbeemfm/6417.1211.0.0_0/cast_setup/index.html
/home/kaveepoj/snap/chromium/412/.config/chromium/Default/Extensions/pkedcjkdefgpdelpbcmbmeomcjbeemfm/6417.1211.0.0_0/cast_setup/index.html
จากตัวอย่างข้างต้น กำหนดให้ตัวเลือก n
เป็น -n 5
ซึ่งทำให้แสดงผลลัพธ์จากการค้นหาจำนวน 5 ผลลัพธ์
แสดงผลการค้นหาไฟล์โดยไม่สนใจตัวอักษร (ignore) กรณีที่เป็นตัวอักษรตัวเล็กหรือตัวใหญ่
- คำสั่ง
locate
สามารถค้นหาที่อยู่ของไฟล์ โดยที่ชื่อสามารถเป็นได้ทั้งตัวเล็กหรือตัวใหญ่ (non-case sensitive) เช่นindex
,INDEX
หรือIndex
จะมีความหมายเดียวกัน โดยใช้ตัวเลือก-i
หรือ--ignore-case
ดังตัวอย่างต่อไปนี้
$ locate -i INDEX.html
- ดังนั้นผลลัพธ์ที่ได้จึงเหมือนกับผลลัพธ์ของคำสั่ง
locate index.html
การกำหนดตัวเลือกที่มากกว่าหนึ่งตัว
- คำสั่งต่าง ๆ อาจมีตัวเลือกให้เลือกเป็นจำนวนมาก ดังนั้นหากต้องการใช้ตัวเลือกมากกว่าหนึ่งตัวสามารถทำได้ดังต่อไปนี้
$ locate -ic Index.html
163
- จากตัวอย่างข้างต้นเลือกใช้ตัวเลือกคือ
i
และc
ดังนั้นจึงสามารถรวมตัวเลือกไว้ด้วยกันเป็น-ic
เป็นต้น - ดังนั้นคำสั่ง
locate -i -c Index.html
และlocate -ic Index.html
จึงมีคำตอบเหมือนกัน