I denne artikkelen vil du lære hvordan du finner filer i Linux ved hjelp av forskjellige verktøy.
De mest populære filsøkeverktøyene i Linux
The “find” command cheatsheet
Søker etter filnavn
“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
Søker etter endringsdato
To search for files that were modified today add “-mtime” key to the find command:
$ find /home -mtime 0
La oss for eksempel finne bilder som ble endret for 10 dager siden:
$ find Pictures/ -mtime 10 Pictures/Screenshots/Screenshot from 2023-11-03 10-29-49.png
Søker etter filtype
The “find” command in Linux allows you to search for files based on their type. Here are some examples:
Søk etter kataloger: For å finne alle kataloger innenfor en spesifisert bane, bruk alternativet -type d.
$ find /home -type d
Søk etter vanlige filer: Omvendt, for å finne vanlige filer, bruk -type f-alternativet.
$ find /home -type f -name "*.txt"
Søk etter symbolske lenker: For å søke etter symbolske lenker, bruk alternativet -type l.
$ find /home -type l -name "linkname"
Søk etter spesifikke filutvidelser: Begrens søket ved å spesifisere filtypene.
$ find /home -type f -name "*.jpg"
Kombinasjonskriterier
You can combine multiple criteria to make your searches more precise. Here’s an example:
Søk etter modifiserte filer med en spesifikk utvidelse: Find files modified in the last 7 days with the extension “.log”.
$ find /var/logs -type f -name "*.log" -mtime -7
Søk etter filer endret mellom en datoperiode: Finn filer endret mellom 1. november 2023 og 5. november 2023.
$ find /home -newermt 2023-11-01 ! -newermt 2023-11-06
Additional “find” Command Options
Ignorerer store og små bokstaver: Hvis du vil at søket skal skille mellom store og små bokstaver, bruk alternativet -iname.
$ find /home -iname "document*"
Begrensende søkedybde: Begrens søkedybden til et spesifikt nivå med -maxdepth-alternativet.
$ find /home -maxdepth 2 -name "*.txt"
Ekskluderer spesifikke kataloger: Ekskluder visse kataloger fra søket ditt ved å bruke -not eller ! alternativ.
$ 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:
Grunnleggende innholdssøk: For å søke etter en bestemt streng i en fil, bruk følgende syntaks
$ grep "search_string" filename
Replace “search_string” with the text you’re looking for and “filename” with the name of the file.
Søk i flere filer: Hvis du vil søke gjennom flere filer, oppgi et jokertegn eller et spesifikt filmønster
$ grep "pattern" /path/to/files/*.txt
søk uten store og små bokstaver: Gjør søket uten store og små bokstaver med alternativet -i
$ grep -i "pattern" filename
Vis linjenumre: Hvis du vil vite linjenumrene der mønsteret er funnet, bruk alternativet -n
$ grep -n "pattern" filename
Vis bare filnavn: For å vise bare navnene på filene som inneholder mønsteret, bruk alternativet -l
$ grep -l "pattern" /path/to/files/*.txt
Søk rekursivt: Hvis du vil søke etter et mønster i alle filene i en katalog og dens underkataloger, bruk alternativet -r
$ grep -r "pattern" /path/to/directory
Ekskluder filer eller kataloger: Exclude certain files or directories from your search with the –exclude option
$ grep "pattern" --exclude=*.log /path/to/files/*
Søk etter hele ord: Bruk alternativet -w for å søke etter hele ord, og forhindrer delvise treff
$ grep -w "word" filename
Advanced “grep” Usage
Søk etter inverterte treff: Inverter samsvaret for å vise linjer som ikke inneholder det spesifiserte mønsteret ved å bruke -v-alternativet
$ grep -v "pattern" filename
Telle kamper: Hvis du vil vite hvor mange linjer som inneholder mønsteret, bruk alternativet -c:
$ grep -c "pattern" filename
Viser kun samsvarende tekst: Vis bare teksten som samsvarer med mønsteret med -o-alternativet:
$ grep -o "pattern" filename
Rekursivt søk med linjenumre: Kombiner alternativer for et omfattende søk, for eksempel:
$ 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.
Alternative metoder for filsøk
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:
Finn filer ved hjelp av Python-skript
Sometimes, a custom Python script can provide more flexibility in file searching. Here’s a simple example using Python’s os og 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 “lokalisere” 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.
Oppdater lokaliseringsdatabasen:
$ sudo updatedb
Søk etter filer:
$ 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.
Finn kjørbar plassering:
$ 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.
Installer fd:
$ sudo apt-get install fd-find # For Ubuntu/Debian $ sudo dnf install fd # For Fedora
Søk etter filer:
$ fdfind Documents Phone/Documents Documents Documents.zip
Egendefinerte skript og alias
Til slutt kan du lage tilpassede shell-skript eller aliaser for å strømlinjeforme filsøkeprosessen. For eksempel:
# Custom Alias alias findtxt='find /path/to/search -name "*.txt"' # Usage $ findtxt
Egendefinerte skript og aliaser lar deg lage snarveier for dine spesifikke filsøkebehov, noe som forbedrer arbeidsflyten din.
Finn filer i Linux ved å bruke Bash-skript
Å lage og bruke et bash-skript hjelper til med å automatisere repeterende filsøkeoppgaver i Linux. Nedenfor er et eksempel på et enkelt Bash-skript som søker etter filer basert på et gitt mønster:
#!/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
Lagre dette skriptet til en fil, for eksempel find_files.sh, og gjør det kjørbart med følgende kommando:
$ chmod +x find_files.sh
Nå kan du bruke skriptet til å søke etter filer ved å oppgi katalogen og mønsteret som argumenter:
$ ./find_files.sh /path/to/search "*.txt"
Dette skriptet sjekker om riktig antall argumenter er oppgitt og om den angitte katalogen eksisterer. Den bruker deretter find-kommandoen til å søke etter filer basert på det gitte mønsteret.
Bonus: hvordan søke gjennom bilder i Linux
På Linux er det flere verktøy og metoder for bildesøk, som hver tilbyr unike funksjoner og muligheter. Her er noen alternativer:
“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
Søk etter bilder med en bestemt utvidelse:
$ fdfind -e jpg
The “locate” Command
The “locate” command can be used for quick searches, especially if an updated database is maintained:
$ locate '*.jpg'
Husk å oppdatere lokaliseringsdatabasen regelmessig for nylige endringer:
$ 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")
Dette åpner en bildeviser med en filliste over alle JPEG-filer i den angitte katalogen.
Bruke bildemetadataverktøy
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'
Grafiske filbehandlere
Grafiske filbehandlere som Nautilus, Dolphin eller Thunar tilbyr ofte søkefunksjoner. Du kan navigere til katalogen og bruke søkefeltet.
Tag-baserte bildeorganisatorer
Tools like “digiKam” or “Shotwell” offer image organization based on tags. Tag your images and use these tools to search based on tags.
Sammendrag
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!






