Finding files with find · การค้นหาไฟล์ด้วย find
Searching the whole tree
lsshows one folder.findsearches a folder and everything inside it, however deep:
.means "start in the current folder".findlists every file and folder it can reach.
ค้นหาทั้งต้นไม้
lsแสดงโฟลเดอร์หนึ่ง folder.findค้นหาโฟลเดอร์ และทุกสิ่งที่อยู่ภายใน ไม่ว่าลึกแค่ไหน:
find .
.หมายความว่า "เริ่มจากโฟลเดอร์ปัจจุบัน"findรายการทุกไฟล์และโฟลเดอร์ที่เข้าถึงได้
Find by name
- Usually you want files matching a pattern. Use
-name:
- This finds every
.txtfile in the tree — even ones buried three folders down. - Quote the pattern (
"*.txt") so the shell passes it tofinduntouched.
ค้นหาตามชื่อ
- โดยปกติคุณต้องการไฟล์ที่ตรงกับ pattern ใช้
-name:
find . -name "*.txt"
- สิ่งนี้จะค้นหาไฟล์
.txtทุกไฟล์ในต้นไม้ — รวมถึง那些 buried สามโฟลเดอร์ลง down - ให้ใส่เครื่องหมายอัญประกาศรอบ pattern (
"*.txt") เพื่อให้ shell ส่งต่อไปยังfindโดยไม่เปลี่ยนรูป
Why it is handy
- "Where did I put that file?" —
findanswers it in one line. - Combined with pipes, you can act on the results — for example count them with
wc -l.
ทำไมมันถึง有用
- "ฉันวางไฟล์นั้นไว้ที่ไหน?" —
findตอบ它以 one line - รวมกับท่อ คุณสามารถดำเนินการกับผลลัพธ์ — ตัวอย่างเช่น นับจำนวน them with
wc -l
Common mistakes
find . -name "*.txt"searches by name from the current folder down.- Quote the pattern so the shell does not expand it first.
ข้อผิดพลาดที่พบบ่อย
find . -name "*.txt"ค้นหาตามชื่อจากโฟลเดอร์ปัจจุบันลง down- ให้ใส่เครื่องหมายอัญประกาศรอบ pattern เพื่อให้ shell ไม่ขยายมันก่อน
find walks the tree · find เดินผ่านต้นไม้
find searches the whole tree below a directory. · find ค้นหาทั้งต้นไม้ใต้ไดเร็กทอรีหนึ่ง
Find every .txt file, even deep inside subfolders, with find . -name "*.txt". (. means "start from here".) · ค้นหาไฟล์ .txt ทุกไฟล์, แม้จะลึกภายในโฟลเดอร์ย่อย, ด้วย find . -name "*.txt". (. หมายถึง "เริ่มจากที่นี่".)
You can also start the search from a subfolder. Find just the .txt files inside docs (and below) with find docs -name "*.txt". · คุณสามารถเริ่มการค้นห从中จาก โฟลเดอร์ย่อย. ค้นหาเฉพาะไฟล์ .txt ภายใน docs (และด้านล่าง) ด้วย find docs -name "*.txt"