ArticleslgStudy

science

Find (Unix)

Find (Unix) is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Find (Unix) rather than just read about it. In short: find is a shell command that locates files based on search criteria and performs actions on the matching files such as printing the file system path to the standard output. It starts searching at a directory in a hierarchical structure and recursively traverses the tree although can be limited to a maximum number of levels.

Key takeaways

  • Find (Unix) belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Find (Unix) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Find (Unix) from memory before moving on to harder problems.

Reference excerpt

find is a shell command that locates files based on search criteria and performs actions on the matching files such as printing the file system path to the standard output. It starts searching at a directory in a hierarchical structure and recursively traverses the tree although can be limited to a maximum number of levels. Commonly used search criteria include file name pattern 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 the working directory. The command can search through different file systems of partitions belonging to one or more storage devices mounted under the starting directory. The command is available on most Unix-like systems.

Variants The command first appeared in Version 5 Unix as part of the Programmer's Workbench project, and was written by Dick Haight alongside cpio, which were designed to be used together. The GNU implementation was originally written by Eric Decker. It was later enhanced by David MacKenzie, Jay Plett, and Tim Wood. The GNU implementation provides features beyond the POSIX specification. The command was ported to the IBM i operating system. The BusyBox computer program provides the find command and many other commands in a single executable file.

Use The syntax of the command can be described as:

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 by whitespace and are evaluated left-to-right. The command can match via wildcard characters but wildcard match text must be quoted to prevent shell globbing. If the expression is omitted, then all files are matched.

Expression

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:

-name pattern Selects files with a name that matches a shell-glob pattern. -type type Selects files that are a specific Unix 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 the standard 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 a null character to the standard output. Not required by POSIX. -exec program [argument...] ; Runs a program with arguments for each candidate file, selecting files for which the program results in exit status 0. If program 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. -exec program [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 for xargs). -ok program [argument...] ; For each candidate path, prompts the user for confirmation. If the user confirms (typically by entering y or yes), it behaves like -exec program [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 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 if expr is false. Logical and expr1 expr2 or expr1 -a expr2; expr2 is not evaluated if expr1 is false. Logical or expr1 -o expr2; expr2 is not evaluated if expr1 is true.

Symbolic link traversal In light of the fact that a file system can contain looped structures via hard and soft 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 handles symbolic links. The default behavior is to not 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. A common extension is the -P option, for explicitly disabling symlink following.

Examples

Search by wildcard 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.

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

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

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

Search multiple directories The following command searches the dira and dirb subdirectory trees for files named myf.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Find (Unix)

Start with the simplest possible case. Write down what Find (Unix) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Find (Unix) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Find (Unix) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Find (Unix)

In research
Find (Unix) appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Find (Unix) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Find (Unix) is common in secondary-school and first-year university syllabi. It links to neighbouring topics IBM i Qshell commands, Information retrieval systems, Plan 9 commands, so understanding it makes those chapters shorter.
In everyday life
Look for Find (Unix) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Find (Unix)” →

How to study Find (Unix) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Find (Unix) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Find (Unix) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Find (Unix) in simple terms?

find is a shell command that locates files based on search criteria and performs actions on the matching files such as printing the file system path to the standard output. It starts searching at a directory in a hierarchical structure and recursively traverses the tree although can be limited to a…

Why does Find (Unix) matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Find (Unix)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Find (Unix).

Tags

  • IBM i Qshell commands
  • Information retrieval systems
  • Plan 9 commands
  • Standard Unix programs
  • Unix SUS2008 utilities

Keep exploring