fortypoundhead.com

FIND

Posted On 2007-05-01 by FortyPoundHead
Keywords: Command Reference
Tags: Linux Commandline Linux
Views: 1575


Search a folder hierarchy for filename(s) that meet a desired criteria: Name, Size, File Type etc.

SYNTAX
find [path...] [options] [tests] [actions]
find searches the directory tree starting at the given pathname (or pathnames if several are given)

by evaluating the given expression from left to right, according to the rules of precedence, until the outcome is known (the left hand side is false for AND operations, true for OR), at which point find moves on to the next file name. (see Operators)

Conditions may be grouped by enclosing them in \( \) (escaped parentheses), negated with !, given as alternatives by separating them with -o, or repeated (adding restrictions to the match; usually only for -name, -type, or -perm). Note that "modification" refers to editing of a file''s contents, whereas "change" means a modification, or permission or ownership changes. In other words, -ctime is more inclusive than -atime or -mtime.

The first argument that begins with a - or ( or ) or , or ! is taken to be the beginning of the expression; any arguments before this are paths to search, and any arguments after it are the rest of the expression.

If no paths are given, the current directory is used. If no action is given, `-print'' is used.

find exits with status 0 if all files are processed successfully, greater than 0 if errors occur.

Examples

List all filenames ending in .mp3, searching in the current folder and all subfolders:

find . -name "*.mp3"

List all filenames ending in .mp3, searching in the music folder and subfolders:

find ./music -name "*.mp3"

List files with the exact name: Sales_document.doc in ./work and subfolders:

find ./work -name Sales_document.doc

List all files that belong to the user Simon:

find . -user Simon

List all the directory and sub-directory names:

find . -type d

List all files in those sub-directories (but not the directory names)

find . -type f

List all the file links:

find . -type l

List all files (and subdirectories) in your home directory:

find $HOME

Find files that are over a gigabyte in size:
find ~/Movies -size +1024M

Find files have been modified within the last day:
find ~/Movies -mtime -1

Find files have been modified within the last 30 minutes:
find ~/Movies -mmin -30

Find .doc files that also start with ''questionnaire'' (AND)

find . -name ''*.doc'' -name questionnaire*

List all files beginning with ''memo'' and owned by Simon (AND)

find . -name ''memo*'' -user Simon

Find .doc files that do NOT start with ''Accounts'' (NOT)

find . -name ''*.doc'' ! -name Accounts*

Search for files which have read and write permission for their owner,
and group, but which the rest of the world can read but not write to.

find . -perm 664

Files which meet these criteria but have other permissions bits set
(for example if someone can execute the file) will not be matched.

Search for files which have read and write permission for their owner,
and group, but which the rest of the world can read but not write to,
without regard to the presence of any extra permission bits
(for example the executable bit).

find . -perm -664

This will match a file which has mode 0777, for example.

Search for files which are writeable by somebody (their owner, or their group, or anybody else).

find . -perm +222
or
find . -perm +g+w,o+w
or
find . -perm +g=w,o=w

All three of these commands do the same thing, but the first one uses
the octal representation of the file mode, and the others use the symbolic form.
The files don''t have to be writeable by both the owner and group to be matched; either will do.

Search for files which are writeable by both their owner and their group:

find . -perm -022
or
find . -perm -g+w,o+w


About the Author

FortyPoundHead has posted a total of 1974 articles.

 


Comments On This Post

No comments on this post yet!


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:54.158.195.221

Before you can post, you need to prove you are human. If you log in, this test goes away.



Recent Forum Posts

Malicious IP Checker Companion Tool
dwirch posted on August 12, 2017 at about 20:24 in Site News

Job Spammer: Steve Adams
dwirch posted on August 8, 2017 at about 7:44 in Spammers

New security implemented
dwirch posted on August 8, 2017 at about 6:43 in Site News

New security implemented
dwirch posted on July 23, 2017 at about 6:58 in Site News

Fold Code Manager into main KB?
VB6Boy posted on July 22, 2017 at about 14:42 in Site News

Fold Code Manager into main KB?
dwirch posted on July 22, 2017 at about 14:41 in Site News