Skip to content

Latest commit

 

History

History
80 lines (69 loc) · 3.34 KB

find.md

File metadata and controls

80 lines (69 loc) · 3.34 KB

Examples

Print all files with non-ASCII characters in their name:

find ./ -print0 | \
perl -n0e 'chomp; print $_, "\n" if /[[:^ascii:][:cntrl:]]/'

Remove all files with non-ASCII characters in their name:

find ./ -print0 | \
perl -MFile::Path=remove_tree -n0e \
'chomp; remove_tree($_, {verbose=>1}) if /[[:^ascii:][:cntrl:]]/'.

Perform ls -l on found files:

find ./ -ls               # Best method.
find ./ -exec ls -l {} \; # Also fine.
find ./ | xargs ls -l     # Not recommended.
  • find ./ -exec <COMMAND> {} \; = Run on every returned result.
    • {} = A "variable" that acts as a placeholder for the current result.
    • \; = Required terminator for exec commands.

  • find ./ -type f | wc -l = Print number of files beneath current path.
    • -type f = Search for files only, not directories.
    • wc -l = Count the number of lines in the ouput.

  • find -03 -L . -type f -name "*.jpg" =
    • -03 = Optimize file search order based on likelihood of finding a match.
    • -L = Follow symbolic links.
    • . = Search in or below the current directory.
    • -type f = Look for regular files only.
    • -name = Search based on file name.
    • "*.jpg" = Use wildcard to search for all files with .jpg extension.

  • find ~ -user alice -mtime 7 -iname ".log" -delete = Delete log files owned by alice within a certain date range.
    • -user alice = Files owned by user alice.
    • ~ = Search in or beneath the current user's home directory.
    • -mtime 7 = Look for files modified 7 days ago.
    • -iname = Match by file name (case insensitive).
    • -delete = Delete matched files.

Options

  • -type f = Match is of type f (f=file, d=dir, l=link, p=pipe, b=block, c=character).
  • -iname = Match by file name, ignoring case.
  • -regex = Match by file name using regex.

  • -uid 1000 = Match is owned by UID 1000 (-gid N for file's group's GID).
  • -user alice = Match is owned by user alice.
  • -group wheel = Match is owned by group wheel.

  • -mmin -19 = Match was last modified less than 19 minutes ago.
  • -mmin +5 = Match was last modified more than 5 minutes ago.
  • -mtime 3 = Same as 'mmin' but in days, not minutes (match modified 3 days ago).
  • -newer file.txt = Match is newer than file.txt.

  • -size +5G = Match is more than 5 gigabytes (c=bytes, w=two-byte words, k=kilobytes), (-=less than, +=more than).
  • -perm 755 = Match has octal permissions 755.

  • -delete = Delete matches.
  • -L = Follow symbolic links.

Locate

Examples

  • locate -ice *.txt = Search for all .txt files on the system.
    • -i (ignore) = Ignore case of match.
    • -c (count) = List number of matches.
    • -e (exist) = Verify file's existence before producing result since database may be old.

NOTE: locate is much faster than find, but locate searches a tabulated database instead of actively scrubbing your disk for a match. This means the data locate uses may be a few hours old