You are on page 1of 28

A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.

shtml

alvin alexander

1 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

categories

Linux/Unix FAQ: Can you share some


Linux find command examples?

Sure. The Linux find command is


very powerful. It can search the entire
filesystem to find files and directories
according to the search criteria you
specify. Besides using the find
command to locate files, you can also
use it to execute other Linux
commands (grep, mv, rm, etc.) on the
files and directories you find, which
makes find extremely powerful.

In this article I’ll take a look at the


most common uses of the find
command.

(this space intentionally left blank for


that long “table of contents” over
there -->)

2 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

If you just want to see


some examples and skip
the reading, here are a
little more than 30 find
command examples to
get you started. Almost
every command is
followed by a short
description to explain
the command; others are
described more fully at
the URLs shown:

basic 'find file' commands


--------------------------
find / -name foo.txt -type f -print
find / -name foo.txt -type f
find / -name foo.txt
find . -name foo.txt
find . -name "foo.*"
find . -name "*.txt"
find /users/al -name Cookbook -type d

search multiple dirs


--------------------
find /opt /usr /var -name foo.scala -ty

case-insensitive searching
--------------------------
find . -iname foo
find . -iname foo -type d
find . -iname foo -type f

find files with different extensions


------------------------------------

3 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

find . -type f \( -name "*.c" -o -name "*.sh" \)


find . -type f \( -name "*cache" -o -name "*xml" -o -name "*htm

find files that don't match a pattern (-not)


--------------------------------------------
find . -type f -not -name "*.html"

find files by text in the file (find + grep)


--------------------------------------------
find . -type f -name "*.java" -exec grep -l StringBuffer {} \;
find . -type f -name "*.java" -exec grep -il string {} \;
find . -type f -name "*.gz" -exec zgrep 'GET /foo' {} \;

5 lines before, 10 lines after grep matches


-------------------------------------------
find . -type f -name "*.scala" -exec grep -B5 -A10 'null' {} \;
(see http://alvinalexander.com/linux-unix/find-grep-print-

find files and act on them (find + exec)


----------------------------------------
find /usr/local -name "*.html" -type f -exec chmod 644 {} \;
find htdocs cgi-bin -name "*.cgi" -type f -exec chmod 755 {} \;
find . -name "*.pl" -exec ls -ld {} \;

find and copy


-------------
find . -type f -name "*.mp3" -exec cp {} /tmp/MusicFiles \;

copy one file to many dirs


--------------------------
find dir1 dir2 dir3 dir4 -type d -exec cp header.shtml {} \;

find and delete


---------------
find . -type f -name "Foo*" -exec rm {} \;
find . -type d -name CVS -exec rm -r {} \;

find files by modification time


-------------------------------
find . -mtime 1 # 24 hours
find . -mtime -7 # last 7 days
find . -mtime -7 -type f # just files
find . -mtime -7 -type d # just dirs

4 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

find files by modification time using a temp file


-------------------------------------------------
touch 09301330 poop # 1) create a temp file with a specific timestamp
find . -mnewer poop # 2) returns a list of new files
rm poop # 3) rm the temp file

find with time: this works on mac os x


--------------------------------------
find / -newerct '1 minute ago' -print

find and tar


------------
find . -type f -name "*.java" | xargs tar cvf myfile.tar
find . -type f -name "*.java" | xargs tar rvf myfile.tar
(see http://alvinalexander.com/blog/post/linux-unix/using-find-xargs-tar-create-huge-
for more information)

find, tar, and xargs


--------------------
find . -name -type f '*.mp3' -mtime -180 -print0 | xargs -0 tar rvf music.tar
(-print0 helps handle spaces in filenames)
(see http://alvinalexander.com/mac-os-x/mac-backup-filename-directories-spaces-find-t

find and pax (instead of xargs and tar)


---------------------------------------
find . -type f -name "*html" | xargs tar cvf jw-htmlfiles.tar -
find . -type f -name "*html" | pax -w -f jw-htmlfiles.tar
(see http://alvinalexander.com/blog/post/linux-unix/using-pax-instead-of-tar)

On a related note, don’t forget the locate command. It keeps a database on your
Unix/Linux system to help find files very fast:

locate command
--------------
locate tomcat.sh # search the entire filesystem for 'tomcat.sh' (uses the locate
locate -i spring.jar # case-insensitive search

If you know of any more good find commands to share, please leave a note in the
Comments section below.

The remaining sections on this page describe more fully the commands just shown.

5 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

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

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

The -type f option here tells the find command to return only files. If you don’t use it,
the find command will returns files, directories, and other things like named pipes and
device files that match the name pattern you specify. If you don't care about that, just leave
the -type f option off your command.

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

find /usr /home -name Chapter1.txt -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 example 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.:

6 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

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

Storite 3.5mm Stereo Jack Splitter Cable for MP3 …

Rs. 126.00 (details + delivery)

Every option you just saw for finding 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

To find all files that don't match a filename pattern, use the -not argument of the find

7 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

command, like this:

find . -type f -not -name "*.html"

That generates a list of all files beneath the current directory whose filename DOES NOT
end in .html, so it matches files like *.txt, *.jpg, and so on.

You can combine the Linux 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 {} \;

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

8 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

(rw-r--r--).

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

This find 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 {} \;

From time to time I run the find command with the ls command so I can get detailed
information about files the find command locates. To get started, this find command will
find all the *.pl files (Perl files) beneath the current directory:

find . -name "*.pl"

In my current directory, the output of this command looks like this:

./news/newsbot/old/3filter.pl
./news/newsbot/tokenParser.pl
./news/robonews/makeListOfNewsURLs.pl

That's nice, but what if I want to see the last modification time of these files, or their
filesize? No problem, I just add the ls -ld command to my find command, like this:

find . -name "*.pl" -exec ls -ld {} \;

9 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

This results in this very different output:

-rwxrwxr-x 1 root root 2907 Jun 15 2002 ./news/newsbot/old/3filter.pl


-rwxrwxr-x 1 root root 336 Jun 17 2002 ./news/newsbot/tokenParser.pl
-rwxr-xr-x 1 root root 2371 Jun 17 2002 ./news/robonews/makeListOfNewsURLs.pl

The "-l" flag of the ls command tells ls to give me a "long listing" of each file, while the -d
flag is extremely useful in this case; it tells ls to give me the same output for a directory.
Normally if you use the ls command on a directory, ls will list the contents of the directory,
but if you use the -d option, you'll get one line of information, as shown above.

Be very careful with these next two commands. If you type them in wrong, or make the
wrong assumptions about what you're searching for, you can delete a lot of files very fast.
Make sure you have backups and all that, you have been warned.

Here's how to find all files beneath the current directory that begin with the letters 'Foo'
and delete them.

find . -type f -name "Foo*" -exec rm {} \;

This one is even more dangerous. It finds all directories named CVS, and deletes them and
their contents. Just like the previous command, be very careful with this command, it is
dangerous(!), and not recommended for newbies, or if you don't have a backup.

find . -type d -name CVS -exec rm -r {} \;

The syntax to find multiple filename extensions with one command looks like this:

10 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

find . -type f \( -name "*.c" -o -name "*.sh" \)

Just keep adding more "-o" (or) options for each filename extension. Here's a link to

To perform a case-insensitive search with the Unix/Linux find command, use the -iname
option instead of -name. For example, if you want 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

To find all files and directories that have been modified in the last seven days, use this
find command:

find . -mtime -7

To limit the output to just files, add the -type f option as shown earlier:

11 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

find . -mtime -7 -type f

and to show just directories:

find . -mtime -7 -type d

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 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 Linux and Unix find commands you’d like to share, please
use the comment form below.

There’s just one person behind this website; if this article was helpful (or interesting), I’d
appreciate it if you’d share it. Thanks, Al.

related

12 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

books i’ve written

what’s new

13 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

Submitted by Anonymous (not verified) on September 12, 2009 - 8:27am

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.

Submitted by Anonymous (not verified) on September 14, 2009 - 2:20pm

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

However there are too many files in the directory.

Submitted by on September 15, 2009 - 10:51am

14 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

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 ,


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..

15 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

Submitted by on October 28, 2009 - 2:18am

Is this command case sensitive?


How about if I want to find directories with upper cases only?

Submitted by on October 28, 2009 - 9:27am

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:

16 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

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."

Submitted by Mahesh (not verified) on March 10, 2010 - 7:12am

Had just what I wanted and straight to the point

Submitted by sachin (not verified) on May 2, 2010 - 4:58am

good one!!!

17 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

Submitted by Sathya (not verified) on May 25, 2010 - 9:27am

Was very helpful. Straight to the point.

Submitted by Roopesh (not verified) on July 12, 2010 - 5:37am

This is amazingly simple and very helpful..A Very good document!!!!Thanks

Submitted by Venkatesh (not verified) on July 15, 2010 - 11:04pm

These information's are very useful.. Thanks

18 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

Submitted by captain (not verified) on September 24, 2010 - 10:19am

Is there any way to set a "-quiet" type flag to ignore all the error lines? I get more
"Permission denied" lines than I do results to my find query. I don't see anything in
the man page. I thought maybe I could run the output through grep -v "Permission
denied" but that fails to work.

Submitted by captain (not verified) on September 24, 2010 - 10:24am

Often I'd like to know exactly WHERE the files that get found reside. the -print
(default) command only gives the file name. The -ls flag prints out a bunch of data,
including size and full path. You can then use sed/awk (if you know how - I suck at
this too) to pull out specific data from what find returns.

Another cool thing to do is run your find output through wc -l to count all the found
files. e.g.:
find ~music -type f -iname *.mp3 | wc -l
will count all the mp3 files in the music user's home and subdirs.

19 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

Submitted by on September 28, 2010 - 12:10pm

There is a -[no]warn flag, though I've never used it. The problem with using grep in
this case is that the errors are probably coming out of find on the STDERR stream
instead of STDOUT, so you'll need to get rid of them something like this:

find ...your options here... 2> /dev/null

If you haven't used it before, the "2>" symbol lets you redirect STDERR output.

Cheers.

Submitted by on September 28, 2010 - 12:12pm

Thanks, you're right, I totally forgot to include an "ls" example. I'll try to add one here
this morning. Usually I use ls with the -ld option, like this:

find . -name "foo" -exec ls -ld {} \;

I'll add this next. Thanks again.

20 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

Submitted by on November 11, 2010 - 11:43pm

Simply ultimate..quite impressed by the way you present the usage of the command.
Well done.

Submitted by Anonymous (not verified) on November 15, 2010 - 11:36am

Objective and minimalist. :-)


Thank you.

Submitted by Anonymous (not verified) on December 8, 2010 - 11:23pm

21 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

Thanks :)

Submitted by MJ_Techi (not verified) on January 4, 2011 - 7:28am

Can someone help me in find files with multiple extensions like .sh or .c or .txt
for ex-
find . -name "*.(c|sh|txt)"

Submitted by on January 4, 2011 - 12:34pm

The syntax to find multiple filename extensions with one command looks like this:

find . -type f \( -name "*.c" -o -name "*.sh" \)

Just keep adding more "-o" (or) options for each filename extension. Here's a link to
.

22 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

Submitted by Sudhakar S (not verified) on February 28, 2013 - 1:07am

Hi
Can anyone help me to find a directory using "find command" and list the contents of
that directory using ls command.

Eg. Suppose say I want to find the directory called "mosra" in the present workign
directory. This directory may be present anywhere in the path and I want to list ONLY
the directories present in that "mosra" directory.

Working Dir $ pwd

/nfs/iind/home/sshirnix/public/scripts/perl_scripts

Working Dir $ find ./ -name mosra -type d


./aging/hspice/mosra

I tried this command


find ./ -name mosra -type d -printf %P\\n -exec ls {} \;
But it lists out file and directores both
What I want is to list out the ONLY directories in mosra directory above....

Please help me on this


Thanks
Sudhakar

23 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

Submitted by on February 28, 2013 - 5:07pm

If I understand the question, this should work:

find . -name mosra -type d -exec ls {} \;

However ... a problem with that command is that it's like issuing this command from
the directory above the mosra directory:

ls mosra

That command will list all of the files in the mosra folder, which is probably what
you're seeing. To eliminate the output of those filenames, add the -d flag to your ls
command:

find . -name mosra -type d -exec ls -d {} \;

However, at this point, unless you want more detailed information about the mosra
folder, such as an ls -l command, there's no point in using ls; just use this
command:

find . -name mosra -type d

(If you're on an older Unix system you may also need the -print argument.)

I hope that helps.

24 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

Submitted by Sudhakar S (not verified) on February 28, 2013 - 9:02pm

Hello Alvin,

Thank you very much for the reply and for yor time.
I am grateful to you for your reply.

The command:
find . -name mosra -type d -exec ls {} \;
lists all the directories and files in mosra directory found. However I want to list
ONLY the directories that are present in the mosra directory once it is found.

The command:
find . -name mosra -type d -exec ls -d {} \;
gives the relative path of the mosra directory not the contents in mosra directory. In
fact I want only the directories present in the mosra not the files.
Basically I want the directories present in mosra after finding it in the current path.

Hope I am clear in presenting my requirements..

Sudhakar

Submitted by on March 1, 2013 - 8:53am

25 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

Depending on your exact needs you may want to do that in a couple of lines in a shell
script, but you can also get a listing of all directories under the mosra directory by
executing one find command inside the other, like this:

find `find . -type d -name mosra` -type d

Add new comment


Your name

Email

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

Homepage

Subject

Comment

About text formats


Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li>
<pre>
Lines and paragraphs break automatically.

By submitting this form, you accept the Mollom privacy policy.

26 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

Links:
front page me on twitter search privacy

java
java applets
java faqs
misc content
java source code
test projects
lejos

Perl
perl faqs
programs
perl recipes
perl tutorials

Unix
man (help) pages
unix by example
tutorials

source code
warehouse
java examples

27 of 28 5/27/17, 10:35 PM
A collection of Unix/Linux find command examples | alvinalexander.com http://alvinalexander.com/unix/edu/examples/find.shtml

drupal examples

misc
privacy policy
terms & conditions
subscribe
unsubscribe
wincvs tutorial
function point
analysis (fpa)
fpa tutorial

Other
mobile website
rss feed
my photos
life in alaska
how i sold my business
living in talkeetna, alaska
my bookmarks
inspirational quotes
source code snippets

28 of 28 5/27/17, 10:35 PM

You might also like