The find command - look for your files
One of the big things that people regularly need to do on a computer is find files or directories on the hard drive, also called the file system. When you are moving from Windows to Linux trying to find things is not quite the same. Usually on Windows you will use the dir command to find things. Although the ls command is roughly the same as dir, and in some ways much more powerful, it is not the best tool to find something when you don't know where on the file system it is located. The find command is uniquely suited for this task.
The find command has a lot of options that you can use when searching the file system. Let's take a look at some of the most common options. Keep in mind that this will be only a fraction of all the options the find command is capable of. First, let's look at the structure of the command
find dir-to-start-in comparison-criteria search-filter
Looking for a file by name
The first and most obvious use is to find a file by the file name. In this case it will be fairly similar to the dir command in Windows. Basically you will tell find the find command where in the directory structure to start, that you want it to find a filename, and then a search filter for the filename.
find /tmp -name "myfi*"
This would start under /tmp and it would return /tmp/myfile /tmp/test/myfilename and any other hits with similar name. It would not find /tmp/MyFile however since the file system is case sensitive. You can make the search case insensitive by using -iname instead of just -name in the command.
find /tmp -iname "myfi*"
If you get a large return you can pipe the output into another command. I typically will use less so I can scroll up and down through the results, or perform additional searches in the results less is displaying.
find /tmp -iname "myfi*" | less
Limit directory depth in the search
Obviously you can string a series of options in the command line. One that can be handy is the -maxdepth option. This one will limit how many subdirectories down the find command will search. If you want to go just in the directory specified you can do a -maxdepth 1 to get the results. If you wanted the directory specified and one directory below that you would do -maxdepth 2 to get there.
find /tmp -maxdepth 2 -iname "*.php"
Inverting the match
Often you will have a need to find every file except a particular pattern. Maybe you have a number of files with a tmp extension and want to ignore those when looking at the list of files. Simply add -not to the command line.
find /web/site -not -name "*.tmp"
This would list all .html files and all .php files and ignore all the .tmp files. It is not uncommon to need to look for all the files but one type. This is often a very handy search.
Search multiple directories
Let's look at one more really popular search, being able to search multiple disconnected directories at once. Let's assume you have a server with multiple websites. You want to find a particular php file and you know it is in one of two websites on the server. And you don't remember the case of the file name.
find /web/site1 /web/site4 -iname searchldap.php
This will look for the file searchldap.php in those two websites but not under the other website directories.
These are probably the most popular uses of the find command. However, there are many many more options, like searching for only files or only directory names, looking for files with particular permissions, or looking for files based on when they were changed last. The options are almost endless.