Home > Linux/Unix > Find command in Linux with examples

Find command in Linux with examples

March 21, 2011

10 usage of find command in UNIX
find is very versatile command in UNIX and I used it a lot in my day to day work. I believe having knowledge of find command in UNIX and understanding of its different usage will increase your productivity a lot in UNIX. If your works involve lots of searching stuff or if you are a java or C++ programmer and your code resides in UNIX, find can greatly help you to look for any word inside your source file in the absence of an IDE, find is the alternative way of searching things in UNIX. grep is another command which provides similar functionality like find but in my opinion find is much more powerful than grep in unix.

This article is in continuation of my earlier article Top 10 basic networking Commands in Unix and Top 10 most useful CVS command in Unix .

Here I am listing down some of the way I use find command regularly, I hope this would help some one who is new in UNIX and find command or any developer who has started working on UNIX environment. this list is by no means complete and just some of my favorites , if you have something to share please share via commenting.

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

This is my favorite 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 command 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.

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

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

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”.

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 subdirectory, 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 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

Hope you find this useful , please share how you are using find commands and we can benefit from each others experience and work more efficiently in UNIX.

Categories: Linux/Unix
%d bloggers like this: