a magnifying glass searching through text

Πώς να βρείτε αρχεία στο Linux: Mastering Find and Grep Commands

Σε αυτό το άρθρο θα μάθετε πώς να βρίσκετε αρχεία στο 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

Παράβλεψη ευαισθησίας πεζών-κεφαλαίων: Εάν θέλετε η αναζήτηση να είναι χωρίς διάκριση πεζών-κεφαλαίων, χρησιμοποιήστε την επιλογή -inname.

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

Βρείτε αρχεία χρησιμοποιώντας το σενάριο 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

Η δημιουργία και η χρήση ενός σεναρίου 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!