find command in linux

• Find all the files, directories and sub directories under a directory

find <directory>  
find . #under current directory

• Find only directories or files specificaly

find . -type d #find all the directories under current directory
find . -type f #find all the files under current directory

• Find file with a name

find . -type f -name "test1.txt"  # File search by name
find . -type f -name "text*"      # Wild card search
find . -type f -iname "test1.txt" # Case insensitive search
find . -type f -not -name "*.sh" # File name not ending with *.sh

• All files modified by time search

find . -type f -mmin -10       # Files modified in last 10 minutes
find . -type f -mmin +10       # Files modified more than 10 minutes ago
find . -type f -mmin +1 -mmin -5 # Files modified more than 1 min ago but less than 5 minutes
find . -type f -mtime -10      # Files modified less than 10 days ago
#amin and atime for access minutes and change access days
#cmin and ctime for change minutes and change days

• Find files with size

find . -size +5M          # Files with size more than 5 Mb in size
find . -size +500k        # Files with size more than 500 Kb in size
find . -size  +5G         # Files with size more than 5Gb in size
find . -empty             # Files which are empty

• Find files with permission

find . -perm 777           # Files and folders with 777 permission on them

• Execute a command on the results of find

find demo -exec chown tyson:www-data {} + 

In the above command -exec is the attribute which helps to execute an another command on the results of the find and {} is the place holder that signifies the result of each output of find.
+ sign is under to end the command here, you also use / instead of +

• Find files under only 1 level under current directory (Max Depth)

find . -type f -name "*.txt" -maxdepth 1

• Delete files which are older than X days

find . -type f -mtime +90 -delete

Leave a Comment