3

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 8

14

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.

1
  • This has helped me many times rather than locate Nov 19, 2011 at 19:33
2

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.

1
  • Thanks! 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) Sep 1, 2011 at 15:45
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

1

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.

1

find . -name test.html or you can use wildcards too: find . -name \*test.html

1

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
1

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/user

Wildcards don't work with locate for whatever reason.

1
  • 1
    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. Feb 1, 2011 at 21:07
1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .