Friday, March 27, 2015

find command in UNIX/LINUX.

Here, are some find Command examples in Unix/Linux.

To run  last executed find command :
!find will repeat the last find command executed by user..

root@testenv ~/java : !find
./emp.java
./dept.java

"." says starts finding from current directory and include all sub directory and
"/" says starts finding from root directory 

To find files names as test.java:
find -name "test.java"

To find files names other then test.java file in test directory:
find /test/ –not -name "test.java"

To find files which has been modified/accessed/created less than a day, minute or hour :

-mtime is used to search files based on last modification time in days.
-atime is used to search files based on last accessed time in days.
-ctime is used to search files based on last changed time in days.

+ sign is used to search for greater than.
- sign is used to search for less than.
without sign is used for exact.

(all the files modified exact 1 day)
find . -mtime 1 

(find all the files modified less than 1 day)
find . -mtime -1

(find all the files modified more than 1 day)
find . -mtime +1

We can use same for –atime and –ctime for last accessed and last changed files.

-mmin is used to search files based on last modification time in minutes.
-amin is used to search files based on last accessed time in minutes.
-cmin is used to search files based on last changed time in minutes.

(find all the files created exact 15 min)
find . -cmin 15

(find all the files created less than 15 min)
find . -cmin -15

(find all the files created more than 15 min – means all other files)
find . -cmin +15

To find all the files and directories which holds the 777 permission:
–perm option is used to find files based upon permissions.

(find all the files with file permission as 777 – rwx on owner, group and others)
find –perm 777

(find all the files with file permission as 750 – rwx on owner, r-x on group and no permission to others)
find –perm  750

(find all the files with file permission as 444 – rrr on owner, group and others)
find –perm 444

(find all the files with file permission as 644 – rw- on owner, r—on group and others)
find –perm 644

To find case insensitive search.
-iname,  by default find searches are case sensitive. It is used for errors and exceptions in log files.
find . –iname "error" –print

To delete temporary files.
–delete option to do a remove specific files from the search criteria.
find . –delete –name "error" –print

use -print0 to avoid problems with white space in the path.
find . -name "*.tmp" –print0 | xargs rm –f
find . -name "*.tmp" -print | xargs -0 rm –f

To find all java file which contains word “Error” with sorted o/p or without sorted o/p.
find . –name "*.java" –print | xargs grep “Error”,
find . –name "*.java" –print | xargs grep “Error” | sort

To find files only in current directory not searching on sub directories:

While using find command I required only files, directories and links that are new, only in the current directory.
find . -type f -newer –name "*.java"
find . -type d –newer –name "*.java"
find . -type l –newer –name "*.java"

While using find command I required only files, directories and links that are new, only in the current directory with searching for two sub directories – specifying the depth of find.

-mindepth  for to find files upto minimum depth levels in directory.
-maxdepth  for to find files upto maximum depth levels in directory.

find . –maxdepth 2 -type f -newer –name "*.java"
find . –maxdepth 2 -type d –newer –name "*.java"
find . –maxdepth 2 -type 1 –newer –name "*.java"

find . –mindepth 2 –maxdepth 10 -type f –name "*.java"

To find files based on size.
–size option to find files based upon certain size. It finds all files in current directory and sub-directory, greater than some size, here size is 1MB = 1048576 bytes:

find . -size +1048576c -exec ls -l {} \;
find . -size +10M -exec ls -l {} \;

-c after the number means specify the size in bytes.
To find files using a range of file sizes, a minus or plus sign can be used before the number.
- minus sign means less than
+ plus sign means "greater than.

If I want to find all the files within a range of 5 MB to 10 MB:
find . -size +5242880c -size -10485760c -print
find . -size +5M -size -10M -print

To find files 30 days older and above 10 MB size.
You can use –mtime and –size option to find files which are 30 days old and greater than 10MB in size
find . -mtime +30 -size +10485760c -exec ls -l {} \;
find . -mtime +30 -size +10M -exec ls -l {} \;


To 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}'

To find all empty files from the server.
find . -empty

To find largest and smallest file in the current directory:
find . -type f -exec ls -s {} \; | sort -n -r | head -1

find . -type f -exec ls -s {} \; | sort -n -r | tail -1
find . -type f -exec ls -s {} \; | sort -n | head -1

To find all hidden files from the directory:
find -type d -name ".*"

find –print and find is same as –print is a default option of find command.
find –print0 should be used to avoid any issue with white space in file name or path while forwarding output to xargs, also use xargs -0 along with find –print0.

find has an option called –delete which can be used in place of  -exec rm {} \;