find FAQ

Creation: 26 septembre 2005
Mise à jour: 10th of january 2011
Version: 1.4
Author: Jean-Louis Bicquelet-Salaün
Location: http://jlbicquelet.free.fr
Copyright: (c) 2004-2011 Jean-Louis BICQUELET-SALAÜN

find Frequently-Asked Questions

Beware, find used in this faq is the AIX 5.3 version. For more information, please contact Jean-Louis BICQUELET


FAQ Revised: Monday 10 January 2011 15:56:09


Table of Contents

1. limits
2. commands
3. search
4. general

1. limits

1.1. How can I copy files when there are too many files ?
find . * -exec cp {} new_dir \;


1.2. How can I delete files when you have too many files ?
ls doesn't work because there are too many files, use find:
    find . -name 'cftsu*' -exec rm {} \;



2. commands

2.1. How can I apply a command to a group of files ?
    find . -name "*.txt" -exec chmod o+r '{}' \;


2.2. How can I suppress files older than n days ?
    #find /var/spool/mqueue -mtime +2 -exec rm {} \;


2.3. How can I compress/uncompress files in a subtree directory ?
rep is the directory to compress:
find rep \( -name '*' \)  -exec gzip {} \;

To compress files older than 300 days use:

find $1 \( -name '*' \) -atime +300 -exec gzip {} \;

To uncompress :

find . -name '*.gz' -exec /usr/local/bin/gunzip  {} \;


2.4. How can I suppress files from a subtree ?
The first syntax is:
find rep \( -name "*.dat"  \) -type f -print | xargs -e rm -f
find rep \( -name "*.bak" -o -name "*.BAK"  -o -name core -o -name a.out -o -name "*.o" \) -type f -print | xargs -e rm -f

You also can use :

find . -name  "*.dat" -exec rm {} \;
c>commands

2.5. How can I change files that belongs to a user to an other user.
You have to search for the user and user chown like this:
find /cft -user cft -exec chown cftssl {} \;



3. search

3.1. How can I apply a command to a group of file ?
find . -not \( -name "*,v" -o -name ".*,v" \) '{}' \; -print

Options possibles:

  1. -not signifie la néation de l'expression qui suit
  2. * \( pour le début d'une expression complexe
  3. * \) pour la fin d'une expression complexe
  4. * -o signifie un ou logique
  5. * -a signifie un et logique


3.2. How can I find a string in a subtree ?
        find . -exec grep "www." '{}' \; -print
        find . -exec grep -q "www." '{}' \; -print


3.3. How can I search a file with jokers ?
    find . -name "*.pdf" -print
    find . -name \*.pdf -print


3.4. How can I find files that don't belong to someone ?
    find . -nouser -print
    find . -nogroup -print


3.5. How can I find files that have been modified for 2 days ?
    find . -mtime -2 -print


3.6. How can I find files that have been modified for 10 minutes ?
We don't think it can exists. It is a bad known option, but you can specify delay in minutes with -mmin.
find . -mmin -10 -ls
    5  200 -rw-rw----  1 root      system      204800 Jan 10 14:25 ./lvmt.log


3.7. How can I count files that have been modified for 2 days ?
find /var/spool/mqueue -mtime +2 | wc -l
   22342


3.8. How can I find files that have a specific right access ?
   find . -perm 0600 -print


3.9. How can I display files in long format ?
find . -ls -long
..
1376    2 -r--r-----  1 root     system       1369 Jun 12 17:01 /etc/sudoers
1435    2 -r--r-----  1 root     system       1161 Mar 25 13:53 /etc/sudoers.rpmsave



4. general

4.1. What are find options ?
    -name	must be quoted when using wildcards
    -type	e.g. f=file d=directory l=link
    -user	name or UID
    -group	name or GID
    -nogroup    group doesn' xist in the group database
    -perm	specify permissions
    -size	rounded up to next 512 byte block or use c to specify bytes
    -atime	last time file was read
    -ctime	last time file's owner or permissions were changed
    -mtime	last time file was modified
    -newer	find files newer than given file
    -delete	remove files found
    -ls	gives same output as ls -dgils
    -print	displays results of find command
    -exec command {} \;	to execute a command; note the required syntax
    -ok	use instead of exec to be prompted before command is executed
    -depth	starts at lowest level in directory tree rather than root of given directory
    -prune	used when you want to exclude certain subdirectories
    -cpio Device  Writes the current file to the specified device in the cpio
                command format.
    -ea         Evaluates to the value True if file has either access control
                information (ACL) or Extended attributes (EA) set.
    -follow     Causes symbolic and hard links to be followed.
    -fstype Type  Evaluates to the value True if the file system to which the file
                belongs is of the specified type, where the Type variable has a
                value of jfs (journaled file system) or nfs (network file system).
     -inum n    Evaluates to the value True if file has an i-node matching the
                value of the n variable.

You also have options that behave differently with links:

       -H
            Cause the file information and file type evaluated for each
            symbolic link encountered on the command line to be those of the
            file referenced by the link, and not the link itself. If the
            referenced file does not exist, the file information and type
            shall be for the link itself. File information for all symbolic
            links not on the command line shall be that of the link itself.
       -L
            Cause the file information and file type evaluated for each
            symbolic link to be those of the file referenced by the link, and
            not the link itself.


4.2. How can I use find on several directory ?
You just have to write the list of the directories.
find ddl emc ssl -print




Copyright (c) 2006-2011 Jean-Louis BICQUELET

This list of questions and answers was generated by makefaq.