Find Tips & Tricks

Tips & Tricks

  • Common usage:

    $ find . -type d -exec chmod 755 {} \;
    $ find . -type f -exec chmod 644 {} \;
  • Using Regular Expressions (regex): If the -name option cannot satisfy your need, remember to use the -regex option, which offers more powerful parttern matching:

    $ find . -regex pattern -print
  • prune option tricks:

    $ find . -path '*/.zhigang' -prune -o -type f

    is equivalent to:

    $ find . \( -path '*/.zhigang' -prune -o -type f \) -print'

    ie., -print is added at the outer level; but -o binds lower than -a, thus:

    $ find . -path '*/.zhigang' -prune -o -type f -print

    is equivalent to:

    $ find . -path '*/.zhigang' -prune -o \( -type f -print \)
  • Exclude some directories when finding files:

    $ find / \( -path '/usr' -o -path '/proc' \) -prune -o -name 'httpd.conf' -print
    $ find . -maxdepth 1 -type f
  • Use find to tar part of a tree only: if you are not worried about the tree structure including empty directories:

    $ cd A; tar cvf - `find . -type f -print | grep -v  B` | gzip > x.tar.Z

    If you do want the full structure apart from B:

    $ tar cvf - `find A/* -type d -print | grep -v B` | gzip > x.tar.Z

    Or use tar only:

    $ tar cvf - --exclude   | gzip -f - > tree.tar.gz
  • Find SUID/SGID commands:

    # find / \( -perm -004000 -o -perm -002000 \) -type f -print
    # find / \( -path '/nfs' -prune \) \( -perm -004000 -o -perm -002000 \) -type f -print
    # find / -xdev \( -perm -004000 -o -perm -002000 \) -type f -print

    Be sure that you are the superuser when you run find, or you may miss SUID files hidden in protected directories.

  • Find keyword in Kconfigs in Linux kernel source tree:

    $ find . -name "Kconfig" -exec grep -H "dmesg" {} \;

None: Find (last edited 2009-07-23 09:41:00 by ZhigangWang)