People ask me this all the time in varying degrees. They have installed Linux, and maybe it’s command-line only, or maybe they’ve shelled in to their pretty GUI server for the first time, and are looking for a file. How to find it without Konqueror or Nautilus (forgoing the X redirection and all that)…?
There are two main options I want to mention here: “find
” and “locate
“.
Find
find
is the common *nix tool that I learned of years ago (in my early Unix days) and still use to this day, because it is so common. I can feel fairly confident that if I write a script to use find it will likely be present — and behave the same way — across multiple distros and platforms (including Solaris and others).
Although find
has a bunch of options and uses, I’m only focusing on its ability to look for a file by name. You can choose to look for things by size, type, age, etc., if you wish.
So here’s a simple example:
find / -name myFile.txt
…this will run find
and search from the root of the disk (“/”), looking for something named “myFile.txt
“. You can change the starting point to anything you want, for instance if you were in your home directory but you wanted to search in the /tmp
directory you would change it to:
find /tmp -name myFile.txt
…and if you wanted to search your local directory (and subdirectories), you would use:
find ./ -name myFile.txt
…and note that wild cards can be used on the filename as well. Actually, the filename part is a “regular expression” (RegEx), so you can get pretty crazy with search patterns.
Locate
I love the locate
tool. I cannot believe it isn’t default on Suse or others. No prob, just install the “findutils-locate
” package for your distro. (Note that when you install this package, it will include a “find
” utility that is similar-but-different from the one I mentioned above, that is part of the “findutils
” package and is default on most systems. These two packages can co-exist fine, but be mindful of which one is first in your path.)
The locate
tool relies on a database to search for your files and such. Once installed, you can create the database manually, or you can let it be created automatically by the default cron job, that would likely run overnight (depending on the distro). But to get the ball rolling and create the data base right away, all you have to do is run:
updatedb
…and wait a while. Note that since you are running it yourself, it matters what user you are logged in as. It can only put things in the database that you can see, right? You may — or may not — want to be root
when you do that, depending upon what you want to be indexed…
What is it indexing? That depends on the config file, which on Suse is /etc/sysconfig/locate
. On others it might be /etc/locate.conf
. Either way, the config file specifies which user is runs as (default is nobody
), if it updates the DB daily (default is yes), and which paths to skip (many, and you can change to suit).
Now that all that is done, you can search by just typing something like this:
locate myFile.txt
…actually, it’s not case-sensitive, so I could even do this:
locate myfi
…and I’ll still find the same file, as well as everything that contains “myfi” anywhere in the name.
I hope this gets you started. I can’t say enough how valuable locate
has been to me. Oh sure, there’s always that delay when building the initial database, but after that it’s one of the most useful tools… and FAST!
Enjoy…
🙂