Jump to content

Find

From Squishu Wiki

General

  • searches for files
  • do it as root
  • While find looks for files, grep searches files for strings
    • Often the exec command for find is grep
    • general regular expression parser

Syntax

  • find [path] [options] [tests] [actions]

Path

  • path
    • can use absolute or relative paths
    • can supply multiple paths like this
      • find /var/home

Search in the current directory: use a dot: .

options

  • -depth
    • ???
  • -follow
    • follow symlinks
  • -maxdepths N
    • Search at most N levels of directory when searching
  • -mount or -xdev
    • don't search directories on other systems

tests

  • each test returns true or false
  • find considers each file it finds and applies each test in the order they were defined
  • If a tests returns false, find stops considering the file it's on, and moves on
  • If a test returns true, find will process the next test or action on the current file
  • Theses are some tests
    • -atime N
      • the file was last accessed N days ago
    • -mtime N
      • The file ws last modified N days ago
    • -name pattern
      • the name of the file excluding any path
      • if you use a pattern for the filename, you must use quotes so that the name isn't expanded by the shell, but passed to find
        • This pattern must always be in quotes
    • -type C
      • The file is of a certain type C which could be
        • d for directory
        • f for a regular file
    • -user username
      • file is owned by this user
  • tests can be combined with operators
  • use parens as needed, but you need to escape them with a backslash
    • \(-newer X -o -name "_*" \)
      • if you use a pattern for the filename, you must use quotes so that the name isn't expanded by the shell, but passed to find


operators

  • invert the test
    • short form !
    • long form -not
  • both tests must be true
    • short form -a
    • long form -and
  • either test must be true
    • short form -o
    • long form -or


Examples

  • find / -name blah -print
    • start at root and search for a file named blah and then print out the name of the file
  • find / -mount -name blah -print
    • start at root and search for a file named blah and then print out the name of the file, but don't search mounted directories
  • find . \( -name "_*" -or -newer blah \) -type f -print
    • above the parens were escped, and the wilcard pattern for the name was in quotes. Finds files in the local directory that start with _ or are newer than the file named blah
  • find . -newer blah -type f -exec ls -l {} \;
    • look in the current directory for files newer than blah and long list them

Actions

  • -exec command
    • Execute a command
  • -ok command
    • like -exec except it prompts for user confirmation of each file on which it will carry out the command before executing it
  • -prnt
    • prints out the name of the file
  • -ls
    • Performs ls -dils on the current file
  • -exec and -ok take subsequent params until terminated with a \;
  • magic string
    • the magic string is {}. It's a param for -exec or -ok and it's replaced with the full path to the current file