How to Use the Find Command in Linux
The find command is a wonderfully useful tool that allows you to find files or folders by searching for them in multiple ways.
Find searches through one or more directory trees of a file system, 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 powerful program for applying actions to many files.
The find command will traverse the file system 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 2 GB (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, i.e. 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://my.liquidweb.com/
Related Articles:
- What is CentOS? Everything You Need to Know
- Check Apache Status with systemctl status and apachectl status Commands
- How to Find the Server Name Indication (SNI) Supporting Details
- How to Install Pip on CentOS 7
- Get Kernel Version for Linux: A Guide
- What Is a Time Series Database? How It Works & Use Cases
About the Author: J. Mays
As a previous contributor, JMays shares his insight with our Knowledge Base center. In our Knowledge Base, you'll be able to find how-to articles on Ubuntu, CentOS, Fedora and much more!
Our Sales and Support teams are available 24 hours by phone or e-mail to assist.
Latest Articles
How to Install WordPress on Linux (AlmaLinux)
Read ArticleWhat is CentOS? Everything You Need to Know
Read ArticleWhat is CentOS? Everything You Need to Know
Read ArticleRedis as Cache: How It Works and Why You Should Use It
Read ArticleRefer-a-Friend Program for Website Hosting: Get $100 for Each Friend!
Read Article