Find

From Colettapedia
Jump to navigation Jump to search

Find

Symbolic Links

  • The -H, -L and -P options control the treatment of symbolic links.
  • -P - Never follow symbolic links. This is the default behaviour.
  • -L - Follow symbolic links.
  • -H - Do not follow symbolic links, except while processing the command line arguments dereference any top-level dirs directly mentioned in the command arguments, otherwise no.

Deleting files

  • find . -name "*.sig" | xargs -i rm "{}"
    • used on a linux box when there was spaces in the path
  • find . -name "*.foo" -exec rm {} \;
    • note the "{}" token, the escaped semicolon, and the space between them.

Find file by type

  • find . -type f -executable
    • only works in GNU
  • f - regular file
  • d - directory
  • l - symbolic link - does not dereference, use -xtype to do that.

Exclude files whose paths match a pattern

  • find . -not -path "*git*" -a -not -path "*md*" -a -not -path "*__init__*"
  • Dump contents of files to stdout for possible redirection to file:
    • find . -not -path "*git*" -a -not -path "*md*" -a -not -path "*__init__*" -a -not -type d -exec sh -c "echo \"\n\n\n###############\"; echo \"{}\n\"; cat {}" \;

Find based on date

  • find . -newerXY
    • find . -neweraa test_file - list all files whose ACCESS time is more recent than access time of test_file
    • find . -newerBa test_file - list all files whose CREATION time is more recent than access time of test_file
    • find . -newerca test_file - list all files whose CHANGE time is more recent than access time of test_file
    • find . -newerma test_file - list all files whose MODIFICATION time is more recent than access time of test_file
    • find . -neweraB test_file - list all files whose access time is more recent than CREATION time of test_file
    • find . -newerac test_file - list all files whose access time is more recent than CHANGE time of test_file
    • find . -neweram test_file - list all files whose access time is more recent than MODIFICATION time of test_file

Find and run an inline shell script

  • find . -type d -maxdepth 1 -exec sh -c "echo {}; find {} | wc -l" \;


fd

  • fd
  • brew install fd
  • Simple, fast and user-friendly alternative to find
  • Uses regular expressions for the pattern by default
  • Change to glob using --glob option
  • Excludes hidden files, directories, .gitignore, .ignore by default
  • -t - type eg. -t f
  • -e - extension
  • -x - exec command
  • -d --max-depth
  • -E, --exclude pattern, e.g., --exclude node_modules