Hot on HuffPost Tech:

See More Stories
Free Switched iPhone app - try it now!
AOL Tech

Flipping the Linux switch: Misplace a file? Find it quick!

It happens to the best of us. We forget where we put things. Car keys. Flash drives. Yes, sometimes we even forget where certain files are on our computers. We can't really help you with the car keys and flash drives (although we inexplicably find things like that in the refrigerator here), we can help you out with finding missing files.

Coming from a Windows environment, you might be familiar with the graphical Search Files/Folder application. You know the one, it has the weird little cartoon dog that sometimes finds your files and folders, sometimes returns a lot of stuff you don't need, or sometimes doesn't return anything at all, even though you know it exists.

Linux also has graphical search applications. With them, you're able to configure your search parameters a little more tightly than with Windows (or maybe it just seems so, because we're not rushing to get the search done and make the freaky little dog go away). However, this usually isn't the quickest, or easiest, way to find your files.

This is definitely one of those times it's more productive for both old pro and new Linux users to use the command line. The confusing thing, even for some more experienced Linux users, is choosing which command to actually use to find the file or folder in question.


In Linux, there are three main search commands: find, locate and whereis. Each is a little different in terms of how and what they search. So when it is appropriate to use one over the others?

whereis

whereis is the most straightforward (and limited) of the commands. It's purpose is to find source or binary/executable files and the associated man pages. So let's say, for instance, you installed the Flock browser via binary file or source, and not from your system's repositories. Now you want to add it to a system menu, but you don't know where the executable has gone.

whereis flock

This returns man pages, source files, and all binaries associated with Flock. That's still more than you need. For your system menu, you really just want binaries.

whereis -b flock



This returns just binaries associated with Flock. The -m argument would just return man pages, and -s would return any source files associated with Flock.

There are, of course, other arguments associated with whereis, most of which allow you to search given directory paths you think the executable might be in. We've not found them quite as useful for new users, who sometimes have no idea where their binaries have disappeared to. If you need them, though, man whereis can get you up to speed.

locate

locate (and slocate) are commands used for broader searching. With slocate, you are unable to see the files you do not have permission to access. Some distributions link locate and slocate. Typing locate -V at the prompt in Xubuntu, for instance, returns that we are linking locate to Secure Locate (slocate).

locate allows you to find the location of every file containing your chosen text string on your system. In its simplest form:

locate flock | less



You almost certainly want to add "| less" to the command, as it does tend to return a lot of results, even when you've narrowed it down with arguments.

locate -i rODent | less

The -i argument tells locate to ignore case (just like grep, which we talked about last week).

locate -q glibc

The -q argument suppresses any errors that might arise in locate. These are usually permissions related (if you're not running this command as root) but sometimes other errors arise that you have no interest in seeing.

Before you run locate for the first time, you'll need to create the database that it searches filenames against. In most cases, this is done by changing to the root user (either using the command su, or sudo) and running updatedb. Running the updatedb command takes a bit of time, so be patient. And remember, if you used su to become root, to exit the root account when you're done.



The updatedb is usually run in the background at set times, so it may not find newly created files. If you're looking for the location of a file you just created, it's good to run updatedb before running locate.

find

find is the command you'll use for the most refined searches. Where whereis gets you a particular sort of file, and locate returns, well, nearly everything, find is most likely to get you the file you're looking for without a lot of extraneous stuff.

find can return results based on a number of search criteria -- names, groups, permissions, and even date of last modification. Using find can be a little more involved (think of it as using the advanced search features on a search engine, as opposed to typing a simple keyword into the main search box), but the results will much more pertinent.

By default, find searches the current directory and all its subfolders. You can point it to other directories, however.

find mp3 -name thick*



This searches the mp3 folder in my home directory, for files with the term "thick" in them (the * is a wildcard character).

This will search the current directory for all files edited in last 30 minutes. (You can enter any time frame you'd like, of course).

find -mmin -30



As you can see, it finds every file in my home folder that's had an alteration in that time frame. Ever download something and not been able to find it (admit it, we all have)? This could really be a time-saver.

This command is a little a more complicated, and shows a bit more of the power the find command has. In it, we're searching the root directory for any file named index.html (case insensitive) with group writeable permissions.

find / -iname index.html -perm -g+w

This command could be written another way, as well, and return the exact same results.

find / -iname index.html -perm -020



See all those permission denieds? It's because we're not running this command as root. That's a good thing, by the way. Should we see that one of those files needs a change of any variety, we can su into our root account and do so.

There are many more options in using the find command. It even has the ability to execute certain commands when it finds a file that matches a given criteria. At the risk of boring you to tears for searches you may not ever need to do, we'll direct you to man find for more complicated searches.

So when is it more appropriate to use one command over the other? Think carefully about what exactly you're looking for. If you need to find where man pages for a certain application are, or where a binary went, whereis is your best bet. For broader, simpler searches, locate is your command. You'll discover, though, for most searches, you'll get better results using find. It allows for an exact search across a number of different criteria, and returns the most pertinent results.

Losing stuff is easy. Finding it? Sure, it's a little more difficult, but you've got three great commands to get you started.

Tags: command line, CommandLine, commands, files, find, folders, gui, howtos, Linux, linux-switch, locate, opensource, terminal, whereis

Comments

12