Still struggling with the Linux learning curve? We don’t blame you! It’s vastly different from Windows so you’re bound to run into trouble, such as locating those files that you misplaced the other day. Is there an easy way to find them?
Fortunately, yes. However, you’ll need to be somewhat familiar with the command line in order to unlock the full power of the Linux ecosystem and all of its underlying utilities. If you haven’t already, get acquainted with these essential Linux commands.
When you’re ready, here are three simple commands that you can use to locate files you just can’t seem to find.
The “Which” Command
The which command is the simplest of the three commands we’re going to explore, but this simplicity comes at a cost: its usage is extremely narrow and specific. However, for what it’s meant to do, it’s very good at what it does.
On Linux, every command that you run in the command line actually points to a binary file (also known as an executable file) somewhere on the system. When you type a command, that command’s binary file is what ends up being executed. When you use the
which command with a command you’re searching for, the output is the path to that command’s binary file.
In the example above, the oft-used command
sudo actually points to a binary file named sudo in the /usr/bin directory. Not all binaries are located in the this directory, though, so
which comes in handy when you need to find a binary but have no idea where to start looking.
An alternative command is the
whereis command, which gives you a bit more information — not just the location of the command’s binary file, but the location of the command’s source files and man pages as well.
Do note that both can search for multiple commands at once by supplying as many command names as you want (as illustrated in the screenshots).
One slight difference between the two commands is that whereis will only look through a built-in list of directories when searching while which will look through all of the directories in the current user’s PATH environment variable.
The “Locate” Command
The next command we’re going to explore is locate, which uses a pre-built database of files and directories to speed up the search process. If you’ve ever used an indexed Windows search, the concept should be pretty familiar.
This kind of indexed search is certainly faster than searching the entire disk drive, but the downside is that the index can sometimes fall out of sync. Though the Linux system periodically updates the index on its own, you can force it to sync using the
Note that an index update could take up to several minutes depending on your system’s processing power.
To use the
locate command, all you have to do is provide a query string that it will use for finding matches. The command will output a list of all indexed directories and files that match the query.
If you want to limit the search to exact matches only, use the
-b parameter. If you want to make the search query case-insensitive, use the
-i parameter. If you want to limit the number of results, use the
-n <#> parameter.
The “Find” Command
Lastly we have the find command, which is the most versatile of the commands we’re going to explore but also the hardest to learn because of how flexible it can be.
Do note that when you use this command, it will always search in the current directory unless specified otherwise.
To find a file by its name, use the
-name parameter (or the
-iname parameter for case-insensitivity). Or, you could inverse the search and exclude files by their name using the
-not modifier. The
* symbol is used as a wildcard.
You can also find all files according to their type using the
-type parameter. The following common options correspond to their respective file types:
- d: directories
- f: regular files
- l: symbolic links
Similar to file types, you can search according to file size using the
-size parameter followed by a string that indicates the size, unit, and whether we want an exact, lower than, or greater than match:
- c: bytes
- k: kilobytes
- M: megabytes
- G: gigabytes
And of course, all of these can be combined to form highly specific searches for specific files. If you want to chain multiple searches into one, use the
-or parameters in between queries.
Never Lose Another File on Linux
And there you have it: the best ways to find files on Linux using nothing more than the command line. No third-party software necessary. Whether you need a simple where, a fast locate, or a powerful find, Linux has got you covered.
How do you find files on Linux? Share your best tips and tricks with us in the comments below!