a magnifying glass searching through text

Kuidas Linuxis faile leida: Find ja Grep käskude valdamine

Selles artiklis õpid, kuidas leida faile Linuxis erinevate tööriistade abil.

The “find” command cheatsheet

Otsimine faili nime järgi

“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

Otsimine muutmise kuupäeva järgi

To search for files that were modified today add “-mtime” key to the find command:

$ find /home -mtime 0

Näiteks otsime pilte, mis on muudetud 10 päeva tagasi:

$ find Pictures/ -mtime 10
Pictures/Screenshots/Screenshot from 2023-11-03 10-29-49.png

Otsimine faili tüübi järgi

The “find” command in Linux allows you to search for files based on their type. Here are some examples:

Otsige kaustu: Leidke kõik kataloogid määratud teel, kasutage -type d valikut.

$ find /home -type d

Otsige tavalisi faile: Vastupidi, et leida tavalisi faile, kasutage -type f valikut.

$ find /home -type f -name "*.txt"

Otsige sümboolseid linke: Sümboolsete linkide otsimiseks kasutage -type l valikut.

$ find /home -type l -name "linkname"

Otsige konkreetseid faililaiendeid: Kitsendage oma otsingut, määrates faililaiendid.

$ find /home -type f -name "*.jpg"

Kriteeriumide kombineerimine

You can combine multiple criteria to make your searches more precise. Here’s an example:

Otsige muudetud faile kindla laiendiga: Find files modified in the last 7 days with the extension “.log”.

$ find /var/logs -type f -name "*.log" -mtime -7

Otsige faile, mis on muudetud kuupäevavahemikus: Leidke faile, mis on muudetud ajavahemikus 1. november 2023 kuni 5. november 2023.

$ find /home -newermt 2023-11-01 ! -newermt 2023-11-06

Additional “find” Command Options

Tähtede suuruse ignoreerimine: Kui soovite, et otsing oleks suuruse suhtes tundetu, kasutage -iname valikut.

$ find /home -iname "document*"

Otsingu sügavuse piiramine: Piira otsingu sügavus kindlale tasemele -maxdepth valikuga.

$ find /home -maxdepth 2 -name "*.txt"

Konkreetsete kaustade välistamine: Välista teatud kaustad oma otsingust -not või ! valikuga.

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

Põhisisu otsing: Konkreetse stringi otsimiseks failis kasutage järgmist süntaksit

$ grep "search_string" filename

Replace “search_string” with the text you’re looking for and “filename” with the name of the file.

Otsing mitmes failis: Kui soovite otsida mitmest failist, andke välja wildcard või konkreetne failimuster

$ grep "pattern" /path/to/files/*.txt

Tundlikkuse ignoreerimine: Tehke oma otsing tundlikkuse ignoreerimiseks -i valikuga

$ grep -i "pattern" filename

Kuva ridade numbrid: Kui soovite teada ridade numbreid, kus mustrit leiti, kasutage -n valikut

$ grep -n "pattern" filename

Kuva ainult failinimesid: Kuvamiseks ainult failide nimesid, mis sisaldavad mustrit, kasutage -l valikut

$ grep -l "pattern" /path/to/files/*.txt

Otsi rekursiivselt: Kui soovite otsida mustrit kõigist failidest kaustas ja selle alamkaustades, kasutage -r valikut

$ grep -r "pattern" /path/to/directory

Käivita failid või kaustad: Exclude certain files or directories from your search with the –exclude option

$ grep "pattern" --exclude=*.log /path/to/files/*

Otsi täislauseid: Kasutage -w valikut, et otsida täislauseid, vältides osalisi vasteid

$ grep -w "word" filename

Advanced “grep” Usage

Otsige pööratud vasteid: Pöörake vaste, et kuvada read, mis ei sisalda määratud mustrit, kasutades -v valikut

$ grep -v "pattern" filename

Vastete loendamine: Kui soovite teada, kui palju ridu mustrit sisaldab, kasutage -c valikut:

$ grep -c "pattern" filename

Kuvage ainult vastav tekst: Kuvage ainult tekst, mis vastab mustrile, kasutades -o valikut:

$ grep -o "pattern" filename

Rekursiivne otsing ridade numbritega: Kombineeri valikud põhjalikuks otsinguks, näiteks:

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

Alternatiivsed failide otsimise meetodid

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:

Leia faile Python'i skripti abil

Sometimes, a custom Python script can provide more flexibility in file searching. Here’s a simple example using Python’s os ja 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 “asukoht” 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.

Värskenda asukoha andmebaasi:

$ sudo updatedb

Otsi faile:

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

Leia täidetava faili asukoht:

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

Installi fd:

$ sudo apt-get install fd-find # For Ubuntu/Debian
$ sudo dnf install fd # For Fedora

Otsi faile:

$ fdfind Documents
Phone/Documents
Documents
Documents.zip

Kohandatud skriptid ja alias

Lõpuks võid luua kohandatud shelli skripte või alias'e, et lihtsustada oma failide otsimise protsessi. Näiteks:

# Custom Alias
alias findtxt='find /path/to/search -name "*.txt"'

# Usage
$ findtxt

Kohandatud skriptid ja alias'ed võimaldavad sul luua otseteid oma spetsiifiliste failide otsimise vajaduste jaoks, parandades sinu töövoogu.

Leia faile Linuxis kasutades Bash skripti

Bash skripti loomine ja kasutamine aitab automatiseerida korduvaid failide otsimise ülesandeid Linuxis. Allpool on näide lihtsast Bash skriptist, mis otsib faile antud mustri põhjal:

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

Salvesta see skript faili, näiteks find_files.sh, ja tee see käivitatav järgmise käsuga:

$ chmod +x find_files.sh

Nüüd saad skripti kasutada failide otsimiseks, andes katalooge ja mustreid argumentidena:

$ ./find_files.sh /path/to/search "*.txt"

See skript kontrollib, kas on antud õige arv argumente ja kas määratud kataloog eksisteerib. Seejärel kasutab see find käsku failide otsimiseks antud mustri alusel.

Boonus: kuidas otsida pilte Linuxis

Linuxis on mitmeid tööriistu ja meetodeid piltide otsimiseks, millest igaühel on ainulaadsed omadused ja võimed. Siin on mõned valikud:

“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

Otsige pilte kindla laiendiga:

$ fdfind -e jpg

The “locate” Command

The “locate” command can be used for quick searches, especially if an updated database is maintained:

$ locate '*.jpg'

Ärge unustage regulaarselt uuendada locate andmebaasi viimaste muudatuste jaoks:

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

See avab pildivaataja, kus on faililoend kõigist määratud kataloogis olevatest JPEG-failidest.

Pildimetainfo tööriistade kasutamine

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'

Graafilised failihaldurid

Graafilised failihaldurid nagu Nautilus, Dolphin või Thunar pakuvad sageli otsingufunktsioone. Sa saad navigeerida kausta ja kasutada otsinguriba.

Siltide põhised pildihaldurid

Tools like “digiKam” or “Shotwell” offer image organization based on tags. Tag your images and use these tools to search based on tags.

Kokkuvõte

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!