U ovom članku ćete naučiti kako pronaći datoteke u Linuxu koristeći različite alate.
Najpopularniji alati za pretraživanje datoteka u Linuxu
The “find” command cheatsheet
Pretraživanje po imenu datoteke
“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
Pretraživanje prema datumu izmjene
To search for files that were modified today add “-mtime” key to the find command:
$ find /home -mtime 0
Na primjer, pronađimo slike koje su izmijenjene prije 10 dana:
$ find Pictures/ -mtime 10 Pictures/Screenshots/Screenshot from 2023-11-03 10-29-49.png
Pretraživanje prema vrsti datoteke
The “find” command in Linux allows you to search for files based on their type. Here are some examples:
Pretraži direktorije: Da biste pronašli sve direktorije unutar određenog puta, upotrijebite opciju -type d.
$ find /home -type d
Pretraži redovne datoteke: Suprotno tome, za lociranje redovnih datoteka, upotrijebite opciju -type f.
$ find /home -type f -name "*.txt"
Pretraži simboličke veze: Da biste pretražili simboličke veze, upotrijebite opciju -type l.
$ find /home -type l -name "linkname"
Pretraži specifične ekstenzije datoteka: Sužavajte pretraživanje određivanjem ekstenzija datoteka.
$ find /home -type f -name "*.jpg"
Kombiniranje kriterija
You can combine multiple criteria to make your searches more precise. Here’s an example:
Pretraži izmijenjene datoteke s određenom ekstenzijom: Find files modified in the last 7 days with the extension “.log”.
$ find /var/logs -type f -name "*.log" -mtime -7
Pretraži datoteke izmijenjene između raspona datuma: Pronađite datoteke izmijenjene između 1. studenog 2023. i 5. studenog 2023.
$ find /home -newermt 2023-11-01 ! -newermt 2023-11-06
Additional “find” Command Options
Ignoriranje osjetljivosti na velika i mala slova: Ako želite da pretraga bude neosjetljiva na velika i mala slova, upotrijebite opciju -iname.
$ find /home -iname "document*"
Ograničavanje dubine pretraživanja: Ograničite dubinu pretraživanja na određenu razinu s opcijom -maxdepth.
$ find /home -maxdepth 2 -name "*.txt"
Isključivanje određenih direktorija: Isključite određene direktorije iz svog pretraživanja koristeći opciju -not ili !.
$ 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:
Osnovno pretraživanje sadržaja: Da biste pretražili određeni niz u datoteci, upotrijebite sljedeću sintaksu
$ grep "search_string" filename
Replace “search_string” with the text you’re looking for and “filename” with the name of the file.
Pretraživanje u više datoteka: Ako želite pretraživati kroz više datoteka, navedite zamjenski znak ili određeni obrazac datoteke
$ grep "pattern" /path/to/files/*.txt
Pretraživanje bez obzira na velika i mala slova: Učinite svoje pretraživanje neovisnim o velikim i malim slovima s opcijom -i
$ grep -i "pattern" filename
Prikaži brojeve redaka: Ako želite znati brojeve redaka gdje se obrazac nalazi, upotrijebite opciju -n
$ grep -n "pattern" filename
Prikaži samo imena datoteka: Da biste prikazali samo imena datoteka koje sadrže obrazac, upotrijebite opciju -l
$ grep -l "pattern" /path/to/files/*.txt
Pretraživanje rekurzivno: Ako želite pretraživati obrazac u svim datotekama unutar direktorija i njegovih poddirektorija, upotrijebite opciju -r
$ grep -r "pattern" /path/to/directory
Isključi datoteke ili direktorije: Exclude certain files or directories from your search with the –exclude option
$ grep "pattern" --exclude=*.log /path/to/files/*
Pretraži cijele riječi: Upotrijebite opciju -w za pretraživanje cijelih riječi, sprječavajući djelomična podudaranja
$ grep -w "word" filename
Advanced “grep” Usage
Pretraživanje inverznih podudaranja: Invertirajte podudaranje da biste prikazali redove koji ne sadrže navedeni obrazac koristeći opciju -v
$ grep -v "pattern" filename
Brojanje podudaranja: Ako želite znati koliko redova sadrži obrazac, koristite opciju -c:
$ grep -c "pattern" filename
Prikazivanje samo podudarajućeg teksta: Prikažite samo tekst koji se podudara s obrascem koristeći opciju -o:
$ grep -o "pattern" filename
Rekurzivno pretraživanje s brojevima redova: Kombinirajte opcije za sveobuhvatno pretraživanje, na primjer:
$ 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.
Alternativne metode pretraživanja datoteka
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:
Pronađite datoteke pomoću Python skripte
Sometimes, a custom Python script can provide more flexibility in file searching. Here’s a simple example using Python’s os i 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 “lociraj” 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.
Ažurirajte bazu podataka lociranja:
$ sudo updatedb
Pretraži datoteke:
$ 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.
Pronađi lokaciju izvršne datoteke:
$ 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.
Instalirajte fd:
$ sudo apt-get install fd-find # For Ubuntu/Debian $ sudo dnf install fd # For Fedora
Pretraži datoteke:
$ fdfind Documents Phone/Documents Documents Documents.zip
Prilagođeni skripti i aliasi
Na kraju, možete stvoriti prilagođene shell skripte ili aliasa kako biste pojednostavili svoj proces pretraživanja datoteka. Na primjer:
# Custom Alias alias findtxt='find /path/to/search -name "*.txt"' # Usage $ findtxt
Prilagođene skripte i aliasi omogućuju vam stvaranje prečaca za vaše specifične potrebe pretraživanja datoteka, poboljšavajući vaš radni tok.
Pronađite datoteke u Linuxu koristeći Bash skriptu
Kreiranje i korištenje bash skripte pomaže u automatizaciji ponavljajućih zadataka pretraživanja datoteka u Linuxu. Ispod je primjer jednostavne Bash skripte koja pretražuje datoteke na temelju zadanog uzorka:
#!/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
Spremite ovaj skript u datoteku, na primjer, find_files.sh, i učinite je izvršnom sljedećom naredbom:
$ chmod +x find_files.sh
Sada možete koristiti skript za pretraživanje datoteka pružajući direktorij i obrazac kao argumente:
$ ./find_files.sh /path/to/search "*.txt"
Ovaj skript provjerava je li pružen ispravan broj argumenata i postoji li navedeni direktorij. Zatim koristi naredbu find za pretraživanje datoteka na temelju zadanog obrasca.
Bonus: kako pretraživati slike u Linuxu
Na Linuxu postoji nekoliko alata i metoda za pretraživanje slika, od kojih svaki nudi jedinstvene značajke i mogućnosti. Evo nekoliko opcija:
“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
Pretražujte slike s određenim ekstenzijama:
$ fdfind -e jpg
The “locate” Command
The “locate” command can be used for quick searches, especially if an updated database is maintained:
$ locate '*.jpg'
Zapamtite redovito ažurirati bazu podataka locate za nedavne promjene:
$ 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")
Ovo otvara preglednik slika s popisom svih JPEG datoteka u određenom direktoriju.
Korištenje alata za metapodatke slika
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'
Grafički upravitelji datoteka
Grafički upravitelji datotekama poput Nautilusa, Dolfina ili Thunara često pružaju funkcionalnosti pretraživanja. Možete se kretati do direktorija i koristiti traku za pretraživanje.
Organizatori slika temeljeni na oznakama
Tools like “digiKam” or “Shotwell” offer image organization based on tags. Tag your images and use these tools to search based on tags.
Sažetak
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!





