a magnifying glass searching through text

كيفية البحث عن الملفات في نظام التشغيل Linux: إتقان أوامر البحث وgrep

ستتعلم في هذه المقالة كيفية العثور على الملفات في 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*"

الحد من عمق البحث: قم بتقييد عمق البحث بمستوى معين باستخدام خيار -maxعمق.

$ 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.

تثبيت فد:

$ 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 script

يساعد إنشاء برنامج نصي 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"

يتحقق هذا البرنامج النصي من توفير العدد الصحيح من الوسائط وما إذا كان الدليل المحدد موجودًا. ثم يستخدم أمر البحث للبحث عن الملفات بناءً على النمط المحدد.

المكافأة: كيفية البحث من خلال الصور في نظام التشغيل 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!