Modern operating systems do a great job of providing ways to search for files and directories located on your system. Some even let you search file contents to find a match. If you’re hanging out on your favorite remote server, you won’t have these niceties. Knowing how to find files and directories from the command-line is critical in these scenarios.
— Sorry to interrupt this program! 📺
Given the current situation with COVID-19, times are hard for most of us, but if you're stuck at home you can perhaps use that extra time to work on your React skills. Wes put all of his courses at 50% off to help out with the current challenge we're facing.
Plus, these are affiliate links, so if you purchase a course you help Alligator.io continue to exist at the same time! 🙏
Before We Begin
Wanted to give a bit of head’s up, some of the commands discussed below may not be installed by default on your system. If you try to run a command and receive a
command not found error, try to install the command via your system’s package manager.
Since package managers vary quite a bit, it’s a bit out of scope to list out package names and commands to run to do so. Best of luck and don’t get discouraged!
One of the quickest and easiest ways to search for files, by name or path is with the
locate command. You can get quite far without really knowing about any of the advanced arguments available:
$ locate alligator.io
That humble command will try to find any files that are named
alligator.io or contain
alligator.io in the path.
Default behavior for
locate is to match the whole path against the patterns, but you can pass in the
--basename flag to tell it to match against the base name of the files.
If you’re working on a system with a case-sensitive file system (like Linux), you can also pass in
--ignore-case to make the match case-insensitive.
For partial matches, you can drop in the wild card character
* as part of your search like
\*gator.io. You can even drop the wild card in the middle too,
locate command works against an internal database, an index of the entire file system, making it incredibly fast. If it’s barking about a missing
.db file, you may need to run
updatedb to prime it.
Finding Files and Directories
locate command is quite powerful, but is intended to search for files and paths to files, not directories by themselves.
find command has the capability to find files or directories, but also can find symbolic links as well as more advanced file types.
find requires a few more arguments, specifically a directory to start your search from and your search phrase:
$ find / -name alligator.io
By default, the
find command will search for files and the same rules apply with using wild card characters.
To search for directories or symbolic links, you can pass in a
-type argument which takes a single character for the type of file you’d like to search for:
$ find / -type f -name alligator.io # files $ find / -type d -name alligator.io # directories $ find / -type l -name alligator.io # symbolic links
find doesn’t use an indexed database to search, so it can be quite slow in comparison, especially if you’re trying to search the entire file system from root.
To best way to avoid the slowness is to make sure you specify a directory closer to where you think the file or directory you’re looking for may be, instead of root. You can also specify the
-maxdepth argument with an integer value to limit how many directories deep
find will traverse to find your file.
Finding Old Files
A handy trick that you can do with
find is to filter the results by the age of the files. Looking for any files that haven’t been modified in the last 30 days? No problem:
$ find /some/path/to/files -type f -mtime +30
Noticed the omission of the
-name argument? That’s not a typo, you don’t necessarily need to filter by name, but you definitely can definitely do both if you’d like:
$ find /some/path/to/files -type f -name *.bkup -mtime +30
To take things a step further, we can also pass the
-delete argument to tell
find to remove the files it finds:
$ find /some/path/to/files -type f -mtime +30 -delete
Obviously we wouldn’t want to run this against root, hence the use of a dummy path!
The aforementioned commands do a great job of finding files, and will actually help you find the location of a command you may be looking for.
If you’re looking for something a bit quicker and without much to remember in regard to arguments, you can use
whereis to give you details about binaries, source files and manual pages.
To get a bit meta, let’s try
whereis on itself:
$ whereis whereis
whereis exists on your system, will return something like this:
> whereis: /usr/bin/whereis /usr/share/man/man1/whereis.1.gz
I find that this particular command comes in super handy when you know damn well that a command is installed and should be available, but is, for whatever reason, giving you grief when trying to run it.