find

Howto: Find can make any thing easily to make and modify.

Posted on Updated on

Find command examples

first Linux find searches through the root filesystem file named “text1” if it finds the file, it prints the output to the screen

find / -name text1 -type f -print

You can alo choosh multiple source locations to search the file /home /etc ie: search on specified folders only

find /home /etc -type f -name text1
find /home /etc -name "text*" -type f

– it will locate the file start with the name text

you may user different key word like listed below
-name “text.*” — it will locate the file start with the name text
-name “*.txt” — it will locate the fileĀ  with the extension .txt

Types

-type f it will follow the file
-type d it will follow the directory

find /home -type d -name dir1 find . -type f -name "*.php" -exec grep -il string {} \;

Finding files that contain text

You can combine the find and grep commands to powerfully search for text strings in many files.

This next command shows how to find all files beneath the current directory that end with the extension .php, and contain the characters StringBuffer. The -l argument to the grep command tells it to just print the name of the file where a match is found, instead of printing all the matches themselves:

find . -type f -name "*.php" -exec grep -l StringBuffer {} \;

(Those last few characters are required any time you want to exec a command on the files that are found. I find it helpful to think of them as a placeholder for each file that is found.)

This next example is similar, but here I use the -i argument to the grep command, telling it to ignore the case of the characters string, so it will find files that contain string, String, STRING, etc.:

find . -type f -name "*.php" -exec grep -il string {} \;

Acting on files that are found (find command and exec)

This command searches through the /usr/local directory for files that end with the extension .html. When these files are found, their permission is changed to mode 644 (rw-r–r–).

find /usr/local -name "*.html" -type f -exec chmod 644 {} \;

This command searches through the htdocs and cgi-bin directories for files that end with the extension .cgi. When these files are found, their permission is changed to mode 755 (rwxr-xr-x). This example shows that the find command can easily search through multiple sub-directories (htdocs, cgi-bin) at one time.

find htdocs cgi-bin -name "*.cgi" -type f -exec chmod 755 {} \;