loading...

linux : find

zakiarsyad profile image Zaki Arrozi Arsyad Updated on ・3 min read

We can search files or directories in linux using find

Basic usage :

find DIRECTORY OPTION WHAT_TO_FIND

DIRECTORY :

We can specify where we want to run the find command.

By default, if we don't specify the directory it will call the current directory ./

# find FILE.txt in home directory
$ find /home -name "FILE.txt"

# find all .txt files in the current directory
$ find -name "*.txt"

OPTIONS :

  • -type f : find files
# find matched files
$ find -type f -name "FILE.txt"

by default find is finding files. we can also use -type f for file type

  • -type d : find a directories
# find matched directories
$ find -type d -name "DIRECTORY"
  • -iname : find with case insensitive
# find a FILE.txt or file.txt
$ find -iname "FILE.txt"
  • -print : display the path name

    We can use -print option or not with the sam result.

# find files
$ find -name "FILE.txt" -print

# find directories
$ find -type d -name "DIRECTORY"
  • -size : find by size
# find files smaller than 10MB
$ find -size -10M

# find files larger than 10MB
$ find -size +10M

# find files exactly 3 10MB
$ find -size 10M

# find files between 10MB and 1GB
$ find -size +10M -size -1G
  • -empty or -size 0 : find empty files or directories
# find an empty files
$ find -type f -empty

# find an empty directories
$ find -type d -size 0
  • -exec CMD : execute a command after
# execute grep after find is done
$  find -name "FILE.txt" -exec grep "KEYWORD" {} \;
  • -delete or -exec rm {} \; : delete matched files or directories
# delete an mathed files
$ find -name "*.bak" -delete

# delete an empty directories
$ find -type d -size 0 -exec rmdir {} \;
  • not or ! : exclude the matched files or directories
# find all except the matched files
$ find -not -name "FILE.txt"
  • -mtime : find by modified time

    we also can use -mmin if we want to specify the time interval to minutes

# find files modified within the last 3 days
$ find -name "FILE.txt" -mtime -3

# find files modified more than 3 days
$ find -name "FILE.txt" -mtime +3

# find files modified exactly 3 days
$ find -name "FILE.txt" -mtime 3

# find files modified between 3 and 10 days
$ find -name "FILE.txt" -mtime +3 -mtime -10
  • -atime : find by accessed time

    we also can use -amin if we want to specify the time interval to minutes

# find files accessed within the last 3 days
$ find -name "FILE.txt" -atime -3

# find files accessed more than 3 days
$ find -name "FILE.txt" -atime +3

# find files accessed exactly 3 days
$ find -name "FILE.txt" -atime 3

# find files accessed between 3 and 10 days
$ find -name "FILE.txt" -atime +3 -atime -10
  • -ctime : find by changed time

    we also can use -cmin if we want to specify the time interval to minutes

# find files changed within the last 3 days
$ find -name "FILE.txt" -atime -3

# find files changed more than 3 days
$ find -name "FILE.txt" -atime +3

# find files changed exactly 3 days
$ find -name "FILE.txt" -atime 3

# find files changed between 3 and 10 days
$ find -name "FILE.txt" -atime +3 -atime -10
  • -perm : find by permission
# find files with permission 777
$ find -perm 777
  • -user : find by user access
# find matched files with user root
$ find -user root -name "FILE.txt" 
  • -group : find by group access
# find matched files with group developer
$ find -group developer -name "FILE.txt"
  • find hidden files
$ find -name ".*"

Posted on by:

Discussion

pic
Editor guide