How to Use Find Command to Find Files

find is a linux command for recursively filtering objects in the file system based on simple conditions. It's pretty easy to use this command to search files and directories.

Find files by name or extensions

If you want to find all .html files on /var/www/ directory, just type the following command into the computer

find /var/www/ -name "*.html"

If you want to delete all the files founded by the previous command, please add -delete option to the end of the previous command. Please carefully use this option when you are certain that the results only match the files that you wish to delete.

find /var/www/ -name "*.html" -delete

Find files by modification time

If you want to find files by modification time, just add -mtime option. Here is the command for you to find all the .php files in /var/www/ directory modified in 3 day.

find /var/www -name "*.php" -mtime 3

Find files based on content

If you need to search files based on the content of the file, use a tool like grep. Here is an example

find . -type f -exec grep "example" '{}' \; -print

This command searches every object in current directory hierarchy that is a file (-type f) and then runs the command grep "example" for every file that satisfies the conditions. The files that match are printed on the screen (-print). The curly braces ({}) are a placeholder for the find match results. The {} are enclosed in single quotes (') to avoid handing grep a malformed file name. The -exec command is terminated with a semicolon (;), which should be escaped (\;) to avoid interpretation by the shell.

Find and process files

The -exec option runs commands against every object that matches the find expression. Here is an example.

find . -name "rc.conf" -exec chmod o+r '{}' \;

This command searches every object in the current hierarchy (.) for files named rc.conf and runs the chmod o+r command to modify file permissions of the find results.

Comments

Popular posts from this blog

How to Enable HSTS in Webmin

ONE-CLICK L2TP/IPSec VPN Installer

Alphabet Sold Google Domains to Squarespace