How to search for files with Linux?
Solution 1
The command is find, like this
find . -iname '*hellofiles*'
you say find ·location· -iname means not case sensitive and in the '' is a regular expression if you wish.
find /home/user -iname '*zip' will find you all the zip files in /home/user
If you want a faster lookup you can use locate, there is a utility which scanns the disc regulary, like every week or day dependin on how it is setup
locate myfile
and it will look in the database if updatedb has seen myfile anywhere.
Solution 2
In case of packages: dpkg -L packagename
or if you know that it is in a particular folder, add the use of grep, like:
dpkg -L packagename | grep foldername
For example you just installed Chromium and you need to know where is the bin
dpkg -L chromium-browser | grep bin
Solution 3
find
has a number of options that allow it to find files by name, regular expressions or even more complex criteria such as size or ownership. E.g.
find . -iname '*.txt'
See also the example section in the linked manual page.
That said, find
has the distinct disadvantage of searching the filesystem each time that you call it. The locate
utility, in its many variations, on the other hand, uses a regularly-updated database of the files in your system.
Solution 4
find ./ -name "filename"
or you can do something like find ./ -name "*.jar"
to find all the files with the .jar extension. You can also do find ./ -name "*.jar" | xargs grep -n 'main'
to find all the .jar files that contain a main in them.
Solution 5
find . -name test.html
or you can use wildcards too: find . -name \*test.html
Related videos on Youtube
![Niklas Rosencrantz](https://i.stack.imgur.com/M1X8W.jpg?s=256&g=1)
Niklas Rosencrantz
I'm as simple as possible but not any simpler.
Updated on September 17, 2022Comments
-
Niklas Rosencrantz almost 2 years
Do you know a good way to find a file or content by name recursively similar to the ms-windows function search with Linux? I can do
find . | grep test.html
suspecting it's not the best. Thanks for any answer.For instance, running
find . | grep terms.html
gives my expected result whilelocate terms.html
doesn't even though locate gets updated db - it might have to do with that it's an USB stick the file is on? -
Rich Homolka over 13 years
locate
does do shell globs a.k.a. wildcards. You may need to quote your pattern then. You can also use the-r
flag to use regular expressions. -
Niklas Rosencrantz almost 13 yearsThanks! The command
find . -iname privacy.html
gives the expected result while locate doesn't (it's a file privacy.html I serched after on a USB memory) -
Niklas Rosencrantz over 12 yearsThis has helped me many times rather than locate