Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

find (Unix)

From Wikipedia, the free encyclopedia
Shell command for finding files
icon
This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed.
Find sources: "Find" Unix – news ·newspapers ·books ·scholar ·JSTOR
(June 2016) (Learn how and when to remove this message)
find
Original authorDick Haight
DeveloperAT&T Bell Laboratories
Operating systemUnix,Unix-like,Plan 9,IBM i
PlatformCross-platform
TypeCommand

find is ashellcommand that locatesfiles[a] based on search criteria and performs actions on the matching files such as printing thefile systempath to thestandard output. It starts searching at adirectory in a hierarchical structure and recursively traverses thetree although can be limited to a maximum number of levels. Commonly used search criteria includefile namepattern matching,file type matching, and time range matching for last modification or last access. With no arguments, the command outputs the path of each file in the tree rooted at theworking directory. The command can search through different file systems of partitions belonging to one or more storage devices mounted under the starting directory.[1] The command is available on mostUnix-like systems.

Variants

[edit]

The command first appeared inVersion 5 Unix as part of theProgrammer's Workbench project, and was written by Dick Haight alongsidecpio,[2] which were designed to be used together.[3]

TheGNU implementation was originally written by Eric Decker. It was later enhanced by David MacKenzie, Jay Plett, and Tim Wood.[4] The GNU implementation provides features beyond the POSIX specification.

The command was ported to theIBM i operating system.[5]

TheBusyBoxcomputer program provides thefind command and many other commands in a single executable file.

Use

[edit]

The syntax of the command can be described as:

$ find [-H|-L] path... [expression]

Traditionally, at least one path must precede the expression, but newer versions allow for no path argument, defaulting to the working directory.

The expression can specify various aspects including match criteria and actions to perform on matched files. Expression elements are separated bywhitespace and are evaluated left-to-right. The command can match viawildcard characters but wildcard match text must be quoted to preventshell globbing. If the expression is omitted, then all files are matched.

Expression

[edit]

The expression specifies the behavior of the command including what files to select (sometimes called predicate) and what to do with each matching file (sometimes called action). The elements of an expression commonly include:

-namepattern
Selects files with a name that matches a shell-glob pattern.
-typetype
Selects files that are a specificUnix file type:
b:block device (buffered)
c:character device (unbuffered)
d:directory
f:regular file
l:symbolic link
p:named pipe
s:socket
D:door
-print
Prints the name of the found file plus a newline to thestandard output. If the expression does not contain an action option (such as-print0,-exec, or-ok) the action of-print is performed.
-print0
Prints the name of the found file plus anull character to the standard output. Not required by POSIX.
-execprogram [argument...] ;
Runs a program with arguments for each candidate file, selecting files for which the program results inexit status 0. Ifprogram or an argument is{}, it is replaced with the candidate path. POSIX doesn't specify what happens if multiple{} are specified. Most implementations replace each{} with the candidate path.
-execprogram [argument...] {} +
Runs a program with arguments followed by as many candidate paths as possible. Multiple commands are run if the maximum command-line size is exceeded, like forxargs).[6]
-okprogram [argument...] ;
For each candidate path, prompts the user for confirmation. If the user confirms (typically by enteringy oryes), it behaves like-execprogram [arguments...] ;, otherwise the command is not run for the candidate path and the file is not selected.
-maxdepth
Limits the directory depth to search. For example,-maxdepth 2 limits searching to the root directory and its direct children.
-mtime,-ctime,-atime
Modification time,inode change time and access time, respectively. Selects files with time metadata matching a number of days ago. With a+ prefix, the number specifies at least and with a- prefix, the number specifies less than.
-daystart
Measure time from the beginning of a day rather than the last 24 hours.
-newer [file],-cnewer [file],-anewer [file]
Selects files more recently modified than a file. These support a-not prefix for inverse results or range.
-newermt YYYY-MM-DD
Selects files with last modified after date. Also supports a-not prefix for modified before date.

Operators

[edit]

Operators are used to combine expression elements. Operators are listed in order of decreasing precedence:

Precedence
( expr ); Selects evaluation order of subexpression.
Negation
! expr; true ifexpr is false.
Logical and
expr1 expr2 orexpr1 -a expr2;expr2 is not evaluated ifexpr1 is false.
Logical or
expr1 -o expr2;expr2 is not evaluated ifexpr1 is true.

Symbolic link traversal

[edit]

In light of the fact that a file system can contain looped structures viahard andsoft links, POSIX requires that the command detect infinite loops, i.e. entering a previously visited directory that is an ancestor of the last file encountered. When it detects an infinite loop, the command must write a diagnostic message to standard error plus either recover its position in the hierarchy or terminate.

The-H and-L options, specified in the POSIX standard, control how the command handlessymbolic links. The default behavior is tonot follow symbolic links. The-L option causes the command to follow symbolic links. The-H option causes the command to follow symbolic links while processing the command line arguments.[6] A common extension is the-P option, for explicitly disabling symlink following.[7][8]

Examples

[edit]

Search by wildcard

[edit]

The following command searches the current working directory tree for files named starting with "my". The single quotes avoid the shell expansion. Without them, the shell would replace "my*" with the list of files whose names begin with "my" in the current working directory which is not necessarily the same as the files matching in subdirectories.

$find.-name'my*'

Limit file type

[edit]

The following command includes-type f to limit results to regular files, excluding other file system items such as directories and symbolic links.

$find.-typef

Include file detail

[edit]

The following command includes the-ls action option to include detailed file information like from commandls -a.

$find.-ls

Exclude subdirectory tree

[edit]

The following command searches every directory except the subdirectory tree/not/this/one as specified by the-prune option, for a regular file namedmyfile.

$find/-path/not/this/one-prune-o-typef-namemyfile-print

Search multiple directories

[edit]

The following command searches thedira anddirb subdirectory trees for files namedmyf.

$finddiradirb-namemyf

Find any one of differently named files

[edit]

This following command finds files with a name ending with either "jsp" or "java" by combining two-name options with an-o option. The sub-expression (-name '*jsp' -o -name '*java') is enclosed in parentheses which are each escaped with a backslash to prevent them from being interpreted as special shell characters.

$find.\(-name'*jsp'-o-name'*java'\)

Execute a program

[edit]

The following command changes thepermissions of all files with names matching "*.mp3" in the directory tree "/var/ftp/mp3". The action is specified by-execchmod 644 {} \;. For every matching file, the commandchmod 644 {} is executed by{} replaced with the name of the file. The semicolon (with backslash prefix to avoid the shell interpreting it as a command separator) indicates the end of the command. In some shells, the{} must be quoted. The trailing; is often escaped with a leading\, but can be enclosed in single quotes instead.

$find/var/ftp/mp3-name'*.mp3'-typef-execchmod644{}\;

Delete files

[edit]

The following command deletes empty files and print the names. The-delete option is an extension, originally added in some BSD-family operating systems,[9] and present inFreeBSD 2.2.1 and later,[10]NetBSD 5.0 and later,[11]OpenBSD 6.1 and later,[12]DragonFly BSD[13] GNU find,[14] andmacOS.[15]

$find.-empty-delete-print

Search by owner

[edit]

The following command searches file owned by user 123.

$find.-user123

Ignore case

[edit]

The following command matches file names ignoring case. The-iname option is not POSIX required.

$find.-iname'MyFile*'

If the-iname switch is not supported on your system then workaround techniques may be possible such as:

$find.-name'[mM][yY][fF][iI][lL][eE]*'

Search by size

[edit]

The following command searches for files sized between 100 kilobytes and 500 kilobytes.

$find.-size+100k-a-size-500k

Search by time

[edit]

The following command searches for files in the document folder modified in the last week.

$find~/Documents/-mtime-7

The following command finds files last edited in February 2017.

$find~/Documents/-newermt2017-02-01-not-newermt2017-03-01

The following command lists files edited more recently than "document.txt".

$find~/Documents/-newerdocument.txt

Related utilities

[edit]
grep
A command for searching plain-text for lines matching a regular expression.
find
A command on Microsoft-based systems that although has the same name, provides significantly different functionality than the Unix-based command.
dir
A commonly-used command for listing files on Microsoft-based systems. It provides the/s option to recursively search for files or directories.
tree
A command on Microsoft-based systems that recursively lists files of a directory tree, indenting the file names according to their position in the file hierarchy.
walk andsor
Commands onPlan 9 from Bell Labs systems that provide similar functionality asfind.walk finds files in a directory tree and prints the names andsor filters (likegrep) by evaluating expressions in the form of a shell script. The commands are not part ofPlan 9 from User Space, so Google's Benjamin Barenblat has a ported version to POSIX systems available through GitHub.[16]
fd
An alternative tofind written inRust.[17]
locate
A tool that searches a prebuiltdatabase instead of the file system. The performance oflocate can exceed that offind, but results can be inaccurate if the database is out-of-date. Typically, the database is updated from file system information via afind command run periodically by acron job.

See also

[edit]

Notes

[edit]
  1. ^ Per normal Unix terminology, afile is a file system entry, which can be of anyUnix file type, including normal file, directory, or other more-specialized types.

References

[edit]
  1. ^"find(1) – Linux manual page".man7.org.Archived from the original on 2019-11-11. Retrieved2019-11-19.
  2. ^McIlroy, M. D. (1987).A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986(PDF) (Technical report). CSTR. Bell Labs. 139.
  3. ^"libarchive/libarchive".GitHub.Archived from the original on 2022-04-30. Retrieved2015-10-04.
  4. ^"Finding Files".Archived from the original on 2022-05-11. Retrieved2020-07-18.
  5. ^"IBM System i Version 7.2 Programming Qshell"(PDF).IBM.Archived(PDF) from the original on 2020-09-18. Retrieved2020-09-05.
  6. ^abfind: find files – Shell and Utilities Reference,The Single UNIX Specification, Version 5 fromThe Open Group
  7. ^find(1) – FreeBSD General CommandsManual
  8. ^find(1) – Linux UserManual – User Commands from Manned.org
  9. ^"10.1.6 Using the -delete action".GNU Findutils.Archived from the original on 2022-05-11. Retrieved2025-12-06.
  10. ^"FreeBSD 2.2.1 find(1)". April 1, 1994.
  11. ^"NetBSD 5.0 find(1)". July 19, 2007.Archived from the original on June 27, 2025. RetrievedDecember 6, 2025.
  12. ^"OpenBSD 6.1 find(1)". January 3, 2017.
  13. ^"DragonFly BSD find(1)".
  14. ^"3.4 Delete Files".GNU Findutils.Archived from the original on 2022-05-11. Retrieved2025-12-06.
  15. ^"macOS find(1)". -delete.Archived from the original on 2024-11-08. Retrieved2025-12-06.
  16. ^"google / walk: Plan 9 style utilities to replace find(1)".GitHub.Archived from the original on 8 November 2020. Retrieved30 March 2020.
  17. ^Peter, David (30 March 2020)."sharkdp/fd: A simple, fast and user-friendly alternative to 'find'".GitHub.Archived from the original on 13 April 2020. Retrieved30 March 2020.

External links

[edit]
The WikibookGuide to Unix has a page on the topic of:Commands
File system
Processes
User environment
Text processing
Shell builtins
Searching
Documentation
Software development
Miscellaneous
Retrieved from "https://en.wikipedia.org/w/index.php?title=Find_(Unix)&oldid=1336334248"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp