I don’t know about you, but when I was brand new to Linux, that Bash prompt was a bit daunting. I’d come from an MS-DOS and Windows 3.x background at the time (yes, this was over 2 decades ago, who’s counting?) so I at least had some time at a command prompt. Is that the case for new Linux admins today? Let me know in the comments. But I digress.

One of the things I had trouble with was learning where everything was. In DOS is was pretty simple. All of my OS stuff was in c:\dos, and just about everything else was either my files, or programs. In Linux it’s all over the place. The structure makes a lot of sense once you learn it, but until then, it feels endless, and complicated. Linux is all about standards, and believe it or not there is a standard regarding where files go based on what their purpose is. It’s called the Filesystem Hierarchy Standard, and you can find it here if you’re interested. That’s not this article though. In this article I’m showing you some of the tools I found early on to help me locate things in the Linux filesystem.

which

which is a handy little tool. Like most operating systems, Linux uses a concept of your default search path to find commands wherever you are. If something is ‘in your path’ then you can execute it from anywhere. Take the ls command for instance. On most distributions it’s in /bin, or /usr/bin. Those directories are part of your path. Your path, by the way, is stored in an environment variable called… $PATH surprisingly. It looks like this:

If an executable is in two places that are in your path, it can get confusing which one of them you just executed if you didn’t call it directly. Or maybe you just don’t know where the executable is located, and you want to know, if I type ls where is it running from? This is exactly what which is here to solve. If you enter which ls it will return where it found ls in your path. It’ll also tell you if there is an alias altering the command. In the case of ls my environment is adding colorization to make the output more readable.

You can use this for any command. A common one might me, where the heck is my java binary? Or maybe where is python?

locate

locate was one of the first tools I came across for finding things. Mainly because find was difficult for a newbie to use. OK it’s not that hard, but I was a teenager gimme some slack. Yes I’ll cover find in a moment. locate uses a database of file locations to spit out where a file exists. This of course means that the database needs to exist, and get updated from time to time. I mention locate because its easy, but honestly, I can’t tell you the last time I used it. It isn’t even present on many modern systems, because find is a better tool in most cases, and really, i think locate has some security implications because whoever builds the locate database, well, the files in the database are everything that user could see. So if you do it as root… well… everything root can see ends up in the database. Though it does seem to filter that a bit. For this article I tried to see if I could get locate to tell me what was in /root for example, and it didn’t have it in the database.

On Red Hat Enterprise Linux (RHEL) the package that adds locate is mlocate, and it is not installed by default on a minimal install. Once it’s installed you’ll need to generate that locate database using the updatedb command.

This command returns no output, but now you can use locate to find things. Locate will show you everything that matches the string you asked it to find. Here’s an example, asking locate to find ls (and just counting up the number of results).

So, let’s look for something more reasonable. How about.. passwd. Shouldn’t be many of those.

So yeah, I guess it filters out /etc as well. So you can see that locate just isn’t all that great. It is worth noting that find is pretty dang fast, because it’s searching a database instead of crawling your filesystem’s tree, and sure, you can probably get more advanced with it, but there isn’t much of a reason, because you’ve got find.

find

find is a powerful tool. It not only lets you find things, it can perform actions on those things. I could write an article about find but for this one, I’ll stick to some basics. So I’ve been looking for ls, let’s use find for the same example. The reason I found find daunting as a newbie was because unlike locate that just takes one argument (the thing your’re looking for) to work, find feels like you need to be a programmer to just find a file. You have to tell it what to find, where to look for it (recursively), the string to look for, and what to do with it! The most basic find I can think to give you is…

Let’s break that down. find of course is the executable, / is the place to start the search, -name ls instructs to find files named ls, and this is an exact match where locate was not, and then -print just says to print it to the screen. So what’s 2>/dev/null about? Well find recursively searches, so because I ran it as a standard user, it also recursed into /sys and /proc/ and lots of other places I don’t have permission to read. So I redirected stderr to /dev/null to clean up the output. You can define a filter with find, but I was trying to keep it simple for you!

Close

So there you have it, three solid ways to find files in Linux. I hope you’ve found this informative, maybe you can learn to use find earlier than I did. 😉 Happy hunting!

Author

  • Nate Lager

    Nate has been using Linux since Red Hat 5.0. He has taken his passion for communities, technology, and open source from a hobby to a career over the past 20+ years. Nate is a Sr. Technical Marketing Manager at Red Hat, and a content creator. From blogs, to live streams, to Podcasts, Nate has a passion for sharing his love of Linux and Technology.


Nate Lager

Nate has been using Linux since Red Hat 5.0. He has taken his passion for communities, technology, and open source from a hobby to a career over the past 20+ years. Nate is a Sr. Technical Marketing Manager at Red Hat, and a content creator. From blogs, to live streams, to Podcasts, Nate has a passion for sharing his love of Linux and Technology.

1 Comment

Seth Kenlon · 2023-05-26 at 00:32

What a great collection of useful tools you’ve featured. I often forget about the locate command, but mental note has been made [again].

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *