Skip to main content
Engineering LibreTexts

05-B.5.1: Searching for Files on Linux - find Command

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The find Command

    The find command in UNIX is a command line utility for walking a file hierarchy. It can be used to find files and directories and perform subsequent operations on them. It supports searching by file, folder, name, creation date, modification date, owner and permissions. By using the ‘-exec’ other UNIX commands can be executed on files or folders found.

    Find is a powerful tool, yet it can be a bit confusing at first. This video discusses some of the more common uses of find, and will show some examples.

    Syntax :

    $ find [where to start searching from]
     [expression determines what to find] [-options] [what to find]

    find Command Options :

    Option Option Meaning
    -exec CMD The file being searched which meets the above criteria and returns 0 as its exit status for successful command execution.
    -ok CMD It works the same as -exec except the user is prompted first.
    -print Display the path name of the files found by using the rest of the criteria.
    -inum N Search for files with inode number ‘N.’
    -links N Search for files with ‘N’ links.
    -name demo Search for files that are specified by ‘demo.’
    -newer file Search for files that were modified/created after ‘file.’
    -perm octal Search for the file if permission is ‘octal.’
    -empty Search for empty files and directories.
    -size +N/-N Search for files of ‘N’ blocks; ‘N’ followed by ‘c’ can be used to measure size in characters; ‘+N’ means size > ‘N’ blocks and ‘-N’ means size < 'N' blocks.
    -user name Search for files owned by user name or ID ‘name.’
    \(expr \) True if ‘expr’ is true; used for grouping criteria combined with OR or AND.
    ! expr True if ‘expr’ is false.

    find versus locate

    The find command searches in the real system. Even though it is slower it always returns up-to-date output. The find command also has many more options (size, modification time...)

    Whereas the locate command uses a previously built database (the updatedb command usually builds the database once a day). It is much faster, but uses an 'older' database and searches only for file names or parts of the name.

    Adapted from:
    "find command in Linux with examples" by rishabh1322, Geeks for Geeks is licensed under CC BY-SA 4.0

    05-B.5.1: Searching for Files on Linux - find Command is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?