V tomto článku sa dozviete, ako nájsť súbory v systéme Linux pomocou rôznych nástrojov.
Najpopulárnejšie nástroje na vyhľadávanie súborov v systéme Linux
The “find” command cheatsheet
Vyhľadávanie podľa názvu súboru
“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
Vyhľadávanie podľa dátumu úpravy
To search for files that were modified today add “-mtime” key to the find command:
$ find /home -mtime 0
Napríklad umožňuje nájsť obrázky, ktoré boli upravené pred 10 dňami:
$ find Pictures/ -mtime 10 Pictures/Screenshots/Screenshot from 2023-11-03 10-29-49.png
Vyhľadávanie podľa typu súboru
The “find” command in Linux allows you to search for files based on their type. Here are some examples:
Hľadať adresáre: Ak chcete nájsť všetky adresáre v zadanej ceste, použite voľbu -type d.
$ find /home -type d
Hľadať bežné súbory: Naopak, ak chcete nájsť bežné súbory, použite možnosť -type f.
$ find /home -type f -name "*.txt"
Vyhľadajte symbolické odkazy: Ak chcete vyhľadať symbolické odkazy, použite voľbu -type l.
$ find /home -type l -name "linkname"
Vyhľadajte konkrétne prípony súborov: Zúžte vyhľadávanie zadaním prípon súborov.
$ find /home -type f -name "*.jpg"
Kombinovanie kritérií
You can combine multiple criteria to make your searches more precise. Here’s an example:
Vyhľadajte upravené súbory so špecifickou príponou: Find files modified in the last 7 days with the extension “.log”.
$ find /var/logs -type f -name "*.log" -mtime -7
Vyhľadajte súbory upravené medzi rozsahom dátumov: Nájdite súbory upravené medzi 1. novembrom 2023 a 5. novembrom 2023.
$ find /home -newermt 2023-11-01 ! -newermt 2023-11-06
Additional “find” Command Options
Ignorovanie veľkosti písmen: Ak chcete, aby vyhľadávanie nerozlišovalo veľké a malé písmená, použite možnosť -iname.
$ find /home -iname "document*"
Obmedzená hĺbka vyhľadávania: Obmedzte hĺbku vyhľadávania na určitú úroveň pomocou voľby -maxdepth.
$ find /home -maxdepth 2 -name "*.txt"
Okrem špecifických adresárov: Vylúčte určité adresáre z vyhľadávania pomocou -not alebo ! možnosť.
$ 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:
Základné vyhľadávanie obsahu: Ak chcete vyhľadať konkrétny reťazec v súbore, použite nasledujúcu syntax
$ grep "search_string" filename
Replace “search_string” with the text you’re looking for and “filename” with the name of the file.
Hľadať vo viacerých súboroch: Ak chcete prehľadávať viacero súborov, zadajte zástupný znak alebo konkrétny vzor súboru
$ grep "pattern" /path/to/files/*.txt
Vyhľadávanie bez rozlišovania malých a veľkých písmen: Pomocou voľby -i upravte vyhľadávanie bez rozlišovania malých a veľkých písmen
$ grep -i "pattern" filename
Zobraziť čísla riadkov: Ak chcete poznať čísla riadkov, kde sa vzor nachádza, použite voľbu -n
$ grep -n "pattern" filename
Zobraziť iba názvy súborov: Ak chcete zobraziť iba názvy súborov obsahujúcich vzor, použite voľbu -l
$ grep -l "pattern" /path/to/files/*.txt
Hľadať rekurzívne: Ak chcete vyhľadať vzor vo všetkých súboroch v adresári a jeho podadresároch, použite voľbu -r
$ grep -r "pattern" /path/to/directory
Vylúčiť súbory alebo adresáre: Exclude certain files or directories from your search with the –exclude option
$ grep "pattern" --exclude=*.log /path/to/files/*
Hľadať celé slová: Použite voľbu -w na vyhľadávanie celých slov, čím zabránite čiastočným zhodám
$ grep -w "word" filename
Advanced “grep” Usage
Hľadať obrátené zhody: Invertujte zhodu, aby sa zobrazili riadky, ktoré neobsahujú zadaný vzor pomocou voľby -v
$ grep -v "pattern" filename
Počítanie zápasov: Ak chcete vedieť, koľko riadkov obsahuje vzor, použite voľbu -c:
$ grep -c "pattern" filename
Zobrazenie len zhodného textu: Zobraziť iba text, ktorý sa zhoduje so vzorom s voľbou -o:
$ grep -o "pattern" filename
Rekurzívne vyhľadávanie s číslami riadkov: Kombinujte možnosti pre komplexné vyhľadávanie, napríklad:
$ 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.
Alternatívne metódy vyhľadávania súborov
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:
Nájdite súbory pomocou skriptu Python
Sometimes, a custom Python script can provide more flexibility in file searching. Here’s a simple example using Python’s os a 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 “lokalizovať” 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.
Aktualizujte lokalizačnú databázu:
$ sudo updatedb
Hľadať súbory:
$ 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.
Nájsť spustiteľné umiestnenie:
$ 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.
Nainštalujte fd:
$ sudo apt-get install fd-find # For Ubuntu/Debian $ sudo dnf install fd # For Fedora
Hľadať súbory:
$ fdfind Documents Phone/Documents Documents Documents.zip
Vlastné skripty a alias
Nakoniec si môžete vytvoriť vlastné skripty shellu alebo aliasy na zefektívnenie procesu vyhľadávania súborov. Napríklad:
# Custom Alias alias findtxt='find /path/to/search -name "*.txt"' # Usage $ findtxt
Vlastné skripty a aliasy vám umožňujú vytvárať skratky pre vaše špecifické potreby vyhľadávania súborov, čím sa zlepšuje váš pracovný tok.
Nájdite súbory v systéme Linux pomocou skriptu Bash
Vytvorenie a používanie bash skriptu pomáha automatizovať opakujúce sa úlohy vyhľadávania súborov v systéme Linux. Nižšie je uvedený príklad jednoduchého skriptu Bash, ktorý vyhľadáva súbory na základe poskytnutého vzoru:
#!/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
Uložte tento skript do súboru, napríklad find_files.sh, a urobte ho spustiteľným pomocou nasledujúceho príkazu:
$ chmod +x find_files.sh
Teraz môžete použiť skript na vyhľadávanie súborov zadaním adresára a vzoru ako argumentov:
$ ./find_files.sh /path/to/search "*.txt"
Tento skript kontroluje, či je zadaný správny počet argumentov a či existuje zadaný adresár. Potom použije príkaz find na vyhľadanie súborov na základe daného vzoru.
Bonus: ako prehľadávať obrázky v Linuxe
V systéme Linux existuje niekoľko nástrojov a metód na vyhľadávanie obrázkov, z ktorých každý ponúka jedinečné funkcie a možnosti. Tu je niekoľko možností:
“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
Vyhľadajte obrázky s konkrétnou príponou:
$ fdfind -e jpg
The “locate” Command
The “locate” command can be used for quick searches, especially if an updated database is maintained:
$ locate '*.jpg'
Nezabudnite pravidelne aktualizovať databázu lokácií kvôli najnovším zmenám:
$ 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")
Otvorí sa prehliadač obrázkov so zoznamom všetkých súborov JPEG v zadanom adresári.
Používanie nástrojov na metaúdaje obrázkov
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'
Správcovia grafických súborov
Grafické správcovia súborov ako Nautilus, Dolphin alebo Thunar často poskytujú funkcie vyhľadávania. Môžete prejsť do adresára a použiť panel vyhľadávania.
Organizátori obrázkov na základe značiek
Tools like “digiKam” or “Shotwell” offer image organization based on tags. Tag your images and use these tools to search based on tags.
Zhrnutie
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!






