Šajā rakstā jūs uzzināsiet, kā atrast failus Linux, izmantojot dažādus rīkus.
Vispopulārākie failu meklēšanas rīki Linux
The “find” command cheatsheet
Meklēšana pēc faila nosaukuma
“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
Meklēšana pēc modifikācijas datuma
To search for files that were modified today add “-mtime” key to the find command:
$ find /home -mtime 0
Piemēram, meklēsim attēlus, kas tika modificēti pirms 10 dienām:
$ find Pictures/ -mtime 10 Pictures/Screenshots/Screenshot from 2023-11-03 10-29-49.png
Meklēšana pēc faila veida
The “find” command in Linux allows you to search for files based on their type. Here are some examples:
Meklēt direktorijas: Lai atrastu visas direktorijas noteiktā ceļā, izmantojiet -type d opciju.
$ find /home -type d
Meklēt parastās failus: Savukārt, lai atrastu parastos failus, izmantojiet -type f opciju.
$ find /home -type f -name "*.txt"
Meklēt simboliskos saites: Lai meklētu simboliskos saites, izmantojiet -type l opciju.
$ find /home -type l -name "linkname"
Meklēt specifiskas failu paplašinājumus: Sašauriniet meklēšanu, norādot failu paplašinājumus.
$ find /home -type f -name "*.jpg"
Kombinējot kritērijus
You can combine multiple criteria to make your searches more precise. Here’s an example:
Meklēt modificētus failus ar konkrētu paplašinājumu: Find files modified in the last 7 days with the extension “.log”.
$ find /var/logs -type f -name "*.log" -mtime -7
Meklēt failus, kas modificēti noteiktā datumu diapazonā: Atrodiet failus, kas modificēti no 2023. gada 1. novembra līdz 2023. gada 5. novembrim.
$ find /home -newermt 2023-11-01 ! -newermt 2023-11-06
Additional “find” Command Options
Ignorējot lielo un mazo burtu jūtīgumu: Ja vēlaties, lai meklēšana būtu nejūtīga pret lielajiem un mazajiem burtiem, izmantojiet -iname opciju.
$ find /home -iname "document*"
Meklēšanas dziļuma ierobežošana: Ierobežojiet meklēšanas dziļumu līdz konkrētam līmenim, izmantojot -maxdepth opciju.
$ find /home -maxdepth 2 -name "*.txt"
Noteiktu direktoriju izslēgšana: Izslēdziet noteiktas direktorijas no savas meklēšanas, izmantojot -not vai ! opciju.
$ 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:
Pamatinformācijas meklēšana: Lai meklētu konkrētu virkni failā, izmantojiet sekojošo sintaksi
$ grep "search_string" filename
Replace “search_string” with the text you’re looking for and “filename” with the name of the file.
Meklēšana vairākos failos: Ja vēlaties meklēt vairākos failos, norādiet aizvietotāju vai konkrētu faila paraugu
$ grep "pattern" /path/to/files/*.txt
Meklēšana bez lielajiem burtiem: Padariet savu meklēšanu par bez lielajiem burtiem, izmantojot -i opciju
$ grep -i "pattern" filename
Rādīt rindu numurus: Ja vēlaties uzzināt rindu numurus, kur atrodams paraugs, izmantojiet -n opciju
$ grep -n "pattern" filename
Rādīt tikai failu nosaukumus: Lai rādītu tikai failu nosaukumus, kas satur paraugu, izmantojiet -l opciju
$ grep -l "pattern" /path/to/files/*.txt
Meklēt rekursīvi: Ja vēlaties meklēt paraugu visos failos direktorijā un tās apakšdirektorijās, izmantojiet -r opciju
$ grep -r "pattern" /path/to/directory
Izslēgt failus vai direktorijas: Exclude certain files or directories from your search with the –exclude option
$ grep "pattern" --exclude=*.log /path/to/files/*
Meklēt veselas vārdus: Izmantojiet -w opciju, lai meklētu veselas vārdus, novēršot daļējas atbilstības
$ grep -w "word" filename
Advanced “grep” Usage
Meklēt apgrieztos atbilstības: Apgrieziet atbilstību, lai parādītu rindas, kas nesatur norādīto paraugu, izmantojot -v opciju
$ grep -v "pattern" filename
Atbilstību skaitīšana: Ja vēlaties uzzināt, cik rindas satur paraugu, izmantojiet -c opciju:
$ grep -c "pattern" filename
Rādīt tikai atbilstošo tekstu: Rādīt tikai tekstu, kas atbilst paraugam, izmantojot -o opciju:
$ grep -o "pattern" filename
Rekursīva meklēšana ar rindas numuriem: Apvienojiet opcijas visaptverošai meklēšanai, piemēram:
$ 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īvas failu meklēšanas metodes
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:
Atrodiet failus, izmantojot Python skriptu
Sometimes, a custom Python script can provide more flexibility in file searching. Here’s a simple example using Python’s os un 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 “atrod” 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.
Atjaunināt Locate datu bāzi:
$ sudo updatedb
Meklēt failus:
$ 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.
Atrast izpildāmā faila atrašanās vietu:
$ 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.
Instalēt fd:
$ sudo apt-get install fd-find # For Ubuntu/Debian $ sudo dnf install fd # For Fedora
Meklēt failus:
$ fdfind Documents Phone/Documents Documents Documents.zip
Pielāgotas skriptu un alias
Visbeidzot, jūs varat izveidot pielāgotus shell skriptus vai alias, lai vienkāršotu failu meklēšanas procesu. Piemēram:
# Custom Alias alias findtxt='find /path/to/search -name "*.txt"' # Usage $ findtxt
Pielāgotie skripti un alias ļauj jums izveidot īsceļus jūsu specifiskajām failu meklēšanas vajadzībām, uzlabojot jūsu darba plūsmu.
Meklēt failus Linux, izmantojot Bash skriptu
Bash skripta izveide un izmantošana palīdz automatizēt atkārtotas failu meklēšanas uzdevumus Linux. Zemāk ir piemērs vienkāršam Bash skriptam, kas meklē failus, pamatojoties uz norādīto paraugu:
#!/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
Saglabājiet šo skriptu failā, piemēram, find_files.sh, un padariet to izpildāmu ar sekojošo komandu:
$ chmod +x find_files.sh
Tagad jūs varat izmantot skriptu, lai meklētu failus, norādot direktoriju un paraugu kā argumentus:
$ ./find_files.sh /path/to/search "*.txt"
Šis skripts pārbauda, vai ir norādīts pareizs argumentu skaits un vai norādītā direktorija pastāv. Pēc tam tas izmanto find komandu, lai meklētu failus, pamatojoties uz norādīto paraugu.
Bonuss: kā meklēt attēlus Linux sistēmā
Linux sistēmā ir vairāki rīki un metodes attēlu meklēšanai, katrs piedāvā unikālas funkcijas un iespējas. Šeit ir daži varianti:
“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
Meklējiet attēlus ar konkrētu paplašinājumu:
$ fdfind -e jpg
The “locate” Command
The “locate” command can be used for quick searches, especially if an updated database is maintained:
$ locate '*.jpg'
Atcerieties regulāri atjaunināt locate datu bāzi nesenām izmaiņā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")
Tas atver attēlu skatītāju ar failu sarakstu, kas satur visus JPEG failus norādītajā direktorijā.
Izmantojot attēlu metadatu rīkus
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'
Grafiskie failu pārvaldnieki
Grafiskie failu pārvaldnieki, piemēram, Nautilus, Dolphin vai Thunar, bieži nodrošina meklēšanas funkcionalitāti. Jūs varat pārvietoties uz direktoriju un izmantot meklēšanas joslu.
Tagu balstīti attēlu organizatori
Tools like “digiKam” or “Shotwell” offer image organization based on tags. Tag your images and use these tools to search based on tags.
Kopsavilkums
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!





