How to use Find

The find command is a wonderfully useful tool that allows you to find files or folders by searching for them in a number of ways.

Find searches through one or more directory trees of a filesystem, locating files based on some user-specified criteria. Further, find allows the user to specify an action to be taken on each matched file. Thus, it is an extremely powerful program for applying actions to many files.

The find command will traverse the filesytem from the location that you run it.  This can be used to your advantage as instead of having to parse through the entire directory structure, find can be used on a small targeted area.

Basic Syntax

find <absolute path> <search variables>

While it is possible to use find using relative paths, it is never a good practice to do so.  Using absolute paths will help prevent you from making harmful changes by insuring that you are making to the files and folders you specifically want too.

One of the most common syntax used in conjunction with find is using a flag with a numerical value.  The syntax for these numerical values follows this format:

  • +n : greater than n
  • -n : less than n
  • n : exactly n

Some Examples of Find in Use

Find all files larger than 2GB (useful for finding pesky logs that prohibit apache from starting)

find /usr/local/apache -type f -size +2048000

Find all files owned by the user ”fred” in /home

find /home -user fred

Find  all files with 777 permissions in /home

find /home -perm 777

Find only directories with 777 perms

find /home/*/public_html/ -type d -perm 777

Find only directories with 777 perms and change to 755 perms

find public_html/ -type d -perm 777 -exec chmod 755 ‘{}’ \;

Find data owned by user nobody and change ownership to user

find . -user nobody -exec chown usernamebob: ‘{}’ \;

Find where that pesky config file may be located with a wildcard, when you don’t know the exact name

find public_html/ -type f -name “*conf*”

Search files for a specific string of text

find public_html/ -type f -print0 |xargs -0 grep -si “searchabletext”

Use ‘or’ and wildcard to find specific files, and list them, ie. ls any files ending in .php or .html

find . \( -name ‘*php’ -or -name ‘*html’ \) -type f -ls

Find php files modified within last 60 days:

find /home/*/public_html -name ‘*.php’ -type f -mtime -60

Find file modified between 2 and 29 minutes ago. (Did someone change a file within the past half-hour)

find public_html/ -type f -mmin +1 -mmin -30

Find hidden files or directories and how much space they are consuming. Looking for anything 1M and over

find /home/user/mail/ -maxdepth 1 -type d -name “.*” |xargs du -shc |grep M |less

===

Liquid Web’s Heroic Support is always available to assist customers with this or any other issue. If you need our assistance please contact us:
Toll Free 1.800.580.4985
International 517.322.0434
support@liquidweb.com
https://manage.liquidweb.com/

Be Sociable, Share!
Here's $75, Launch a New VPS Today. Find out why 30,000 customers have chosen our Best-in-Class Performance & 24x7 Heroic Support.