Linux find command examples

Introduction

The Linux find command is very powerful. It can search the entire filesystem for one or more files that you specify. It's an extremely helpful Linux command to understand, especially when you need to find a file or directory.

You can also use the find command to locate files, and then perform some type of action on the files after they've been located. With this capability, you can locate files using powerful search criteria, and then run any Unix command you want on the files you locate. (See the examples below.)

Find files with the Linux find command

This first Linux find example searches through the root filesystem ("/") for the file named "Chapter1". If it finds the file, it prints the location to the screen.

find / -name Chapter1 -type f -print

A nice thing to know is that on Linux systems and modern Unix system you no longer need the -print option at the end of the find command, so you can issue it like this:

find / -name Chapter1 -type f

This next find command searches through the /usr and /home directories for the file named Chapter1:

find /usr /home -name Chapter1 -type f

To search in the current directory, and all subdirectories, just use the . character to reference the current directory in your find commands, like this:

find . -name Chapter1 -type f

This next command searches through the /usr directory for all files that begin with the letters Chapter, followed by anything else. The filename can end with any other combination of characters. It will match filenames such as Chapter, Chapter1, Chapter1.bad, Chapter-in-life, etc.:

find /usr -name "Chapter*" -type f

This next command searches through the /usr/local directory for files that end with the extension .html. These file locations are then printed to the screen.

find /usr/local -name "*.html" -type f -print

How to find directories with Linux find

Every option you just saw for working with files can also be used on directories. Just replace the -f option with a -d option. For instance, to find all directories named build under the current directory, use this command:

find . -type d -name build

Finding files that contain text (find plus grep)

You can combine the find and grep commands to powerfully search for text strings in many files.

This next command shows how to find all files beneath the current directory that end with the extension .java, and contain the characters StringBuffer. The -l argument to the grep command tells it to just print the name of the file where a match is found, instead of printing all the matches themselves:

find . -type f -name "*.java" -exec grep -l StringBuffer {} \;

(Those last few characters are required any time you want to exec a command on the files that are found. I find it helpful to think of them as a placeholder for each file that is found.)

This next example is similar, but here I use the -i argument to the grep command, telling it to ignore the case of the characters string, so it will find files that contain string, String, STRING, etc.:

find . -type f -name "*.java" -exec grep -il string {} \;

Acting on files that are found

This command searches through the /usr/local directory for files that end with the extension .html. When these files are found, their permission is changed to mode 644 (rw-r--r--).

find /usr/local -name "*.html" -type f -exec chmod 644 {} \;

This command searches through the htdocs and cgi-bin directories for files that end with the extension .cgi. When these files are found, their permission is changed to mode 755 (rwxr-xr-x). This example shows that the find command can easily search through multiple sub-directories (htdocs, cgi-bin) at one time.

find htdocs cgi-bin -name "*.cgi" -type f -exec chmod 755 {} \;

Case-insensitive file searching with find

To perform a case-insensitive search with the Unix/Linux find command, use the -iname option instead of -name. So, to search for all files and directories named foo, FOO, or any other combination of uppercase and lowercase characters beneath the current directory, use this command:

find . -iname foo

If you're just interested in directories, search like this:

find . -iname foo -type d

And if you're just looking for files, search like this:

find . -iname foo -type f

Related

If you're just looking for a file by name, and you want to be able to find that file even faster than you can with the Linux find command, take a look at the Linux locate command. The locate command keeps filenames in a database, and can find them very fast.

For more details on the find command, check out our online version of the find man page.

Also, if you have any favorite Unix/Linux find commands you'd like to share, please use our comment form below.

Used find on bugzilla directories

I just had a problem with a Bugzilla installation (on an intranet), and finally gave up on trying to fix all the permission problems, and just made all the bugzilla subdirectories 775 like this:

find bugzilla -type d -exec chmod 775 {} \;

Here 'bugzilla' was a subdirectory of my current directory.

deleting files with the find command

I was using "find * -mtime +63 -exec rm{} \;

However there are too many files in the directory.

find, xargs, and rm

I've always been lazy in this situation and done something as follows. First, create a list of all the files you want to remove, putting that list into a file:

find mydir -mtime +63 > files-to-remove

Then follow this with a shell for loop, like this:

for i in `cat files-to-remove`
do
  rm $i
done

But a quick warning: I haven't tested that for syntax errors, but I think it's right.

But, even better, I've used find with xargs before to create large tar backup archives, and in the xargs man page docs I just saw these examples related to removing files. Hopefully these examples will help:

EXAMPLES

find /tmp -name core -type f -print | xargs /bin/rm -f
 
  Find  files  named  core in or below the directory /tmp and delete them.
  Note that this will work incorrectly if there are any filenames containing 
  newlines or spaces.
 
find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f

  Find files named core in or below the directory /tmp and delete them, 
  processing filenames in such a way that file or directory names containing 
  spaces or  new-lines are correctly handled.

No matter, which approach, be careful, and try a test first (maybe using the echo command) to make sure it works was expected..

case sensitive?

Is this command case sensitive?
How about if I want to find directories with upper cases only?

How to ignore case

I'll add a new section to this article with this update, but to perform a case-insensitive search, use the -iname option instead of -name. So, to search for all files and directories named foo, FOO, or any other combination of uppercase and lowercase characters beneath the current directory, use this command:

find . -iname foo

In response to the second question, to search for directories with uppercase characters only I'd need to know a little more about what you're looking for, but something like this might help get you started:

find . -type d -name "[A-Z]*"

The -type d option tells find to just look for directories, and the wildcard pattern "[A-Z]*" says "Search for any directory whose name contains one or more uppercase characters."

Post new comment

The content of this field is kept private and will not be shown publicly.