DEV Community

Zaki Arrozi Arsyad
Zaki Arrozi Arsyad

Posted on • Edited on

1 2

linux : find

We can search files or directories in linux using find

Basic usage :

find DIRECTORY OPTION WHAT_TO_FIND
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

OPTIONS :

  • -type f : find files
# find matched files
$ find -type f -name "FILE.txt"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode
  • -iname : find with case insensitive
# find a FILE.txt or file.txt
$ find -iname "FILE.txt"
Enter fullscreen mode Exit fullscreen mode
  • -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"
Enter fullscreen mode Exit fullscreen mode
  • -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
Enter fullscreen mode Exit fullscreen mode
  • -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
Enter fullscreen mode Exit fullscreen mode
  • -exec CMD : execute a command after
# execute grep after find is done
$  find -name "FILE.txt" -exec grep "KEYWORD" {} \;
Enter fullscreen mode Exit fullscreen mode
  • -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 {} \;
Enter fullscreen mode Exit fullscreen mode
  • not or ! : exclude the matched files or directories
# find all except the matched files
$ find -not -name "FILE.txt"
Enter fullscreen mode Exit fullscreen mode
  • -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
Enter fullscreen mode Exit fullscreen mode
  • -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
Enter fullscreen mode Exit fullscreen mode
  • -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
Enter fullscreen mode Exit fullscreen mode
  • -perm : find by permission
# find files with permission 777
$ find -perm 777
Enter fullscreen mode Exit fullscreen mode
  • -user : find by user access
# find matched files with user root
$ find -user root -name "FILE.txt" 
Enter fullscreen mode Exit fullscreen mode
  • -group : find by group access
# find matched files with group developer
$ find -group developer -name "FILE.txt"
Enter fullscreen mode Exit fullscreen mode
  • find hidden files
$ find -name ".*"
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay