Category

Linux Command


Usage

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]


Manual

Required arguments

  • path...: path(s) to start the search from
  • expression: expression describing what to search for

Options

Options for the treatment of symbolic links
  • -H: never follow symbolic links (default behavior)
  • -L: follow symbolic links
  • -P: do not follow symbolic links except while processing the command line arguments
Misc options
  • -D: debug options to print diagnostic information; accepts comma-separated list of options (debugopts)
    • help: Explain the debugging options
    • tree: Show the expression tree in its original and optimized form
    • stat: Print messages as files are examined with the stat and lstat system calls
    • opt: Prints diagnostic information relating to the optimization of the expression tree
    • rates: Prints a summary indicating how often each predicate succeeded or failed
  • -O: enables query optimization with specified level (level)
    • 0: Equivalent to optimization level 1
    • 1: Default optimization level, corresponds to the traditional behavior
    • 2: File type tests are performed after tests based only on the names of files
    • 3: Full cost-based query optimizer is enabled
Expression options
  • -d: A synonym for -depth, for compatibility with FreeBSD, NetBSD, MacOS X and OpenBSD.
  • -daystart: Measure times (for -amin, -atime, -cmin, -ctime, -mmin, and -mtime) from the beginning of today rather than from 24 hours ago. This option only affects tests which appear later on the command line.
  • -depth: Process each directory's contents before the directory itself. The -delete action also implies -depth.
  • -follow: Deprecated; use the -L option instead. Dereference symbolic links. Implies -noleaf. The -follow option affects only those tests which appear after it on the command line. Unless the -H or -L option has been specified, the position of the -follow option changes the behavior of the -newer predicate; any files listed as the argument of -newer will be dereferenced if they are symbolic links. The same consideration applies to -newerXY, -anewer and -cnewer. Similarly, the -type predicate will always match against the type of the file that a symbolic link points to rather than the link itself. Using -follow causes the -lname and -ilname predicates always to return false.
  • --help: Print a summary of the command-line usage of find and exit.
  • --ignore_readdir_race: Normally, find will emit an error message when it fails to stat a file. If you give this option and a file is deleted between the time find reads the name of the file from the directory and the time it tries to stat the file, no error message will be issued. This also applies to files or directories whose names are given on the command line. This option takes effect at the time the command line is read, which means that you cannot search one part of the filesystem with this option on and part of it with this option off (if you need to do that, you will need to issue two find commands instead, one with the option and one without it).
  • -maxdepth: Descend at most levels (a non-negative integer) levels of directories below the command line arguments. -maxdepth 0 means only apply the tests and actions to the command line arguments.
  • -mindepth: Do not apply any tests or actions at levels less than levels (a non-negative integer). -mindepth 1 means process all files except the command line arguments.
  • -mount: Don't descend directories on other filesystems. An alternate name for -xdev, for compatibility with some other versions of find.
  • --noignore_readdir_race: Turns off the effect of --ignore_readdir_race.
  • -noleaf: Do not optimize by assuming that directories contain 2 fewer subdirectories than their hard link count. This option is needed when searching filesystems that do not follow the Unix directory-link convention, such as CD-ROM or MS-DOS filesystems or AFS volume mount points. Each directory on a normal Unix filesystem has at least 2 hard links: its name and its '.' entry. Additionally, its subdirectories (if any) each have a '..' entry linked to that directory. When find is examining a directory, after it has statted 2 fewer subdirectories than the directory's link count, it knows that the rest of the entries in the directory are non-directories ('leaf' files in the directory tree). If only the files' names need to be examined, there is no need to stat them; this gives a significant increase in search speed.
  • -regextype: Changes the regular expression syntax understood by -regex and -iregex tests which occur later on the command line. Currently-implemented types are emacs (this is the default), posix-awk, posix-basic, posix-egrep and posix-extended.
  • --version: Print the find version number and exit.
  • -warn, --nowarn: Turn warning messages on or off. These warnings apply only to the command line usage, not to any conditions that find might encounter when it searches directories. The default behavior corresponds to -warn if standard input is a tty, and to --nowarn otherwise.
  • -xautofs: Don't descend directories on autofs filesystems.
  • -xdev: Don't descend directories on other filesystems.

 

Examples

Find files with specific extension and compress them

Find all files with the ".bdg" extension and compress them using gzip, you can use the find command in combination with gzip.

$ find /path/to/search -type f -name "*.bdg" -exec gzip {} \; 

Breakdown of the command:

  1. find: This command is used to search for files and directories within a specified directory hierarchy.
  2. /path/to/search: This is the starting point for the search. Replace it with the directory where you want to begin searching for files with the ".bdg" extension.
  3. -type f: This option instructs find to only consider regular files (not directories or other types of files).
  4. -name "*.bdg": This option specifies the pattern to match against the filenames. In this case, *.bdg matches any filename that ends with ".bdg".
  5. -exec gzip {} \;: This part of the command tells find to execute the gzip command on each file that matches the search criteria. Here's how it works:
    • {} is a placeholder that gets replaced by each matching filename.
    • \; marks the end of the -exec command.

 


Share your experience or ask a question