Lullabot Ideas

We know stuff. We empower you to know stuff too.

Command Line Basics: Finding files

Video by Addison Berry

The locate, updatedb and find commands

short-cli-basics-7-find-locate.mov-1.png

Watch the video on Drupalize.me

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.

Comments

grep it

You can take it one step further.
Say you're debugging a Drupal site and you've got a situation where you're trying to determine where in the heck that <DIV class="command-line-basics"> was coming from.
You can read thru file contents searching for that class.

$ cd ttamniwdoog.com/
$ grep -ir 'command\-line\-basics' *

yep, next video is about grep

Just to be clear for folks who are new to all this stuff, locate and find look for files. Grep will look for strings inside files. Both useful, but definitely very different tools for different needs.

Grep will be the next video out as a complement to this one. :-) It was too much to fit it all into one video. Look for the grep video next week.

Using grep to find file names

du -a will show all files within the current directory, like so:

12 ./includes/database.mysqli.inc
8 ./includes/cache.inc
16 ./includes/xmlrpc.inc
8 ./includes/install.pgsql.inc
16 ./includes/database.mysql-common.inc
8 ./includes/tablesort.inc
12 ./includes/batch.inc
8 ./includes/install.mysqli.inc
12 ./includes/theme.maintenance.inc
20 ./includes/unicode.inc
24 ./includes/install.inc
...

You can "pipe" this to grep to find file names like so:

du -a | grep 'xmlrpc'

Which will yield:

16 ./includes/xmlrpc.inc
12 ./includes/xmlrpcs.inc
4 ./xmlrpc.php

Hopefully this quick tip helps some folks out too :-)

You can also use this same

You can also use this same method to find directories, and utilize regular expressions for your matching ;-)

Warning

I'm getting this warning:

WARNING: The locate database (/var/db/locate.database) does not exist.
To create the database, run the following command:

sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist

Please be aware that the database can take some time to generate; once
the database has been created, this message will no longer appear.

If I create this database, do I have to update it as new files get added to the disk? Or does that happen automatically?

Great!

Nice easy to follow format for those new to the *nix command line. These are important tools who's usage it's important to grok.

I'd like to suggest perhaps the next in this series be on grep, along the same lines as this, but rather than searching filenames, sometimes you only know a function name or want to see every file a variable is used in, grep is like find, INSIDE files. Definitely would be a useful tutorial. :)