Finding Files on Command Line

Video loading...

  • 0:02
    Finding Files on Command Line with Addison Berry
  • 0:08
    In this Command Line Basics video,
  • 0:09
    we're going to be looking at how you can find files within
  • 0:12
    the command line, the system.
  • 0:15
    What we're going to be looking at is 3 commands,
  • 0:18
    locate and updatedb, those 2 go together,
  • 0:21
    and then the find command.
  • 0:23
    And they're kind of 2 different approaches.
  • 0:26
    So, we'll look at both of them, and what the differences are
  • 0:28
    between them, and how to use both of them.
  • 0:33
    To just get ourselves oriented, I'm going to do a pwd
  • 0:36
    just so we can see where we are on the server.
  • 0:37
    I'm in my home directory.
  • 0:38
    And this'll matter in a moment as we begin to look at commands.
  • 0:44
    The first command we're going to look at is locate.
  • 0:46
    locate works, actually, there's a database stored on the server
  • 0:51
    which lists all the files that it has access to.
  • 0:55
    And you will look up against that database.
  • 0:58
    And it's very straightforward, so I'm just going to basically
  • 1:02
    put the filename that I'm looking for.
  • 1:04
    I'm going to try to find php.ini.
  • 1:05
    And you'll see I get a list back of all kinds of stuff,
  • 1:10
    anything that contains php.ini, and in any directory.
  • 1:15
    You can see that this is stuff that's located
  • 1:18
    in etc and in user.
  • 1:21
    It's not limited to just where I am.
  • 1:24
    It's going from the roots, looking through
  • 1:26
    the entire database of everything that it has.
  • 1:28
    That's pretty handy and pretty fast and easy to do.
  • 1:31
    I'm going to go ahead and just create a file here real quickly.
  • 1:35
    It's just a blank text file called test.txt.
  • 1:40
    I'm going to just stick that in my home directory here.
  • 1:42
    You can see it exists.
  • 1:45
    And let's go ahead and do locate again and look for this file.
  • 1:52
    Same command.
  • 1:53
    And you notice it brings nothing back.
  • 1:56
    That's because locate is based on using a database.
  • 1:58
    And if a database isn't up-to-date,
  • 2:01
    then it's not going to actually find things.
  • 2:03
    So, there's the updatedb command,
  • 2:05
    and most systems have this running on a cron to go through
  • 2:09
    and update itself periodically.
  • 2:10
    But if there's something that's relatively new,
  • 2:12
    you can manually do it by just typing this command.
  • 2:16
    Now you can see I'm getting some permission denied errors
  • 2:19
    because it needs to go into a lot of places,
  • 2:21
    and I'm just a regular user right now.
  • 2:23
    I'm going to do this as sudo, so I'll just do sudo updatedb
  • 2:28
    so I can grab all the stuff in the database that I need.
  • 2:34
    And now that it's updated, if I run locate again for that file,
  • 2:40
    you'll see that we're going to find it.
  • 2:45
    Now it exists in the database, so locate can find it.
  • 2:48
    Without updating the database, locate doesn't know.
  • 2:51
    If I remove the file, it's the same thing.
  • 2:53
    I've removed the file.
  • 2:54
    You can see it's no longer in the directory.
  • 2:56
    If I do locate, it's still there because it's still
  • 3:00
    in the database.
  • 3:02
    Again, if you update the database,
  • 3:04
    then that'll be a completely fresh version to search against.
  • 3:10
    Do that.
  • 3:11
    And then we'll run this locate again.
  • 3:13
    And it's gone.
  • 3:15
    OK.
  • 3:17
    That's the basics of locate.
  • 3:20
    Now let's go look at another way that you can find things
  • 3:24
    on your server, called find.
  • 3:28
    Pretty simple command name.
  • 3:31
    But you'll see that the command itself is not
  • 3:33
    quite as simple as locate is.
  • 3:36
    find does not use a database.
  • 3:37
    It's actually going to just go through everything real time.
  • 3:42
    And it requires a few more parameters.
  • 3:46
    So, we're going to work our way through this
  • 3:48
    and try to find php.ini.
  • 3:50
    I need to put this -name to tell find this is the
  • 3:54
    name that I'm looking for.
  • 3:56
    Find -name and then the name of the file I'm looking for.
  • 4:01
    And you'll see we're getting nothing back.
  • 4:04
    The reason for this, remember, those php.ini files were
  • 4:09
    in etc and user, and I'm in home.
  • 4:12
    I'm in my home directory.
  • 4:13
    It's going from where I'm located,
  • 4:16
    rather than doing the entire server.
  • 4:18
    I can change that by telling find where to start.
  • 4:22
    I can put in a path.
  • 4:23
    I can do just /etc, or I could say just start looking in /home.
  • 4:29
    Or I can just leave a slash, which means look root.
  • 4:32
    That means search the entire thing.
  • 4:34
    And then I'll still put my -name .php.
  • 4:37
    And again, I'm getting permission denied errors
  • 4:41
    because a php.ini file is in places that this user
  • 4:44
    doesn't have write access to.
  • 4:47
    If I do sudo, though, same command, sudo find,
  • 4:51
    from the root of the server for php.ini,
  • 4:55
    and now I get some results back.
  • 4:58
    You can see I'm only getting 2 because these are exactly
  • 5:02
    php.ini, is the filename.
  • 5:05
    If we look at the locate command that we did earlier,
  • 5:08
    we'll see that we actually had a whole lot more items
  • 5:14
    with php.ini.
  • 5:18
    And that's because locate is doing things that
  • 5:21
    are contained within.
  • 5:23
    If I want to replicate the same thing with my find command,
  • 5:26
    I can just add a wildcard to the end.
  • 5:29
    I could also add one at the beginning if I truly wanted it
  • 5:31
    to be contained with any characters before or after,
  • 5:34
    but I'm just going to do, begins with php.ini,
  • 5:36
    and then ends with any other characters after that.
  • 5:41
    By adding the wildcard and then running the command,
  • 5:44
    now you can see we get the full list.
  • 5:46
    These are the same file names that locate returned.
  • 5:49
    You can see, find is a little more complicated.
  • 5:53
    locate is very straightforward and simple and really fast
  • 5:58
    because it's using the database.
  • 6:00
    You just need to remember to do updatedb.
  • 6:03
    Also, keep in mind that on a Mac, updatedb works differently.
  • 6:06
    It's set up on its own cron, so running it manually is funky.
  • 6:11
    I'm actually on a Linux server here.
  • 6:15
    But locate is really good for just doing a fast lookup.
  • 6:19
    You just need to find a file, don't care where you are.
  • 6:21
    find gives you a little more flexibility in terms of
  • 6:25
    what you can put in there for parameters, but is also a little
  • 6:27
    more complicated.
  • 6:30
    There's one last thing I want to look at,
  • 6:31
    which is looking for directories.
  • 6:33
    We've been looking at files, which is what these commands
  • 6:38
    are really designed to do.
  • 6:39
    But let's take a look and see how things go when
  • 6:42
    I want to try and find a directory on my server.
  • 6:45
    I'm going to look for all directories called public_html.
  • 6:49
    I'm just going to do locate and run that.
  • 6:53
    And you can see I get back craziness because it's going
  • 6:57
    through that database of filenames and finding public_html
  • 7:00
    in any of them, with their paths.
  • 7:02
    It returns every single thing, which is a bit overwhelming
  • 7:06
    and not what I was looking for.
  • 7:08
    But if I use the find command, I can actually kind
  • 7:12
    of make this work for me.
  • 7:13
    I'm going to go ahead and do my find command, sudo find,
  • 7:18
    because I'm going to be finding in places
  • 7:20
    my user doesn't have access.
  • 7:22
    I'm going to just start from the home directory.
  • 7:24
    There's no need to go from root.
  • 7:26
    I just want all of the public_html folders in
  • 7:28
    the home directory, or under the home directory.
  • 7:31
    And then name and my folder name, public_html.
  • 7:37
    Very similar thing.
  • 7:39
    I'm just limiting it and giving it a directory name.
  • 7:42
    And as you can see, this turns back the 3 public_html
  • 7:47
    directories that are located underneath my home directory.
  • 7:51
    That's really handy and a nice thing to know, as well.
  • 7:55
    If you're looking for directories,
  • 7:57
    you really probably want to work with find, rather than locate.
  • 8:00
    But both really useful tools.
  • 8:02
    And you should get familiar with the basics of them,
  • 8:04
    and have fun.
Loading ...

Finding Files on Command Line

Loading...

In this video we'll look at two ways of finding files from the command line. We'll use both locate (and its friend updatedb) and find, and talk about the differences between them.

Note: this video was originally released June 8, 2010 on Lullabot.com.

Additional resources:
There are no resources for this video. If you believe there should be, please contact us.