Terms of Use For FixedByVonnie

By proceeding to access fixedByVonnie.com, you expressly acknowledge, and agree to, all of the following:

fixedByVonnie.com is a personal website and blog owned by Security Plus Pro LLC, which is being presented for informational purposes only. The views on this website are solely those of the website owner (and not those of any employer or of any professional associations affiliated with the website owner).  Any views expressed in this website and any information presented on this website, or in any of its blog entries, should not be relied on for any purpose whatsoever other than as the personal opinions of the website owner.  The website owner expressly disclaims any and all liability for any information presented on this site.  The owner of this website and its blog posts shall not be held liable, and shall be held harmless, for any errors or omissions in any information or representations contained in this website, or in any of its blog entries.  The website owner also expressly disclaims any liability for the current or future availability of any such information. The website owner makes no representations as to the accuracy or completeness of any information on this website or which may be found by following any link on this website. The website owner shall not be held liable for any losses, injuries, damages, claims, or causes of action, from the display or use of any information on this website or in any of its blog entries. If you use the information on this website, or on any of its blog entries, you do so solely at your own risk.

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

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: