<img src=”http://images.del.icio.us/static/img/delicious.42px.gif” alt=Delicious it></img> </img>

These are 10 useful uses of the command find in Linux, they are not the most useful, just some useful for me, I will use $HOME as the path for every example but you may use any other.

  1. Find empty directories

  2. find $HOME -depth -type d -empty

    This will find empty directories in your home directory.
  3. Find empty files

  4. find $HOME -depth -type f -empty

    This will finde empty common files in your home directory.
  5. Find a file with a specific name

  6. find $HOME -name [name_of_file]

    This will find files with a given name in any child directory of your home.
  7. Find a file with a specific extension

  8. find $HOME -name "*.[given_extension]"

    This find files wit the given extension all along your home, an example to find jpg files is:

    find $HOME -name '*.jpg'

  9. Find files with specific permissions

  10. find $HOME -perm [permission bits]

    This will find files with the given permission bits in your home, as an example we can look for .txt files that can have 644 bits on.

    find $HOME -name '*.txt' -perm 644

  11. Find files with some given permissions and no matter the rest

  12. find $HOME -perm -[permision_bits]

    This will find files in your home that have match with the given permissions but that can also have some others, as an example:

    find $HOME -name '*.txt' -perm -644

    This will find the files with 644 but also some with 664 or 777 or anything "greater" than 644. Output comparison Let's see some output comparison for this to be better understood.
    find $HOME -name '*.txt' -perm 644 -exec ls -l {} \;
    -rw-r--r-- 1 root root 181 2007-10-09 23:27 /home/ggarron/lmsensors.txt
    -rw-r--r-- 1 ggarron ggarron 2757 2007-08-29 23:52 /home/ggarron/mv.txt
    -rw-r--r-- 1 ggarron ggarron 77431 2007-09-05 23:11 /home/ggarron/curl.txt
    
    find $HOME -name '*.txt' -perm -644 -exec ls -l {} \;
    -rw-r--r-- 1 root root 181 2007-10-09 23:27 /home/ggarron/lmsensors.txt
    -rw-r--r-- 1 ggarron ggarron 2757 2007-08-29 23:52 /home/ggarron/mv.txt
    -rw-r--r-- 1 ggarron ggarron 77431 2007-09-05 23:11 /home/ggarron/curl.txt
    -rw-rw-r-- 1 ggarron ggarron 464 2007-09-06 01:23 /home/ggarron/post.txt
    
    As you may see in the first example we do not have the file post as it has 664 permissions, so do not exactly match 644, but on the second output, it is listed as it has "greater" permissions than 644.
  13. Find files of given sizes

  14. find -size n[cwbkMG]

    This will output the files of a given block size, as an example we can see:

    find $HOME -name '*.txt' -size 4k -exec ls -l {} \;

    And the output is:
    -rw-r--r-- 1 ggarron ggarron 3707 2007-07-25 15:48 /home/ggarron/drupal/public_html/sites/all/modules/akismet/README.txt
    -rw-r--r-- 1 ggarron ggarron 4043 2007-01-22 15:44 /home/ggarron/Desktop/front/README.txt
    -rw-r--r-- 1 ggarron ggarron 3112 2007-09-23 15:39 /home/ggarron/Desktop/borrar/upgrading-drupal/public_html/sites/all/modules/adsense/README.txt
    -rw-r--r-- 1 ggarron ggarron 3707 2007-09-23 15:39 /home/ggarron/Desktop/borrar/upgrading-drupal/public_html/sites/all/modules/akismet/README.txt
    -rw-r--r-- 1 ggarron ggarron 3616 2007-09-23 15:39 /home/ggarron/Desktop/borrar/centos-asterisk/how-to-install.txt
    
    Now let's see this other:

    find $HOME -name '*.txt' -size 5k -exec ls -l {} \;

    And the output is:
    -rw-r--r-- 1 ggarron ggarron 4496 2007-09-23 15:39 /home/ggarron/Desktop/borrar/upgrading-drupal/public_html/sites/all/modules/captcha/captcha_api.txt
    
    Now if you divide each file size by 1024 (1k) you will see that the first output is always lower than 4096 (4k) and upper 3072 (3k), on the second output you have it between 4096 (4k) and 5120 (5k).
  15. Find files with a give name and any extension

  16. find -name '[given_name].*'

    This will output the files of any given name but with any extension
  17. Find files modified in the latest blocks of 24 hours

  18. find -mtime n

    Where n is: 0 for the last 24 hours 1 for the last 48 hours 2 for the last 72 hours and so on.
  19. Find files that was accessed in the latests blocks of 24 hours

  20. find -atime n

    Where n is: 0 for the last 24 hours 1 for the last 48 hours 2 for the last 72 hours and so on.

I have been using in the examples the -exec parameter, this is used to execute any action over the find files, I was listing them but you may delete them or move them or copy them anything you want.

You may also would like to read: http://www.go2linux.org/find-Linux-command-to-find-files-on-your-disk

Update: April 10 - 2009 I have written an script that merge the use of scp and find, find it here