a magnifying glass searching through text

लिनक्स में फ़ाइलें कैसे खोजें: फाइंड और ग्रेप कमांड में महारत हासिल करें

इस लेख में आप सीखेंगे कि विभिन्न टूल का उपयोग करके लिनक्स में फ़ाइलें कैसे खोजें।

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*"

खोज की सीमित गहराई: -maxdepth विकल्प के साथ खोज की गहराई को एक विशिष्ट स्तर तक सीमित करें।

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

पायथन स्क्रिप्ट का उपयोग करके फ़ाइलें ढूंढें

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

कस्टम स्क्रिप्ट और उपनाम आपको अपने वर्कफ़्लो को बढ़ाते हुए, अपनी विशिष्ट फ़ाइल खोज आवश्यकताओं के लिए शॉर्टकट बनाने की अनुमति देते हैं।

बैश स्क्रिप्ट का उपयोग करके लिनक्स में फ़ाइलें ढूंढें

बैश स्क्रिप्ट बनाने और उपयोग करने से लिनक्स में दोहराए जाने वाले फ़ाइल खोज कार्यों को स्वचालित करने में मदद मिलती है। नीचे एक सरल बैश स्क्रिप्ट का उदाहरण दिया गया है जो दिए गए पैटर्न के आधार पर फ़ाइलों की खोज करता है:

#!/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” 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'

ग्राफ़िकल फ़ाइल प्रबंधक

नॉटिलस, डॉल्फिन, या थूनर जैसे ग्राफ़िकल फ़ाइल प्रबंधक अक्सर खोज कार्यक्षमताएँ प्रदान करते हैं। आप निर्देशिका पर नेविगेट कर सकते हैं और खोज बार का उपयोग कर सकते हैं।

टैग-आधारित छवि आयोजक

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!