Linux: Find Command

来源:互联网 发布:网络管理看什么书 编辑:程序博客网 时间:2024/06/05 14:25

This blog comes from here.

'find' command is a handy command to search for files.  In this post we shall learn to use the find command along with various options that it supports. The find command is available on most Linux by default so you do not have to install any package. The find command is an essential one to learn if you want to get productive.

The basic syntax of this command looks like this

$ find location comparison-criteria search-term


  • 1-Basic examples
      • 1-1 List all files in current and sub dir
      • 1-2 search specific directory or path
      • 1-3 Limit depth of directory traversal
      • 1-4 Invert match
      • 1-5 Combine multiple search criteria
      • 1-6 Search only files or only directories
      • 1-7 Search multiple directories together
      • 1-8 Find hidden files
      • Summary
  • 2-Find files based on permissions
      • 2-1 Find files with certain permissions
      • 2-2find files belonging to particular user
  • 3-Search files and dirs based on modification data and time
      • 3-1Find files modified N days back
      • 3-2find fils accessed in last N days
      • 3-3find files modified in a range of days
      • 3-4find files changed in last N minutes
      • Summary
  • 4-Search files and dirs based on sizes
      • 4-1Find files of given size
      • 2Find files in a size range
      • 3Find empty fiels and dirs
  • Summary

1-Basic examples

1-1. List all files in current and sub dir

find
find .
find . -print

These three commands are the same

1-2. search specific directory or path

find ./test
find ./test -name ‘*.txt’

Ignore the case
It is often useful to ignore the case when searching for the file names with ‘iname’ option instead of the ‘name’ option

find ./test -iname “*.Php”

This search is equivalent to the search term

find ./test -name “*.php”

1-3. Limit depth of directory traversal

find ./test -maxdepth 2 -name “*.php”
find ./test -maxdepth 1 -name “*.php”

1-4. Invert match

find ./test -not -name “*.php”

It is the same as

find ./test ! -name “*.php”

1-5. Combine multiple search criteria

find ./test -name ‘abc*’ ! -name ‘*.php’

The above find command looks for files that begin with abc in their names and do not have a php extension.

OR operator

find ./test -name ‘.php’ -o -name ‘.txt’

The above find command looks for files ending in either php or txt extension

1-6. Search only files or only directories

find ./test -type f -name “abc*”
find ./test -type d -name “abc*”

The first command looks for the files(-type f) with the name begining with abc
The second command looks for the directory(-type d) with the name begining with abc

Quite useful and handy.

1-7. Search multiple directories together

find ./test ./dir2 -type f -name “abc*”

It finds all satisfied files in both ./test and ./dir2 directories.

1-8. Find hidden files

find ~ -type f -name “.*”

Summary

find location with various options and any combinations( and, or, not) of various basic operation(-name, -type, -maxdepth)

2-Find files based on permissions

2-1. Find files with certain permissions

find . -type f -perm 0664
find . -type f ! perm 0777

The second command will avoid the situation that search items which perm 0777, giving wrong warning “Permission Denied”

2-2find files belonging to particular user

find . -user bob

3-Search files and dirs based on modification data and time

3-1.Find files modified N days back

find / -mtime 50

To find all the files which are modified 50 days back.

3-2.find fils accessed in last N days

find / -atime 50

find all files that were accessed in the last 50 days

3-3.find files modified in a range of days

find / -mtime +50 -mtime -100

Find files that were modified between 50 to 100 days ago

3-4.find files changed in last N minutes

find /test -cmin -60

find files modified within the last 1 hour

Summary

-mtime: modification time
-atime: access time
-ctime: change time

4-Search files and dirs based on sizes

4-1.Find files of given size

find / -size 50M

To find all 50MB files.

4.2Find files in a size range

find /test -size +50M -size -100M

To find all the files which are greater than 50MB and less than 100MB

4.3Find empty fiels and dirs

find /test -type f -empty

find all files that are empty

find /test -type -d -empty

Summary

The options described above are:

-name
-type
-maxdepth
-size ( -size +10M -size -100M)
-mtime, -atime, -ctime
-empty

0 0