ในบทความนี้ คุณจะได้เรียนรู้วิธีค้นหาไฟล์ใน Linux โดยใช้เครื่องมือต่างๆ
เครื่องมือค้นหาไฟล์ยอดนิยมใน Linux
The “find” command cheatsheet
ค้นหาตามชื่อไฟล์
“Find” is one of the most popular tools to search files in Linux. When you know the file name you can search for it using command below. Replace “/home” with a folder to search through and “filename.txt” with the file name.
$ find /home -name filename.txt
To search the current folder for a file replace “/home” with a dot (.) symbol:
$ find . -name Documents.zip ./Documents.zip
When you do not know the exact file name use the regular expression to look for a match. The asterisk (*) symbol matches any character set after the “Documents” keyword.
$ find /home -name "Documents*" /home/eugene/Documents.zip /home/eugene/Documents /home/eugene/Phone/Documents
ค้นหาตามวันที่แก้ไข
To search for files that were modified today add “-mtime” key to the find command:
$ find /home -mtime 0
ตัวอย่างเช่น ให้ค้นหารูปภาพที่ถูกแก้ไขเมื่อ 10 วันที่แล้ว:
$ find Pictures/ -mtime 10 Pictures/Screenshots/Screenshot from 2023-11-03 10-29-49.png
ค้นหาตามประเภทไฟล์
The “find” command in Linux allows you to search for files based on their type. Here are some examples:
ค้นหาไดเรกทอรี: หากต้องการค้นหาไดเร็กทอรีทั้งหมดภายในพาธที่ระบุ ให้ใช้อ็อพชัน -type d
$ find /home -type d
ค้นหาไฟล์ปกติ: ในทางกลับกัน หากต้องการค้นหาไฟล์ปกติ ให้ใช้อ็อพชัน -type f
$ find /home -type f -name "*.txt"
ค้นหาลิงก์สัญลักษณ์: หากต้องการค้นหาลิงก์สัญลักษณ์ ให้ใช้ตัวเลือก -type l
$ find /home -type l -name "linkname"
ค้นหานามสกุลไฟล์เฉพาะ: จำกัดการค้นหาของคุณให้แคบลงโดยการระบุนามสกุลไฟล์
$ find /home -type f -name "*.jpg"
เกณฑ์การรวม
You can combine multiple criteria to make your searches more precise. Here’s an example:
ค้นหาไฟล์ที่ถูกแก้ไขด้วยนามสกุลเฉพาะ: Find files modified in the last 7 days with the extension “.log”.
$ find /var/logs -type f -name "*.log" -mtime -7
ค้นหาไฟล์ที่ถูกแก้ไขระหว่างช่วงวันที่: ค้นหาไฟล์ที่ถูกแก้ไขระหว่างวันที่ 1 พฤศจิกายน 2023 ถึง 5 พฤศจิกายน 2023
$ find /home -newermt 2023-11-01 ! -newermt 2023-11-06
Additional “find” Command Options
ละเว้นความอ่อนไหวของตัวพิมพ์: หากคุณต้องการให้การค้นหาไม่คำนึงถึงขนาดตัวพิมพ์ ให้ใช้ตัวเลือก -iname
$ find /home -iname "document*"
การจำกัดความลึกของการค้นหา: จำกัดความลึกในการค้นหาให้อยู่ในระดับที่ระบุด้วยตัวเลือก -maxdeep
$ find /home -maxdepth 2 -name "*.txt"
ไม่รวมไดเรกทอรีเฉพาะ: แยกบางไดเร็กทอรีออกจากการค้นหาของคุณโดยใช้ -not หรือ ! ตัวเลือก.
$ find /home -type f -name "*.txt" -not -path "/home/user/exclude/*"
Remember, mastering the “find” command can significantly enhance your ability to navigate and manage files in a Linux environment. Experiment with different options to tailor your searches according to specific criteria.
Search by file content using “grep” command
When it comes to finding specific content within files, the “grep” command becomes your go-to tool. It allows you to search for patterns or text strings within one or multiple files. Here’s a quick cheatsheet to get you started:
การค้นหาเนื้อหาพื้นฐาน: หากต้องการค้นหาสตริงเฉพาะในไฟล์ ให้ใช้ไวยากรณ์ต่อไปนี้
$ grep "search_string" filename
Replace “search_string” with the text you’re looking for and “filename” with the name of the file.
ค้นหาในหลายไฟล์: หากคุณต้องการค้นหาหลายไฟล์ ให้ระบุไวด์การ์ดหรือรูปแบบไฟล์เฉพาะ
$ grep "pattern" /path/to/files/*.txt
การค้นหาที่ไม่คำนึงถึงขนาดตัวพิมพ์: ทำให้การค้นหาของคุณไม่คำนึงถึงขนาดตัวพิมพ์ด้วยตัวเลือก -i
$ grep -i "pattern" filename
หมายเลขบรรทัดที่แสดง: หากคุณต้องการทราบหมายเลขบรรทัดที่พบรูปแบบ ให้ใช้ตัวเลือก -n
$ grep -n "pattern" filename
แสดงเฉพาะชื่อไฟล์: หากต้องการแสดงเฉพาะชื่อไฟล์ที่มีรูปแบบ ให้ใช้ตัวเลือก -l
$ grep -l "pattern" /path/to/files/*.txt
ค้นหาซ้ำ: หากคุณต้องการค้นหารูปแบบในไฟล์ทั้งหมดภายในไดเร็กทอรีและไดเร็กทอรีย่อย ให้ใช้ตัวเลือก -r
$ grep -r "pattern" /path/to/directory
ไม่รวมไฟล์หรือไดเร็กทอรี: Exclude certain files or directories from your search with the –exclude option
$ grep "pattern" --exclude=*.log /path/to/files/*
ค้นหาทั้งคำ: ใช้ตัวเลือก -w เพื่อค้นหาทั้งคำ เพื่อป้องกันการจับคู่บางส่วน
$ grep -w "word" filename
Advanced “grep” Usage
ค้นหาการแข่งขันแบบกลับหัว: กลับการจับคู่เพื่อแสดงบรรทัดที่ไม่มีรูปแบบที่ระบุโดยใช้ตัวเลือก -v
$ grep -v "pattern" filename
การนับการแข่งขัน: หากคุณต้องการทราบว่ามีรูปแบบกี่บรรทัด ให้ใช้ตัวเลือก -c:
$ grep -c "pattern" filename
การแสดงข้อความที่ตรงกันเท่านั้น: แสดงเฉพาะข้อความที่ตรงกับรูปแบบด้วยตัวเลือก -o:
$ grep -o "pattern" filename
การค้นหาแบบเรียกซ้ำด้วยหมายเลขบรรทัด: รวมตัวเลือกสำหรับการค้นหาที่ครอบคลุม เช่น:
$ grep -r -n "pattern" /path/to/directory
Mastering the “grep” command allows you to swiftly locate specific content within files, making it an indispensable tool for efficient file exploration in a Linux environment. Experiment with different options to tailor your searches and uncover the information you need.
วิธีการค้นหาไฟล์ทางเลือก
In addition to the powerful “find” and “grep” commands, there are alternative methods for searching files in a Linux environment. Let’s explore a few:
ค้นหาไฟล์โดยใช้สคริปต์ Python
Sometimes, a custom Python script can provide more flexibility in file searching. Here’s a simple example using Python’s os และ fnmatch modules:
import os
from fnmatch import fnmatch
def find_files(directory, pattern):
for root, dirs, files in os.walk(directory):
for file in files:
if fnmatch(file, pattern):
print(os.path.join(root, file))
# Usage
find_files('/path/to/search', '*.txt')
This script walks through the directory and its subdirectories, matching files based on the specified pattern. Customize the find_files function to suit your specific search criteria.
The “locate” Command
The “ค้นหา” command is another efficient way to find files on a Linux system. It uses a pre-built index, making searches faster compared to the “find” command. However, keep in mind that the index needs regular updates to include recent changes.
อัปเดตฐานข้อมูลค้นหา:
$ sudo updatedb
ค้นหาไฟล์:
$ locate filename.txt
Using the “which” Command
If you’re looking for the location of an executable in your system’s PATH, the “which” command can help.
ค้นหาตำแหน่งที่ปฏิบัติการได้:
$ which executable_name
Using “fd” – A Fast and User-friendly Alternative to “find”
The “fd” command is a fast and user-friendly alternative to “find.” It comes with a simplified syntax and colorized output.
ติดตั้ง fd:
$ sudo apt-get install fd-find # For Ubuntu/Debian $ sudo dnf install fd # For Fedora
ค้นหาไฟล์:
$ fdfind Documents Phone/Documents Documents Documents.zip
สคริปต์และนามแฝงที่กำหนดเอง
สุดท้ายนี้ คุณสามารถสร้างเชลล์สคริปต์หรือนามแฝงแบบกำหนดเองเพื่อปรับปรุงกระบวนการค้นหาไฟล์ของคุณได้ ตัวอย่างเช่น:
# Custom Alias alias findtxt='find /path/to/search -name "*.txt"' # Usage $ findtxt
สคริปต์และนามแฝงที่กำหนดเองช่วยให้คุณสร้างทางลัดสำหรับความต้องการในการค้นหาไฟล์เฉพาะของคุณ ซึ่งช่วยปรับปรุงขั้นตอนการทำงานของคุณ
ค้นหาไฟล์ใน Linux โดยใช้สคริปต์ Bash
การสร้างและการใช้สคริปต์ทุบตีช่วยให้การค้นหาไฟล์ซ้ำๆ ใน Linux เป็นไปโดยอัตโนมัติ ด้านล่างนี้เป็นตัวอย่างของสคริปต์ Bash ธรรมดาที่ค้นหาไฟล์ตามรูปแบบที่ให้ไว้:
#!/bin/bash # Bash script to find files based on a pattern if [ $# -ne 2 ]; then echo "Usage: $0 <directory> <pattern>" exit 1 fi directory=$1 pattern=$2 # Check if the directory exists if [ ! -d "$directory" ]; then echo "Error: Directory $directory not found." exit 1 fi # Use find command to search for files find "$directory" -name "$pattern" -print
บันทึกสคริปต์นี้ลงในไฟล์ เช่น find_files.sh และทำให้สามารถเรียกทำงานได้ด้วยคำสั่งต่อไปนี้:
$ chmod +x find_files.sh
ตอนนี้คุณสามารถใช้สคริปต์เพื่อค้นหาไฟล์โดยระบุไดเร็กทอรีและรูปแบบเป็นอาร์กิวเมนต์:
$ ./find_files.sh /path/to/search "*.txt"
สคริปต์นี้จะตรวจสอบว่าระบุจำนวนอาร์กิวเมนต์ที่ถูกต้องหรือไม่ และมีไดเร็กทอรีที่ระบุอยู่หรือไม่ จากนั้นใช้คำสั่ง find เพื่อค้นหาไฟล์ตามรูปแบบที่กำหนด
โบนัส: วิธีค้นหารูปภาพใน Linux
บน Linux มีเครื่องมือและวิธีการค้นหารูปภาพหลายวิธี โดยแต่ละวิธีนำเสนอคุณสมบัติและความสามารถเฉพาะตัว นี่คือตัวเลือกบางส่วน:
“find” Command with File Type Filtering:
The standard “find” command can be used to locate image files based on their file types. For example, to find all JPEG files in a directory and its subdirectories:
$ find /path/to/images -type f -name "*.jpg"
The “fdfind” Command:
The “fdfind” command, a fast and user-friendly alternative to “find,” can also be used to search for image files. Install it if you haven’t already:
$ sudo apt-get install fd-find # For Ubuntu/Debian $ sudo dnf install fd # For Fedora
ค้นหาภาพที่มีนามสกุลเฉพาะ:
$ fdfind -e jpg
The “locate” Command
The “locate” command can be used for quick searches, especially if an updated database is maintained:
$ locate '*.jpg'
อย่าลืมอัปเดตฐานข้อมูลการค้นหาเป็นประจำสำหรับการเปลี่ยนแปลงล่าสุด:
$ sudo updatedb
“grep” Command for Specific Image Names:
If you have a specific naming convention for your image files, you can use the “grep” command to search for them:
$ grep -r 'pattern' /path/to/images
Replace ‘pattern’ with a part of the image file name.
“file” Command
The “file” command can identify file types, helping you filter out image files:
$ file /path/to/images/*
Look for lines that include “image” to identify image files.
“feh” Image Viewer with Filelist
If you have a list of image files and want to view them, the “feh” image viewer allows you to create a filelist:
$ feh -f $(find /path/to/images -type f -name "*.jpg")
ซึ่งจะเปิดโปรแกรมดูรูปภาพพร้อมรายการไฟล์ของไฟล์ JPEG ทั้งหมดในไดเร็กทอรีที่ระบุ
การใช้เครื่องมือข้อมูลเมตาของรูปภาพ
If your images have metadata, you can use tools like “exiftool” to search based on image properties:
$ exiftool -filename -r /path/to/images | grep 'search_term'
ตัวจัดการไฟล์แบบกราฟิก
ตัวจัดการไฟล์แบบกราฟิกเช่น Nautilus, Dolphin หรือ Thunar มักจะมีฟังก์ชันการค้นหามาให้ คุณสามารถนำทางไปยังไดเร็กทอรีและใช้แถบค้นหา
ตัวจัดระเบียบรูปภาพตามแท็ก
Tools like “digiKam” or “Shotwell” offer image organization based on tags. Tag your images and use these tools to search based on tags.
สรุป
In conclusion, mastering file search on Linux involves understanding and combining various commands, tools, and scripting techniques. Whether you’re a command-line enthusiast or prefer graphical interfaces, this guide equips you with the knowledge to navigate and search for files seamlessly in a Linux environment. Happy file hunting!






