+5 votes
65 views
in Linux by (242k points)
reopened
Linux file search - this is how it works

1 Answer

+3 votes
by (1.6m points)
 
Best answer

Search files via find
Search files via locate


Linux offers several ways to find files and folders. We'll show you two important commands you'll need to do this..

image image

Finding files on a Linux system can be complicated. A wide variety of tools are available, each with different advantages and disadvantages. We'll introduce you to two of the most important commands for finding Linux files below.

Search files via find

The find command is available by default on Linux and searches for files in a directory and all of its subdirectories. A disadvantage of find , however, is that the system first has to search through the folders, which can take some time if there are many folders and subfolders in the named directory. In our step-by-step instructions , we will show you the console commands that you will need most frequently.

1st step:

image

First, a starting point must be passed to find find <Startpunkt> .
find
searches in the current directory.
find /Arbeit
looks in the directory "/ work".
find /Arbeit /Freizeit
searches in the directories "/ work" and "/ leisure".
find ~
looks in your user directory.
find /
looking everywhere..

2nd step:

image

Next, the file name you are looking for must be entered.
find <Startpunkt> -name "<Dateiname>"
or alternatively
find <Startpunkt> -iname "<Dateiname>"
if upper and lower case should be ignored. For example, searches for all files with the name " Hausarbeit.abc " in the directory " / home / heise-tipps-tricks ".
find /home/heise-tipps-tricks -name "Hausarbeit.abc"

3rd step:

image

Wildcards or placeholders can also be used in the search term . Use:
? for exactly one arbitrary character
* for any number of characters
find / -name "*.pdf"
examined all the PDF files in the system is, for example, " Test.jpg ", " test.txt " (case insensitive) and similar files . finds all files with the ending ".txt" whose names are exactly three characters long. For example " abc.txt " or " xyz.txt ".
find / -iname "test.*"

find /home/heise-tipps-tricks -name "???.txt"

If you are interested in the numerous other possibilities of the find command under Linux, you can call the associated help page with man find in the terminal .

Search files via locate

Another console command for searching files in Linux is locate . The locate command is very similar to the find command. Nevertheless, they distinguish one essential detail: While find searches for the files as soon as the command is called, locate has already created a database with all files. The locate method is therefore many times faster , but does not always provide correct results because the database can be out of date. We'll show you how to use locate in our step-by-step instructions :

1st step:

image

If locate is not available by default on your Linux operating system, you can sudo apt-get install mlocate install it later using the command . When asked, enter your password and confirm it with [Enter] ..

2nd step:

image

As already mentioned, locate works on the basis of a database . As a rule, this is updated daily. However, if you are looking for a file that you have just created or moved, you will need to manually update the database. You can sudo updatedb force the manual update with . If you are looking for an older file, this step is unnecessary, but it doesn't hurt either.

3rd step:

image

Now you can use locate as follows: locate "<Suchbegriff>" or even locate -i "<Suchbegriff>" if you don't want to differentiate between upper and lower case . The output is the paths to the files found .

4th step:

image

As with find, wildcards can also be used in the search term.
? for exactly one character
* for any number of characters
Please note, however, that locate returns the absolute paths to the files. So if you want to search for all files with the prefix "housework" with any ending, use:
locate "*Hausarbeit.*"
The first " * " indicates that all characters can be anything beforehand.
locate "Test.*"
Lists all files with the name " Test " and any extension.
locate "*.pdf"
Shows you all files with the ending " .pdf "

If you want to find out more about the locate command, you can call up the associated help page with man locate in the terminal .


...