How can I find a directory in Linux from the command line?

Alright so you know how to get around in Windows pretty well.  You’re not an expert but you know more than most.  Lately, you’ve been playing with a Linux computer and you’re not sure how to find files and directories.

In Windows Vista and newer searching for files is just a matter of pressing the Windows Logo Key and typing the search phrase; however, Linux isn’t so intuitive.

Today I want to show you how to find files on your Linux box.  It’s actually easier than you think and once you start using the command you’ll never forget it.

Let’s start with the aptly named find command!

Let’s say you’re setting up a webserver on your Linux box and you forget where to put your web documents.  The only think you remember is that the folder is called html.

We can use find to locate the full path like so:

nice find / -iname 'html'

Linux find command with nice command

Let’s break this down phrase by phrase.

The find command kicks off the show but what’s that nice business about?

First let me say that nice isn’t an order for Linux to be civil.

Well, I take that back – actually it sort of is.  Nice tells Linux to go easy on the CPU when performing resource intensive tasks.

Since we’re starting our search from the root directory we’re searching the entire file system; therefore, it’s prudent to use the nice modifier so we don’t interfere with more critical processes.

And this leads me to the next parameter in the find command: the  forward slash (/).

The forward slice says

Hey Linux, start our search quest in the root directory

If you change the forward slash to a dot (.) find begins and restricts its search to the current directory. (whatever that is)

To view the current directory just type:

pwd

Back in the find command syntax: the -iname parameter tells find to match the phrase that follows without regard for case.  So -iname ‘html’ matches:

  • ‘HTML’
  • ‘Html’
  • ‘HtMl’

You can see the highlighted results located the HTML folder in /var/www/html.

W00t!

That’s all there is to it.

But wait one second…

Let’s say you only wanted to list directories.

In the example above there is exactly one directory called ‘html” and no files of the same name.  In other words, I don’t have a file called ‘html’ – but if I did you would see that file appear in the search results too.

So how do you get around that?

-type d

Adding -type d to the find command forces find to stick to directories.

In the output below you can see I created a file simply named ‘html’ (no extension) in the /home/vonnie directory. (highlighted below)

Now when I type find / -iname ‘html’ I get results for both my webserver html directory and the empty html file I just made.

The next line under the highlighted item shows how adding -type d to the command ignores files and only displays directories.

Tell find in Linux to only search directories

I hope this helps!

 

About

Connect with Vonnie on Twitter

Posted in Linux Tagged with: