GNU Find
The GNU find command is a powerful tool for locating files and directories within your server’s file system. If you’re managing a website, knowing how to use find can be incredibly helpful for various tasks, from locating specific configuration files to identifying recently modified content. This guide will walk you through the basic syntax and some common use cases to get you started.
Basic syntax
The basic structure of the find command is as follows:
find [path] [expression]- [path]: This specifies the directory where
findwill start its search. For example,.represents the current directory,/home/your_cpanel_user/public_html/would be your website’s document root. - [expression]: This is where you define the criteria for your search, such as the name of the file, its type, or its permissions.
You’ll typically run these commands via SSH (Secure Shell) access.
The output of the command will be a list of files that match the requested expression(s).
Common use cases and options
Let’s explore some of the most frequently used options with the find command.
Finding files by name (-name and -iname)
One of the most common tasks is searching for a file by its name. The -name option performs a case-sensitive search, while -iname performs a case-insensitive search.
Example (case-sensitive): Find the main WordPress configuration file.
find /home/your_cpanel_user/public_html/ -name "wp-config.php"Example (case-insensitive): Find any file named “readme” regardless of capitalization within your plugins directory.
find /home/your_cpanel_user/public_html/wp-content/plugins/ -iname "readme.txt"You can also use wildcards. For instance, to find all PHP files:
find /home/your_cpanel_user/public_html/ -name "*.php"Finding files by type (-type)
The -type option allows you to specify the kind of file you’re looking for.
Finding regular files (-type f)
This option searches for regular files (not directories or links).
Example: Find all regular files named .htaccess within the document root.
find /home/your_cpanel_user/public_html/ -type f -name ".htaccess"Finding directories (-type d)
This option searches for directories.
Example: Find all directories named “uploads” within your WordPress content directory.
find /home/your_cpanel_user/public_html/wp-content/ -type d -name "uploads"Finding symbolic links (-type l)
Symbolic links (symlinks) are like shortcuts to other files or directories. You can find them using -type l.
Example: Find all symbolic links within the current directory.
find . -type lFinding files by owner (-user)
The -user option helps you locate files owned by a specific user. On a cPanel server, your files are typically owned by your cPanel username.
Example: Find all files in /tmp owned by the cPanel user “myuser”.
find /tmp -user myuserFinding files by permissions (-perm)
You can search for files based on their permission settings using the -perm option. Permissions are often represented in octal (e.g., 644, 755) or symbolic notation.
Example: Find all files in your WordPress directory that have permissions set to 777 (which is generally insecure and should be investigated).
find /home/your_cpanel_user/public_html/ -perm 777Example: Find all directories with permissions of 755.
find /home/your_cpanel_user/public_html/ -type d -perm 755Finding files by modification time (-mtime)
The -mtime option allows you to find files based on when their content was last modified. The time is measured in 24-hour periods (days).
-mtime n: Files modified exactlyndays ago.-mtime +n: Files modified more thanndays ago.-mtime -n: Files modified less thanndays ago (i.e., within the lastndays).
Example: Find all PHP files in your WordPress themes directory modified in the last 7 days.
find /home/your_cpanel_user/public_html/wp-content/themes/ -name "*.php" -mtime -7This is very useful for identifying recently changed files, especially if you are looking for recent updates or troubleshooting an issue that started recently.
Combining options
The true power of find comes from combining these options to create highly specific searches. You can chain multiple expressions together, and find will locate items that match all criteria.
Example: Find all regular files (-type f) within the wp-content directory, owned by “your_cpanel_user” (-user your_cpanel_user), that end with .js (-name "*.js") and were modified in the last 3 days (-mtime -3).
find /home/your_cpanel_user/public_html/wp-content/ -type f -user your_cpanel_user -name "*.js" -mtime -3By mastering these basic find command options, you’ll be better equipped to manage your WordPress installation, troubleshoot issues, and understand the layout of your files on your cPanel server.