Quelques commandes find

1) Running the last executed find command in Unix:
This will repeat the last find command executed. It saves lot of time if you re searching for something and you need to execute same command again and again. In fact "!" can be used with any command to invoke previous run of that command.

2) Finding files which has been modified less than one day in Unix:
find . -mtime -1

While looking out some issue just to check which files have been modified recently which could be likely cause of  issue, believe me it helps a lot and many a times gives you enough hint of any problem due to intended or unintended file change.

3) List all the files and directories in the box which holds the 777 permission in Unix?
find . -perm 777 –print

I use this find command example to find out all the executable files , you can also modify it to find all the read only files or files having write permission etc by changing permissions e.g. to find all read only files in current directory : find . –perm 555
Here "." or period denotes current directory. You can replace it with any directory you want. Its also benefit to know about file permissions in Unix 

4) How to do case insensitive search using find command in Unix? Use option “-i" with name, by default find searches are case sensitive.
find . –iname "error" –print 

find command is also a favorite topic during Unix interview and interview often asked questions on find command during interview.

UNIX find command and xargs

Now we will see some UNIX find command example combined with xargs command, xargs can be used to do whatever witch each file found by find command for example we can delete that file, list content of that file or can apply any comment on that file.

5) How to delete temporary files using find command in Unix?
find . -name "*.tmp" -print | xargs rm –f

Use of xargs along with find gives you immense power to do whatever you want with each search result. See another example below , also its worth considering use of -print0 to avoid problems with white space in the path when piping to xargs (use with the xargs -0 option) as suggested by Ebon Elaza.

6) How to find all text file which contains word Exception using find command in Unix ?
find . –name "*.txt" –print | xargs grep “Exception” 

find . –name "*.java" –print | xargs grep “MemoryCache”, this will search all java files starting from current directory for word "MemoryCache". we can also leave -print option in all cases because its default for UNIX find command as pointed out by Ben in comments. 

7) Finding files only in current directory not searching on sub directories:
While using find command I realized that some time I only need to find files and directories that are new , only in the current directory so I modified the find command as follows.

find . -maxdepth 1 -type f -newer first_file

Another way of doing it is below:

find . -type f -cmin 15 -prune

Means type file, last modified 15 minutes ago, only look at the current directory. (No sub-directories)

8) Find all files in current directory and sub-directory, greater than some size using find command in Unix:
find . -size +1000c -exec ls -l {} \; 

Always use a c after the number, and specify the size in bytes, otherwise you will get confuse because find -size list files based on size of disk block. to find files using a range of file sizes, a minus or plus sign can be specified before the number. The minus sign means "less than," and the plus sign means "greater than." Suppose if you want to find all the files within a range you can use find command as below

find . -size +10000c -size -50000c -print

This find command example lists all files that are greater than 10,000 bytes, but less than 50,000 bytes: 

9) Find files which are some days old and greater than some size in Unix. Very common scenario where you want to delete some large old files to free some space in your machine. You can use combination of "-mtime" and "-size" to achieve this.

find . -mtime +10 -size +50000c -exec ls -l {} \; 

This command will find which are more than 10 days old and size greater than 50K.

10) You can use "awk" in combination of find to print a formatted output e.g. next command will find all of the symbolic links in your home directory, and print the files your symbolic links points to:

find . -type l -print | xargs ls -ld | awk '{print $10}'

"." says starts from current directory and include all sub directory
"-type l" says list all links