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

เริ่มต้นใช้งานคำสั่ง find ใน Linux (คู่มือครบถ้วน)

คู่มือนี้สอน find - คำสั่งค้นหาไฟล์ทรงพลังที่สุดใน Linux รองรับเงื่อนไขหลากหลาย: ชื่อ, ขนาด, เวลา, Permission, Owner

Syntax พื้นฐาน

find [path] [expression]
# path = เริ่มค้นหาที่ไหน (default = .)
# expression = เงื่อนไขการค้นหา

ค้นหาตามชื่อ (Name)

find -name "*.txt"                    # ไฟล์ .txt ในปัจจุบัน + ย่อย (Case sensitive)
find -iname "*.txt"                   # Case insensitive
find /home -name "report.pdf"         # ระบุ Path
find /home/user1 /home/user2 -name "*.log"  # หลาย Path

ค้นหาตามประเภท (Type)

find -type f      # File ปกติ
find -type d      # Directory
find -type l      # Symbolic Link
find -type b      # Block Device
find -type c      # Character Device
find -type p      # Named Pipe (FIFO)
find -type s      # Socket

ค้นหาตามขนาด (Size)

find -size +100M     # > 100 MB
find -size -10k      # < 10 KB
find -size 100M      # = 100 MB (Exact - ใช้ block 512 bytes)
# Suffix: c=bytes, k=KiB, M=MiB, G=GiB

ค้นหาตามเวลา (Time) - วัน

find -mtime -7       # Modified < 7 วัน (New)
find -mtime +30      # Modified > 30 วัน (Old)
find -mtime 7        # Modified = 7 วันพอดี
find -atime -1       # Accessed < 1 วัน
find -ctime +90      # Status Changed > 90 วัน

ค้นหาตามเวลา (Time) - นาที

find -mmin -60       # Modified < 60 นาที
find -mmin +1440     # Modified > 1 วัน (1440 นาที)

ค้นหาตาม Permission / Owner

find -perm 644           # Exact permission
find -perm -644          # At least these bits
find -perm /222          # Any of these bits (Write for any)
find -user username      # Owner
find -group groupname    # Group
find -uid 1000           # UID
find -nouser             # ไม่มี Owner (Orphan)
find -nogroup            # ไม่มี Group

ควบคุมความลึก (Depth)

find -maxdepth 2 -name "*.txt"   # ลงลึกสูงสุด 2 ระดับ
find -mindepth 2 -name "*.txt"   # เริ่มจากระดับ 2 (ข้าม Current + ระดับ 1)

Action (ทำอะไรกับผลลัพธ์)


# พื้นฐาน
find -name "*.tmp" -delete              # ลบทันที (เร็วที่สุด)
find -name "*.log" -exec rm {} +        # รันคำสั่ง (รวม Args)
find -name "*.log" -exec rm {} \;       # รันคำสั่ง (แยก Args - ช้ากว่า)

# แสดงข้อมูล
find -name "*.conf" -ls                 # ls -lids format
find -name "*.conf" -printf "%p %s\n"   # Custom format: Path Size

# Copy/Move
find -name "*.bak" -exec cp {} /backup/ \;
find -name "*.bak" -exec mv {} /backup/ \;

กรอง Error (Permission Denied)

find / -name "*.conf" 2>/dev/null
# หรือ
find / -name "*.conf" 2>&1 | grep -v "Permission denied"

ตัวอย่าง Real-world


# หาไฟล์ Log เก่า > 30 วัน ลบ
find /var/log -name "*.log" -mtime +30 -delete

# หาไฟล์ใหญ่ > 100MB ใน Home
find ~ -type f -size +100M -exec ls -lh {} \;

# หา Script ที่ Executable
find /usr/local/bin -type f -perm /111

# หา Symlink ชำรุด
find -xtype l

# หาไฟล์ที่เปลี่ยนแปลงใน 1 ชั่วโมงล่าสุด
find /etc -mmin -60 -type f

เครื่องมือทันสมัย: fd

  • fd - Rust-based, เร็วกว่า, สีสัน, Default ignore .gitignore, Pattern ง่ายกว่า
  • fd "pattern" แทน find -name "*pattern*"
  • ติดตั้ง: apt install fd-find (binary: fdfind)

find เป็นพื้นฐานของ Linux Administration เรียนรู้ Expression ต่างๆ จะช่วยจัดการไฟล์ได้อย่างมีประสิทธิภาพ