The Unix find command - searching the hierarchical filesystem tree structure

Contents

Overview

find' is one of the '''on-board power tools''' that you should know well. It it '''the one' tools that operates on hierarchical filesystem tree structure'''.
Only in some modern Unix systems there are other tools that have a recursive Switch (z.B. grep -R) who can directly operate on filesystem hierarchies. However find has many tricks in its sleeve to that will allow you to to things that other commands cannot.

find Options

 -ls  
    long listing 
 -print
 -print0  
    everything is taken literally so files can contain quotes and spaces. Strings have aree \0 terminated 
 -exec   
    ask before exec cmd 
 -ok   
    ask before exec cmd 
 -perm 75  
    exactly rwxr-xr-x 
 -perm -002  
    general readable files (all bits set) 
 -perm +1000 
    any of the bits set 
 - !  
    not 
 -o  
    or 
 -a  
    and 
 -user  
 -type   
    out of f, d 
 -size +100  
    size greater 100k 
 -atime  
    access for n days 
 -mtime  
    modify for n days 
 -group
 -nouser
 -nogroup  
    group that is not mentioned in group file 
 -inum number TRUE if file has inode number. 


Examples - Tips and Tricks


Protect Web-files

Protect Web-files and make them global readable so even the owner can not add them. 444 is a octal numerical representation of the file permissions (-r--r--r--).
 find . -type f | xargs chmod 444

Delete a file wit special characters in the filename



 6423022 -rw-r--r--   1 ahaack  staff     0 11 Nov 10:08 ??

6423022 is the inode number of the strange file shown as "??"

 find . -inum 6423022 -ok rm "{}" \;
 "rm ./??"? y

Delete all .cache files in the html director tree


 find . -name ".cache" | xargs rm


Create an RCS directory in every sub directory

Create an RCS directory in every sub directory , except where one already exists The trick here is to prevent create a subdirectory where a RCS directory already exist (e.g. RCS/RCS/RCS ).
find . -type d -a ! -name RCS -exec mkdir {}/RCS \;

Search for "localhost" in all php files

On Linux the command can handle files that contain spaces.
 find . -name "*.php" -print0 | xargs -0 grep "localhost"

Status: Published Date: 2017/06/05 14:14:14 Revision: 1.1

Copyright bei Andreas Haack (C) 2014. Diese Seite wird so wie sie ist zur Verfuegung gestellt, ohne irgenweche Garantien der Verwendbarkeit fuer bestimte Zwecke. Die auf dieser Seiten angebrachten Links liegen ausserhalb der redaktionellen Verantwortung von Andreas Haack und es wird keine Haftung oder Garantie uebernommen. Die Seiten sind Copyright (c) 2014 von Andreas Haack. Kein Teil darf ohne die schriftliche Einverstaendnis von Andreas Haack veroeffentlicht werden.
The page is provided 'as is' , without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fit- ness for a particular purpose and non-infringement. In no event shall Andreas Haack be liable for any claim, damages or other liability. This page is copyrighted property of Andreas Haack. Copyright by Andreas Haack (c) 2014 . No part of this page may be published without written permission for Andreas Haack. A hyper-link may created to this page but NOT to the embedded elements of this page. It may be freely downloaded for private purpose only as long as it is unaltered.