Skip navigation.
Home

Reply to comment

Find 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

Note that:

 $ 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

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.

Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h1>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
9 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.