• Home
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All
  • About
Linux-Ubuntu | find -- search files/directories
  1. Notes
  2. Examples
  3. Command Help (man find)
  4. Possible values of find options

  1. Notes
    find path ...

    You might want to ignore access permission errors and redirect them to /dev/null by using the STDERR redirection operator:
    find path ... 2> /dev/null

    To filter out the search result, you can use the following options: -or, -o, -and, -not
    To combine these options and form an expression, you can use the following syntax: \( expression \) (note the space before and after the expression)

    $ find . \( -name file1 -or -name folder1 \) -ls

    $ find . \( -name file1 -o -name folder1 \) -ls

    $ find . \( -name file1 -and -name folder1 \) -ls

    $ find . \( -name file1 -not -name folder1 \) -ls

    You can execute a command on the search result by using the following options: -exec, -ok
    The -exec option executes the command on each file found (or directory), without asking for confirmation.
    The -ok option ask for confirmation before executing the command on each file found (or directory). You can confirm the execution of the command by typing y and pressing Enter otherwise press Enter to skip the execution of the command.

    The syntax for both options is as following:
    $ find . -exec command {} \;

    $ find . -ok command {} \;

    Notice the use of the curly braces that act as a place holder for the found file name (or directory).
    The command line should end with a semicolon (;) or a plus (+) character.
    The semicolon (;) character force the execution of the command for each found file.
    The plus (+) character allow the execution of the command on a group of found files.

    The semicolon (;) character should be escaped with backslash character (\) to avoid that shell interpret the semicolon as a special character (shell commands separator). You can also escape the semicolon (;) character by putting it within double quotes (";") or single quotes (';'). You might also want to escape the plus (+) character with backslash character (\).

    $ find . -exec echo {} \;
    ./folder1
    ./folder2

    $ find . -exec echo {} \+
    ./folder1 ./folder2
  2. Examples
    • Search files:
      $ find . -type f

    • Search directories:
      $ find . -type d

    • Search files/directories with a specific name:

      • Search files with a specific name:
        $ find . -type f -name 'file1'

      • Search directories with a specific name:
        $ find . -type d -name 'folder1'

      • Search files whose names do not end with .c:
        $ find . -type f \! -name "*.c"

      • Search files/directories which are not both files and their names end with .c:
        $ find . \! \( -type f -name "*.c" \)

      • Search files/directories which are either directories or that their names end with .c:
        $ find . \( -type d -or -name "*.c" \)

    • Search files/directories where the name of the file/directory contain a specific pattern:

      • Search files where the name of the file contain a specific pattern:
        $ find . -type f -name '*file*'
        
        $ find . -type f | grep "file"

      • Search directories where the name of the directory contain a specific pattern:
        $ find . -type d -name '*folder*'
        
        $ find . -type d | grep "folder"

    • Search files that contain a specific word:
      $ find . -type f | xargs grep -l "bar"

    • Search files and use the "-exec" command to execute specific commands:

      • Search files where the name of the file contain a specific pattern:
        $ find . -type f -exec ls -al {} \; | grep foo

      • Search files inside jar files:
        $ find . -type f -name "*.jar" -exec jar -tf {} \; | grep "MANIFEST.MF"

      • Search files inside jar files:
        $ for i in `find . -type f -name "*.jar"`; do echo $i; jar -tf $i | grep "MANIFEST.MF"; done

    • Search and delete files/directories:
      $ find . -type f -delete
      
      $ find . -type f -exec rm -f {} \;
      
      $ find . -type d -exec rm -rf {} \;
      
      $ find . -type f -print0 | xargs -0 rm

    • Search files and replace text within these files:
      $ find . -type f -exec sed -i 's/bar/foo/g' {} \;
      
      $ find . -type f -exec sed -i 's/\#\!\/bin\/sh/\#\!\/bin\/bash/g' {} \;

      mac users (OS X):
      $ find . -type f -exec sed -i '' -e 's/bar/foo/g' {} \;

    • Search and rename directories:
      for i in `find . -type d -name "foo"`; do folderName=$(dirname "$i"); mv $folderName/foo $folderName/bar; done

    • Clean-up an eclipse project:
      $ find . -type d -name "target" -exec rm -rf {} \;
      
      $ find . -type d -name ".settings" -exec rm -rf {} \;
      
      $ find . -type f -name ".classpath" -exec rm {} \;
      $ find . -type f -name ".project" -exec rm {} \;
      #$ find . -type f \( -name ".classpath" -o -name ".project" \) -exec rm -rf {} \;
      
      $ find . -type f -name "*~" -exec rm {} \;
      $ find . -type f -name ".directory" -exec rm {} \;
      #$ find . -type f \( -name "*~" -o -name ".directory" \) -exec rm -rf {} \;
      
      $ find . -type d -name ".svn" -exec rm -rf {} \;
  3. Command Help (man find)
    The following options can be used:
    • To search files by type/name/path, use the following options:
      -type t
      |True if the file is of the specified type t.
      
      -name pattern
      |True if the last component of the pathname being examined matches pattern.
      |Special shell pattern matching characters ("[", "]", "*", and "?") may be used as part of pattern.
      |These characters may be matched explicitly by escaping them with a backslash ("\").
      
      -iname pattern
      |Like -name, but the match is case insensitive.
      
      -path pattern
      |True if the pathname being examined matches pattern.
      |Special shell pattern matching characters ("[", "]", "*", and "?") may be used as part of pattern.
      |These characters may be matched explicitly by escaping them with a backslash ("\").
      |Slashes ("/") are treated as normal characters and do not have to be matched explicitly.
      
      -ipath pattern
      |Like -path, but the match is case insensitive.
      
      -regex pattern
      |True if the whole path of the file matches pattern using regular expression.
      |To match a file named "./foo/xyzzy", you can use the regular expression ".*/[xyz]*" or ".*/foo/.*", but not "xyzzy" or "/foo/".
      
      -iregex pattern
      |Like -regex, but the match is case insensitive.
      
      -empty
      |True if the current file or directory is empty.
      
      -depth n
      |True if the depth of the file relative to the starting point of the traversal is n.
      |1 means the starting directory.

    • To search files by time, use the following options:
      -mmin n
      |True if the file is last modified exactly n minutes ago (use +/- for more/less than n minutes).
      
      -mtime [+-]n[smhdw]
      |True if the file is last modified exactly n units ago (use +/- for more/less than n units).
      |Any number of units may be combined in one -mtime, -Btime, -atime, or -ctime argument, for example, '-1h30m'.
      
      -Bmin [+-]n
      |True if the file is created exactly n minutes ago (use +/- for more/less than n minutes).
      
      -Btime [+-]n[smhdw]
      |True if the file is created exactly n units ago (use +/- for more/less than n units).
      
      -amin [+-]n
      |True if the file is last accessed exactly n minutes ago (use +/- for more/less than n minutes).
      
      -atime [+-]n[smhdw]
      |True if the file is last accessed exactly n units ago (use +/- for more/less than n units).
      
      -cmin [+-]n
      |True if the file is last changed exactly n minutes ago (use +/- for more/less than n minutes).
      
      -ctime [+-]n[smhdw]
      |True if the file is last changed exactly n units ago (use +/- for more/less than n units).

    • To search files by size, use the following options:
      -size [+-]n[ckMGTP]
      |True if the file's size is exactly n units (use +/- for more/less than n units).
      |If no unit specified, the file's size, will be rounded up, in 512-byte blocks.

    • To search files by user/group, use the following options:
      -user uname
      |True if the file belongs to the user uname.
      |If uname is numeric and there is no such user name, then uname is treated as a user ID.
      |If the user doesn't exist you get this error: "find: -user: uname: no such user".
      
      -group gname
      |True if the file belongs to the group gname.
      |If gname is numeric and there is no such group name, then gname is treated as a group ID.
      |If the group doesn't exist you get this error: "find: -group: gname: no such group".
      
      -nouser
      |True if the file belongs to an unknown user.
      
      -nogroup
      |True if the file belongs to an unknown group.

    • The following options can be used to execute specific actions:
      -ls
      |Always returns true.
      |The following information for the current file is written to standard output:
      |its inode number, size in 512-byte blocks, file permissions, number of hard links, owner, group, size in bytes, last modification time, and pathname.
      |If the file is a block or character special file, the device number will be displayed instead of the size in bytes.
      |If the file is a symbolic link, the pathname of the linked-to file will be displayed preceded by "->".
      |The format is identical to that produced by "ls -dgils".
      
      -print
      |Always returns true.
      |It prints the pathname of the current file to standard output.
      
      -print0
      |Always returns true.
      |It prints the pathname of the current file to standard output, followed by an ASCII NUL character (character code 0).
      
      -delete
      |Delete found files and/or directories.
      |Always returns true.
      |This executes from the current working directory as find recurses down the tree.
      |It will not attempt to delete a filename with a "/" character in its pathname relative to "." for security reasons.
      |If you search for directories only, the delete action won't delete a directory if it's not empty.
      
      -exec utility [argument ...] ;
      |True if the program named utility returns a zero value as its exit status.
      |Optional arguments may be passed to the utility.
      |The expression must be terminated by a semicolon (";").
      |If you invoke find from a shell you may need to quote the semicolon if the shell would otherwise treat it as a control operator.
      |If the string "{}" appears anywhere in the utility name or the arguments it is replaced by the pathname of the current file.
      |Utility will be executed from the directory from which find was executed.
      |Utility and arguments are not subject to the further expansion of shell patterns and constructs.
      
      -exec utility [argument ...] {} +
      |Same as -exec, except that "{}" is replaced with as many pathnames as possible for each invocation of utility.
      |This behaviour is similar to that of xargs.
      
      -ok utility [argument ...] ;
      |Same as -exec, except that find requests user affirmation for the execution of the utility by printing a message to the terminal and reading a response.
      |If the response is not affirmative ('y'), the command is not executed and the value of the -ok expression is false.

    • The following operators (listed in order of decreasing precedence) can be combined with options:
      ( expression )
      |This evaluates to true if the parenthesized expression evaluates to true.
      
      ! expression
      -not expression
      |This is the unary NOT operator.
      |It evaluates to true if the expression is false.
      
      -false
      |Always false.
      
      -true
      |Always true.
      
      expression -and expression
      expression expression
      |The -and operator is the logical AND operator.
      |As it is implied by the juxtaposition of two expressions it does not have to be specified.
      |The expression evaluates to true if both expressions are true.
      |The second expression is not evaluated if the first expression is false.
      
      expression -or expression
      |The -or operator is the logical OR operator.
      |The expression evaluates to true if either the first or the second expression is true.
      |The second expression is not evaluated if the first expression is true.
  4. Possible values of find options
    • All options that take a numeric argument (n) allow the number to be preceded by a plus sign (+) or a minus sign (-):
      +{n}: a plus sign (+) means 'more than n' (+3: more than 3)
      -{n}: a minus sign (-) means `less than n' (-3: less than 3)
      {n}: means 'exactly n' (3: exactly 3)

    • Possible time units [smhdw] are as follows:
      s: second
      m: minute (60 seconds)
      h: hour (60 minutes)
      d: day (24 hours) (default)
      w: week (7 days)

    • Possible size units [ckMGTP] are as follows:
      c: bytes
      k: kilobytes (1024 bytes)
      M: megabytes (1024 kilobytes)
      G: gigabytes (1024 megabytes)
      T: terabytes (1024 gigabytes)
      P: petabytes (1024 terabytes)

    • Possible file types are as follows:
      f: regular file
      d: directory
      l: symbolic link
      c: character special
      b: block special
      p: FIFO
      s: socket
© 2025  mtitek