Friday, March 30, 2012

The Find command cheat sheet


The Find Command Cheat Sheet


1. Basic find command
# find -name "TestFile"

2. Find Files Using Name and Ignoring Case
# find -iname "TestFile"

3. Limit Search To Specific Directory Level Using mindepth and maxdepth
# find / -maxdepth 3 -name passwd
-maxdepth --> will go 3 directories below -- / 1st; /etc 2nd; /usr/bin 3rd

# find / -mindepth 3 -maxdepth 5 -name passwd
will go 3 depths first and upto 5 -- so will not disply under /; /usr; /usr/bin

4. Executing Commands on the Files Found by the Find Command.
user -exec {} /;
# find -iname "TestFile" -exec md5sum {} \;

5. Inverting the match.
To inver the match use the "-not" switch
# find / -not -iname "TestFile"

6. List inodes of the files
# ls -i1 test*
16187429 test-file-name
16187430 test-file-name

# find -inum 16187430 -exec mv {} new-test-file-name \;
# ls -i1 *test*
16187430 new-test-file-name
16187429 test-file-name

7. Find file based on the File-Permissions
You can :
    * Find files that match exact permission
    * Check whether the given permission matches, irrespective of other permission bits
    * Search by giving octal / symbolic representation

# find . -perm -g=r -type f -exec ls -l {} \;
Will display all files with group permission read. Not files with readonly group permission

# find . -perm g=r -type f -exec ls -l {} \;
Will dispay files with 040 permission. i.e files with group read only permisison

# find . -perm 040 -type f -exec ls -l {} \;
Will dispay files with 040 permission. i.e files with group read only permisison