Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบ >> Linux

วิธีหาไฟล์ใน Linux ด้วยคำสั่ง Find

คำสั่ง Linux find เป็นหนึ่งในคำสั่งที่ important และ handy ที่สุดใน Linux systems มันสามารถ find files บน Linux PC ของคุณได้ based on pretty much whatever conditions และ variables ที่คุณตั้ง คุณสามารถหาไฟล์โดย permissions, users, groups, file type, date, size และ other possible criteria ด้วยคำสั่ง find นี่คือวิธีหาไฟล์ใน Linux ด้วยคำสั่ง find

คำสั่ง find available บน most Linux distro by default จึงไม่ต้อง install package สำหรับมัน

หาไฟล์ด้วยชื่อใน Current Directories

วิธีที่ชัดเจนที่สุดคือหาตามชื่อ หาไฟล์ด้วยชื่อใน current directory รัน:

find . -name photo.png

หากต้องการหาไฟล์ด้วยชื่อที่มีทั้ง capital และ small letters รัน:

find . -iname photo.png

หากต้องการหาไฟล์ใน root directory ใส่ sudo นำหน้า search ซึ่งจะให้ permissions ทั้งหมดที่จำเป็น และ symbol / บอก Linux ให้ search ใน root directory สุดท้าย expression -print แสดง directories ของ search results หากหา Gzip จะพิมพ์:

sudo find / -name gzip -print

หาไฟล์ภายใต้ Specific Directory

หากต้องการหาไฟล์ภายใต้ specific directory อย่าง "/home" รัน:

find /home -name filename.txt

หากต้องการหาไฟล์ที่มี extension ".txt" ภายใต้ "/home" directory รัน:

find /home -name "*.txt"

หาไฟล์ชื่อ "test.txt" ภายใต้ multiple directories อย่าง "/home" และ "/opt" รัน:

find /home /opt -name test.txt

หา hidden files ใน "/home" directory รัน:

find /home -name ".*"

หา single file ชื่อ "test.txt" และ remove มัน รัน:

find /home -type f -name test.txt -exec rm -f {}

หา empty files ทั้งหมดภายใต้ "/opt" directory รัน:

find /opt -type f -empty

หา Directories ด้วย Name

หากต้องการหา directories ทั้งหมดชื่อ "testdir" ภายใต้ "/home" directory รัน:

find /home -type d -name testdir

หา empty directories ทั้งหมดภายใต้ "/home" รัน:

find /home -type d -empty

หาไฟล์ด้วย Permissions เฉพาะ

คำสั่ง find ใช้หาไฟล์ด้วย specific permission ด้วย option perm

หาไฟล์ทั้งหมดที่ permissions เป็น "777" ใน "/home" directory รัน:

find /home -type f -perm 0777 -print

หาไฟล์ทั้งหมดที่ไม่มี permission "777" รัน:

find . -type f ! -perm 777

หา read-only files ทั้งหมด รัน:

find /home -perm /u=r

หา executable files ทั้งหมด รัน:

find /home -perm /a=x

หา sticky bit set files ทั้งหมดที่ permissions เป็น "553" รัน:

find /home -perm 1553

หา SUID set files ทั้งหมด รัน:

find /home -perm /u=s

หาไฟล์ permissions "777" และเปลี่ยนเป็น "700" รัน:

find /home -type f -perm 0777 -print -exec chmod 700 {} \;

หาไฟล์และ Directories ตาม Date และ Time

หาไฟล์ภายใต้ "/opt" ที่ modified 20 days earlier รัน:

find /opt -mtime 20

หาไฟล์ภายใต้ "/opt" ที่ accessed twenty days earlier รัน:

find /opt -atime 20

หาไฟล์ภายใต้ "/opt" ที่ modified more than 30 days earlier และ less than 50 days after:

find /opt -mtime +30 -mtime -50

หาไฟล์ภายใต้ "/opt" ที่ changed ใน last two hours รัน:

find /opt -cmin -120

หาไฟล์และ Directories ตาม Size

หาไฟล์ 10MB ทั้งหมดภายใต้ "/home" directory รัน:

find /home -size 10M

หาไฟล์ภายใต้ "/home" ที่ greater than 10MB และ less than 50MB รัน:

find /home -size +10M -size -50M

หาไฟล์ ".mp4" ภายใต้ "/home" ที่ more than 10MB และ delete ด้วย single command รัน:

find /home -type f -name *.mp4 -size +10M -exec rm {} \;

เห็นได้ว่า find command incredibly useful สำหรับ administering system, looking through directories หาไฟล์ และ generally pruning virtual directory tree ใน Linux หากชอบบทความ Linux นี้ อย่าลืม check out เนื้อหา Linux อื่นๆ เช่น วิธีใช้ scp command securely transfer files, วิธีใช้ nnn as file manager in terminal, และวิธี fix broken packages