Skip to content Gå til innhold


Using the 'find' command with Linux Bruke 'finne' kommando med Linux

Linux

With hard drives becoming real cheap over the past few years we've grown accustomed to having many gigabytes or sometimes even terabytes of data on our computers. Med harddisker blir virkelig billig de siste årene har vi blitt vant til å ha mange gigabyte eller noen ganger enda terabyte med data på våre datamaskiner. This is when being able to find information on our computers becomes important. Dette er da å kunne finne informasjon på våre datamaskiner blir viktig. Linux has a very handy command line tool very aptly called “find”. Linux har et svært hendig kommandolinje verktøyet svært treffende kalte "finne". I'll share with you some basic features of the “find” command to get you started. Jeg vil dele med deg noen grunnleggende trekk ved "finne"-kommandoen til å komme i gang. I'm quite certain that you will find it pretty useful. Jeg er ganske sikker på at du vil finne det ganske nyttig.

The syntax for using “find” is formed like this: Syntaksen for å bruke "finne" er formet slik:

# find [where to search] [criteria] # Finne [der til søk] [kriterier]

A simple example of its usage is: Et enkelt eksempel på bruk sine er:

# find -name “*.txt” # Find-name "*. txt"
./Documents/articles/doctor_dolittle.txt . / Dokumenter / artikler / doctor_dolittle.txt
./Documents/articles/Installing_Redhat_Linux.txt . / Dokumenter / artikler / Installing_Redhat_Linux.txt
./Desktop/todo_list.txt . / Desktop / todo_list.txt

The above command will search the current directory and all its subdirectories for files with the “txt” extension. Det over kommandoen vil søke gjeldende mappe og alle dens underkataloger for filer med "txt" forlengelse. One thing to note here is that all the parameters for find are completely optional. En ting å merke seg her er at alle parametere for å finne er helt valgfritt. This means that even if you just enter the “find” command and hit the Enter key you will get a result – a list of all the in the current directory and the subdirectories. Dette betyr at selv om du bare skrive inn "finne" kommando og trykk Enter-tasten får du et resultat - en liste over alle i gjeldende katalog og underkataloger. Try it out. Try it out.

If you want to search multiple directories for text files you can list the directories to search in one after another: Hvis du ønsker å søke i flere registre for tekstfiler du kan føre opp kataloger for å søke i en etter en:

# find /home/hobbes/ /home/calvin/ -name “*.txt” # Find / home / Hobbes / / home / calvin /-name "*. txt"
/home/calvin/Documents/articles/doctor_dolittle.txt / home / calvin / Dokumenter / artikler / doctor_dolittle.txt
/home/calvin/Desktop/todo_list.txt / home / calvin / Desktop / todo_list.txt
/home/hobbes/Desktop/articles.txt / home / Hobbes / Desktop / articles.txt
/home/hobbes/data_logging.txt / home / Hobbes / data_logging.txt

You might get a few errors when you run some searches. Du kan få noen feil når du kjører noen søk. This would mostly be related to the lack of permissions. Dette vil hovedsakelig være knyttet til manglende tillatelser. A nice trick to make the output a lot cleaner is to send the errors to “/dev/null”: Et fint triks for å gjøre produksjonen mye renere er å sende feil til "/ dev / null":

# find /home/hobbes/ /home/calvin/ -name “*.txt” 2>/dev/null # Find / home / Hobbes / / home / calvin /-name "*. txt" 2> / dev / null

Another problem you might encounter is with the fact that by default these searches are case sensitive. Et annet problem du kan støte på er med det faktum at som standard disse søkene er case sensitive. So searching for “picasso” will not yield the result “Picasso”. Så søker etter "Picasso" vil ikke gi resultatet "Picasso". You can circumvent that by using the “-iname” option: Du kan omgå det ved å bruke "-iname" alternativet:

# find /home/calvin/ -iname “picasso” # Find / home / calvin /-iname "Picasso"
./Documents/images/picasso.png . / Dokumenter / images / picasso.png
./Documents/articles/The_artist_Picasso.doc . / Dokumenter / artikler / The_artist_Picasso.doc

There are many cool “hacks” for the “find” command. Det er mange kule "hacks" for å "finne" kommandoen. Here are a few. Her er noen. To find files that were modified in the last 45 mins you can do a time-based search: For å finne filer som er blitt endret de siste 45 min kan du gjøre et tidsbasert søk:

# find /home/calvin/ -mmin -45 # Find / home / calvin /-mmin -45
./hello.txt . / hello.txt
./artwork.jpg . / artwork.jpg

Similarly you can search for files that have been modified between 15 and 25 minutes ago: På samme måte kan du søke etter filer som har blitt endret mellom 15 og 25 minutter siden:

# find -mmin +15 -mmin -25 # Finn-mmin +15-mmin -25

You can also control the depth that “find” goes into when searching subdirectories. Du kan også kontrollere dybden som "finner" går inn i når du søker underkataloger. The following command: Følgende kommando:

# find /home/calvin/ -maxdepth 2  -name “*.txt” # Find / home / calvin /-maxdepth 2-name "*. txt"

will only go to the top two levels of the specified directory and look for files with the “txt” file extension. vil bare gå til de to øverste nivåene av den angitte katalogen og se etter filer med "txt" filtypen. Optionally you can also tell “find” to search only two levels lower: Alternativt kan du også si "finne" for å søke kun to nivåer lavere:

# find /home/calvin/ -mindepth 2  -name “*.txt” # Find / home / calvin /-mindepth 2-name "*. txt"
./artexpo.txt . / artexpo.txt
./Documents/project.txt . / Dokumenter / project.txt

As with most commands you can append the output to a file. Som med de fleste kommandoer du kan føye utdataene til en fil. So if you want to log a search you can do the following: Så hvis du vil logge deg et søk kan du gjøre følgende:

# find /home/calvin/ -name “*.txt” > /tmp/search.log # Find / home / calvin /-name "*. txt"> / tmp / search.log

The “find” command can be extremely useful once you get the hang of it. "Finn" kommandoen kan være svært nyttig når du først får taket på det. A book can probably be written on. En bok kan trolig skrevet på. What I've shared with you here is just a taste of the possibilities. Hva jeg har delt med deg her er bare en smak av mulighetene. Take a look at the command's manpage for pointers to other features of “find”. Ta en titt på kommandoen manpage for pekere til andre funksjoner på "finn".

Posted in Posted in Linux Linux . .

Related Posts: Relaterte innlegg:

How to resolve the '/bin/rm: Argument list too long' error Hvordan løse '/ bin / rm: Argumentet listen er for lang "-feil
How to format and mount a USB hard drive in Linux Hvordan du formaterer og montere en USB-harddisk i Linux
Linux command line magic – find and replace Linux kommandolinje magi - finn og erstatt
How to securely clean up data on a hard disk on Linux Slik sikkert rydde opp data på en harddisk på Linux
How to save the output of a Linux command to a file Hvordan lagre utdata fra en Linux-kommandoen til en fil

0 Responses 0 Responses

Stay in touch with the conversation, subscribe to the Holde kontakten med samtalen, abonnere på RSS feed for comments on this post RSS feed for kommentarer til dette innlegget . .



Some HTML is OK Some HTML is OK

or, reply to this post via eller svare på dette innlegget via trackback styrekule . .