How to search for files with Linux?
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.htmlgives my expected result while locate 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?
8 Answers
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 myfileand it will look in the database if updatedb has seen myfile anywhere.
1find 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.
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
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.
find . -name test.html or you can use wildcards too: find . -name \*test.html
You could use the command find, if you wish to do this via a terminal.
Pipelining find and grep will work, otherwise find provides itself some options (like -name and -iname) to do this:
find DIRECTORY -name test.html There are two utilities find and locate. Since others have suggested find, I will talk about locate.
You install a package named mlocate or slocate on older systems. After installing run the command updatedb. locate uses a database to store the path to all of your files, so searching is much faster but it has the disadvantage of going out of date frequently, and populating the database can take a couple of minutes.
Once you have the database populated just run: locate file. If you just use the name of the file it will search for that file starting from /. So you could get /etc/file and /usr/share/file. If you want to just limit it to your home directory use this:
locate file | grep /home/userWildcards don't work with locate for whatever reason.
I don't know the Windows search function, but when you looking for an alternative to | grep * then you can try:
find /* -name "keyword"slocate should also fit into your criteria.
Regards, /dley
More in general
"Zoraya ter Beek, age 29, just died by assisted suicide in the Netherlands. She was physically healthy, but psychologically depressed. It's an abomination that an entire society would actively facilitate, even encourage, someone ending their own life because they had no hope. Th…"