a magnifying glass searching through text

როგორ მოვძებნოთ ფაილები Linux-ში: Find and 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

მოძებნეთ ფაილები, რომლებიც შეცვლილია თარიღის დიაპაზონს შორის: იპოვნეთ ფაილები, რომლებიც შეცვლილია 2023 წლის 1 ნოემბრიდან 2023 წლის 5 ნოემბრამდე.

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

კონკრეტული დირექტორიების გამოკლებით: გამორიცხეთ გარკვეული დირექტორიები თქვენი ძიებიდან -არა ან ! ვარიანტი.

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

ძიება მრავალ ფაილში: თუ გსურთ მოძებნოთ მრავალი ფაილი, მიაწოდეთ wildcard ან კონკრეტული ფაილის ნიმუში

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

დააინსტალირეთ 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!