Finding find - A Reference
Luis F. Guzmán
luis.guzman@att.net
Central Indiana Linux Users Group
www.cinlug.org
Table of Contents
- Introduction
- Find files with a given name
- Find a file using a shell pattern
- Find a file using a regular expression
- Search for a string
- Change owner/group on a directory sub-tree
- Find file types
- Find files with different modification, access, and status times
- Find files with specific permissions
- Find files with specific permissions - wildcards
- During search, ignore a directory and files under it
Introduction
The find command is one of the most useful and flexible.
It can greatly simplify administrative tasks, such as,
deleting files that are older than a particular date or
modifying the owner and group of files in many directories.
It can also find files that have been modified after a
particular date or time. It can find executable files, read-only
files, block files, character files, directory files, etc.
However, due to its many options, tests, actions, and operators,
it can be intimidating.
These examples are meant to serve as a reference.
They illustrate some of the most common features of find.
It is assumed that the reader knows the basics of the Linux/UNIX
shell, as well as the basics of the find command.
This document is a complement of the man pages for find;
it is not a substitute.
Find files with a given name
In the current directory sub-tree, find all files
named "resume.ps:"
$ find . -name resume.ps -print
In the current directory sub-tree,
Remove all files named "core:"
$ find . -name core -type f -exec rm {} \;
Find a file using a shell pattern
In the current directory sub-tree, find all files
which name ends with .c or .C:
$ find . -name '*.[cC]' -print
In the current directory sub-tree,
find and compress files that don't end with .Z:
$ compress `find . -type f \! -name '*.Z' -print`
Find a file using a regular expression
In the current directory sub-tree,
find any files which path includes "RCS" and ends
with "tcl,v:"
$ find . -regex '.*RCS.*tcl,v$' -print
Search for a string
Search two directory sub-trees, /src and /local/src, for files
containing the string "Where are you" and print the file names:
$ find /src /local/src -exec grep -l 'Where are you' {} \;
Change owner/group on a directory sub-tree
Change owner and group for all files and directories in a directory
sub-tree to owner "luis" and group "users."
$ find /subtree/path -exec chown luis:users {} \;
Find file types
Change permissions on all regular files in a directory
sub-tree to mode 444:
$ find /subtree/path -type f -print | xargs chmod 444
Change permissions on all directories in a directory
sub-tree to mode 555:
$ find /subtree/path -type d -print | xargs chmod 555
Find files with different modification, access, and status times
Find all files in the current directory sub-tree that
had their status changed one day ago:
$ find . -ctime 1 -print
Find all files in the current directory sub-tree that
have been modified in the last 90 minutes:
$ find . -mmin -90 -print
Remove all files named a.out or *.o that have not been
accessed for a week:
$ find / \( -name a.out -o -name '*.o'\) -atime +7 -exec rm {} \;
Find files with specific permissions
Find files which permissions match 444 (r--r--r--):
$ find -perm 444 -print
Find files with specific permissions - wildcards
Find files with permissions that match: **x******, where * can
be any mode:
$ find . -perm -100 -print
Find files with permissions that match: r**r**r**, where * can
be any mode:
$ find . -perm -444 -print
Find files with permissions that match: **x***rw*, where * can
be any mode:
$ find . -perm -106 -print
Find files which are executable by their user, group, or others
(any of their executable permission bits is set):
$ find . -perm +111 -print
Find files which are executable or writable by others:
$ find . -perm +003 -print
Find files with permissions that match: ***r-x***, where * can
be any mode:
$ find . -perm -050 ! -perm +020 -print
Find files with permissions that match ***r--***, where * can
be any mode:
$ find . -perm -040 ! -perm +030 -print
During search, ignore a directory and files under it
Skip RCS directories, but list remaining files with
permissions 444 (r--r--r--):
$ find . -path '*RCS*' -prune -o -perm 444 -print
Note: the -print option is important in the
example above. It does not work properly without it?!
Go to
Author's home page
This document is maintained by:
Luis F. Guzmán