15

I'm trying to locate all copies of example.filename on my FreeBSD server. What's the best / easiest / most efficient way to do this?

3
  • 2
    While the answers will be similar, you might want to clarify whether you are using Linux, or FreeBSD. They aren't the same thing. Apr 30, 2009 at 23:17
  • edited title to make the OS more specific
    – Ian
    Apr 30, 2009 at 23:22
  • This should probably be tagged FreeBSD, but I lack the reputation to do so.
    – mikl
    May 5, 2009 at 8:33

7 Answers 7

21
find / -name example.filename
2
  • 2
    Reliable, but slow. Sometimes very slow. Apr 30, 2009 at 23:15
  • 3
    other 'find' flags that may be appropriate, depending on the need: -type f (won't bother with directories or symlinks with the same name) -ls (to show details such as size of the file, e.g. if files have same name but different contents) May 4, 2009 at 4:13
23
locate filename

Much faster than find, if you're running the locate service, and it only finds files that existed at the time updatedb last ran (usualy the night befor under the control of a cron job).

You can run updatedb by hand, but that is even slower than the find cletus suggests, and requires root. I sometimes update the database by hand after installing a bunch of new stuff.

4
  • 3
    Locate only works if you have the service running to build the locate db (forget what it's called). It can also suffer from time delay (in that the file you're looking for may have been added since the last build).
    – cletus
    Apr 30, 2009 at 23:15
  • Edits and comments crossed on the wire. Cool. You are, of course, right on both counts. Apr 30, 2009 at 23:16
  • 4
    If you find yourself doing find / or on any large tree more than once a week, then running the locate service is probably worthwhile, because locate(1) is so much faster. May 1, 2009 at 0:38
  • @cletus Running sudo periodic weekly updates the locate database. Run that after any install or download that may contain the files you desire. Jul 28, 2019 at 5:37
3

Sometimes you want to find files at a specific directory level. In this case it can be convenient to use shell wildcards:

ls /data/*/example.filename

Obviously this only works if you have a rigid directory structure.

2

If you've got locate (aka slocate) installed, then

locate example.filename

locate runs a cron job every night that reindexes all the files on your machine. It's not always up to date for that reason.

1

I sometimes do


  find . | grep example.filename

probably hugely inefficient however.

2
  • Yup, that matches everything then throws away everything except for that one name you were looking for. Instead you can simply do ' find -name "example.filename" ' which does exactly the same stuff without first printing everything and without running an extra grep process. May 2, 2009 at 19:43
  • I've been using this since 2003 lol. And the hell with what everyone else says - the machine is here to make my life easier (not other way around).
    – XO01
    Feb 4, 2022 at 19:24
1

locate

As others mentioned, locate is the fast way to find a file. This command uses an pre-compiled index of the file and folder names. This database of names is searched, rather than crawling through your file system.

locate example.filename 

To be case-insensitive, add -i.

locate -i eXAmPle.FileName

Update locate database

The database used by locate must be up-to-date. After doing an install or download that may contain your desired file, you must update the locate database.

The locate database will eventually be updated automatically by your FreeBSD system. There is a weekly set of chores that includes this task. These chores are listed in the /etc/periodic/weekly/310.locate script.

The easiest safest way to force the early updating of the locate database is to force the weekly set of chores to be done now.

sudo periodic weekly

Or, you could even force all the regular chores to be done. You might want to do this immediately after setting up a new FreeBSD system.

sudo periodic daily weekly monthly

If using other avenues to update the locate database, you may get a message about being unsafe, revealing the names of all your system’s files to any user on the FreeBSD system. Using the periodic route avoids this problem.

0

If you have the locate database up-to-date, then just:

locate example.filename

You must log in to answer this question.

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