Tässä artikkelissa opit, kuinka löytää tiedostoja Linuxissa eri työkaluilla.
Suosituimmat tiedostohaku työkalut Linuxissa
The “find” command cheatsheet
Haku tiedoston nimellä
“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
Etsiminen muokkauspäivämäärän mukaan
To search for files that were modified today add “-mtime” key to the find command:
$ find /home -mtime 0
Esimerkiksi etsitään kuvia, joita on muokattu 10 päivää sitten:
$ find Pictures/ -mtime 10 Pictures/Screenshots/Screenshot from 2023-11-03 10-29-49.png
Etsiminen tiedostotyypin mukaan
The “find” command in Linux allows you to search for files based on their type. Here are some examples:
Etsi hakemistoja: Etsi kaikki hakemistot määritellyn polun sisällä, käytä -type d -vaihtoehtoa.
$ find /home -type d
Etsi tavallisia tiedostoja: Toisaalta, löytääksesi tavallisia tiedostoja, käytä -type f -vaihtoehtoa.
$ find /home -type f -name "*.txt"
Etsi symbolisia linkkejä: Etsiäksesi symbolisia linkkejä, käytä -type l -vaihtoehtoa.
$ find /home -type l -name "linkname"
Etsi tiettyjä tiedostopäätteitä: Rajoita hakua määrittämällä tiedostopäätteet.
$ find /home -type f -name "*.jpg"
Kombinoi kriteerit
You can combine multiple criteria to make your searches more precise. Here’s an example:
Hae muokattuja tiedostoja tietyllä laajennuksella: Find files modified in the last 7 days with the extension “.log”.
$ find /var/logs -type f -name "*.log" -mtime -7
Hae tiedostoja, jotka on muokattu tietyn päivämäärävälin sisällä: Etsi tiedostoja, jotka on muokattu 1. marraskuuta 2023 ja 5. marraskuuta 2023 välillä.
$ find /home -newermt 2023-11-01 ! -newermt 2023-11-06
Additional “find” Command Options
Huomioimatta kirjainkokoa: Jos haluat haun olevan kirjainkooltaan riippumaton, käytä -iname-vaihtoehtoa.
$ find /home -iname "document*"
Haku syvyyden rajoittaminen: Rajoita hakusyvyyttä tiettyyn tasoon -maxdepth-vaihtoehdolla.
$ find /home -maxdepth 2 -name "*.txt"
Tiettyjen hakemistojen poissulkeminen: Poissulje tietyt hakemistot haustasi -not- tai !-vaihtoehdolla.
$ 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:
Perushaku sisällön perusteella: Etsi erityistä merkkijonoa tiedostosta, käytä seuraavaa syntaksia
$ grep "search_string" filename
Replace “search_string” with the text you’re looking for and “filename” with the name of the file.
Hae useista tiedostoista: Jos haluat etsiä useista tiedostoista, anna jokerimerkki tai tietty tiedostomalli
$ grep "pattern" /path/to/files/*.txt
Kokonaishaku: Tee haustasi kirjainkoolla huomiotta -i-vaihtoehdolla
$ grep -i "pattern" filename
Näytä rivinumerot: Jos haluat tietää rivinumerot, joilla kuvio löytyy, käytä -n-vaihtoehtoa
$ grep -n "pattern" filename
Näytä vain tiedostojen nimet: Jos haluat näyttää vain tiedostojen nimet, jotka sisältävät kaavan, käytä -l-vaihtoehtoa
$ grep -l "pattern" /path/to/files/*.txt
Etsi rekursiivisesti: Jos haluat etsiä kaavaa kaikista tiedostoista hakemistossa ja sen alihakemistoissa, käytä -r-vaihtoehtoa
$ grep -r "pattern" /path/to/directory
Poista tiedostot tai hakemistot: Exclude certain files or directories from your search with the –exclude option
$ grep "pattern" --exclude=*.log /path/to/files/*
Etsi kokonaisia sanoja: Käytä -w-vaihtoehtoa etsiäksesi kokonaisia sanoja, estäen osittaiset osumat
$ grep -w "word" filename
Advanced “grep” Usage
Etsi käänteisiä osumia: Käännä osuma näyttämään rivit, jotka eivät sisällä määriteltyä kaavaa käyttämällä -v vaihtoehtoa
$ grep -v "pattern" filename
Osumien laskeminen: Jos haluat tietää, kuinka monta riviä sisältää kaavan, käytä -c vaihtoehtoa:
$ grep -c "pattern" filename
Näytä vain vastaava teksti: Näytä vain teksti, joka vastaa kaavaa -o vaihtoehdolla:
$ grep -o "pattern" filename
Rekursiivinen haku rivin numeroilla: Yhdistä vaihtoehtoja kattavaa hakua varten, esimerkiksi:
$ 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.
Vaihtoehtoiset tiedostohakumenetelmät
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:
Etsi tiedostoja Python-skriptillä
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 “paikanna” 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.
Päivitä Locate-tietokanta:
$ sudo updatedb
Etsi tiedostoja:
$ 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.
Löydä suoritettavan tiedoston sijainti:
$ 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.
Asenna fd:
$ sudo apt-get install fd-find # For Ubuntu/Debian $ sudo dnf install fd # For Fedora
Etsi tiedostoja:
$ fdfind Documents Phone/Documents Documents Documents.zip
Mukautetut skriptit ja alias
Viimeiseksi voit luoda mukautettuja shell-skriptejä tai alias-nimiä tiedostohakuprosessisi sujuvoittamiseksi. Esimerkiksi:
# Custom Alias alias findtxt='find /path/to/search -name "*.txt"' # Usage $ findtxt
Mukautetut skriptit ja alias mahdollistavat oikopolkujen luomisen erityisiin tiedostohakutarpeisiisi, parantaen työnkulkuasi.
Etsi tiedostoja Linuxissa käyttäen Bash-skriptiä
Bash-skriptin luominen ja käyttäminen auttaa automatisoimaan toistuvia tiedostohakutehtäviä Linuxissa. Alla on esimerkki yksinkertaisesta Bash-skriptistä, joka etsii tiedostoja annetun mallin perusteella:
#!/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
Tallenna tämä skripti tiedostoon, esimerkiksi find_files.sh, ja tee siitä suoritettava seuraavalla komennolla:
$ chmod +x find_files.sh
Nyt voit käyttää skriptiä etsiäksesi tiedostoja antamalla hakemiston ja kaavan argumentteina:
$ ./find_files.sh /path/to/search "*.txt"
Tämä skripti tarkistaa, onko oikea määrä argumentteja annettu ja onko määritetty hakemisto olemassa. Se käyttää sitten find-komentoa etsiäkseen tiedostoja annetun kaavan perusteella.
Bonus: kuinka etsiä kuvia Linuxissa
Linuxissa on useita työkaluja ja menetelmiä kuvien etsimiseen, joista jokaisella on ainutlaatuisia ominaisuuksia ja kykyjä. Tässä on muutama vaihtoehto:
“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
Etsi kuvia tietyllä tiedostopäätteellä:
$ fdfind -e jpg
The “locate” Command
The “locate” command can be used for quick searches, especially if an updated database is maintained:
$ locate '*.jpg'
Muista päivittää locate-tietokanta säännöllisesti viimeisimmille muutoksille:
$ 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")
Tämä avaa kuvakatseluohjelman, jossa on tiedostoluettelo kaikista JPEG-tiedostoista määritellyssä hakemistossa.
Kuvan metatietotyökalujen käyttäminen
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'
Graafiset tiedostonhallintaohjelmat
Graafiset tiedostohallintaohjelmat kuten Nautilus, Dolphin tai Thunar tarjoavat usein hakutoimintoja. Voit navigoida hakemistoon ja käyttää hakupalkkia.
Tunnistepohjaiset kuvajärjestäjät
Tools like “digiKam” or “Shotwell” offer image organization based on tags. Tag your images and use these tools to search based on tags.
Yhteenveto
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!





