Power Of Find Command In Linux
What is find command in linux?
The find command is a powerful tool in Linux used to search for files and directories within a directory hierarchy based on various criteria, such as size, name, modification date, and permissions. This command is particularly useful for locating files across complex directory structures, filtering results based on specific needs.
It has a lot of options and filters that you can use to find the files or directories.
Syntax
find [path] [options] [expr]
OPTIONS
-empty = empty file
-name = specify file name
-type = specify type of file f for regular file d for directories , l for symbolic link and s for socket file etc
-iname = specify name to search with case insensitive
-not or ! = not condition
-perm = specify permission
-mtime = modify time in day
-atime = access time in day
-ctime = change time in day
-mmin = modify time in min
-cmin = change time in min
-amin = access time in min
-size = specify the size
-regex = regular expression pattern
-user = specify user
-delete = to delete files which has been found
-exec = to execute command on found file
1.Find the file which is empty
find . -empty -type f # -type f represents a regular files
This command searches for empty file in currect directory (.)
2.Find the file which name starts from docker
find . -type f -iname docker*
This command searches for files from current directory which name starts from docker
3.Find the file which name not starts from docker
find . -type f -not -iname docker*
find . -type f ! -iname docker*
This command searches for files from current directory which name not starts from docker
4.Find the file which extention is .py
find . -type f -iname *.py
This command searches for files from current directory which has extention .py
5. FInd the file which size is 10MB
find . -type f -size 10M # for Bytes = c, KB = k, MB = M, GB = G, TB = T
This command searches for files from current directory which size is exactly 10 MB
6. FInd the file which size is greater than 10MB
find . -type f -size +10M
This command searches for files from current directory which size is greater than 10 MB
7. FInd the file which size is less than 10MB
find . -type f -size -10M
This command searches for files from current directory which size is less than 10MB
8. FInd the file which size is greater than 10MB and less than 100MB
find . -type f -size +10M -size -100M
This command searches for files from current directory which size is greated than 10MB and less than 100MB
9. Find directories (folder) which name is blogs
find . -type d -name blogs
This command searches from current directories and find the file which name is blogs
10. Find the file which owner is John
find / -type f -user john
This command searches file from root directories and find the file which owner is John
11.Find the file which has execute permission for user group and others
find ~ -type f -perm -u=x,g=x,o=x # using symbolic notation
find ~ -type f -perm 111 # using numeric 1 means execute permission
This command searches the file from user home directory which has execute permission for user, group and others.
12.Find the file which is modified within 7 days
find . -type f -mtime -7
This command searches file from current directories and find the file which is modified within 7 days
13.Find the file which is accessed within 7 days
find . -type f -atime -7
This command searches file from current directories and find the file which is accessed within 7 days
14.Find the file which is changed with 10 days
find . -type f -ctime -10
This command searches file from current directories and find the file which is changed within 10 days
15.Find the file which is modified within 1 hour
find . -type f -mmin -60
This command searches file from current directories and find the file which is modified within 1 hour
16.Find the file which is modified after 1 hour
find . -type f -mmin +60
This command searches file from current directories and find the file which is modified after 1 hour
17. Delete the file which is empty and not modified within 7 days
find . -type f -empty -mtime +7 -delete
This comand will delete the all empty file which is not modified within 7 days
18.Find the empty file and check their permission
find . -type f -empty -exec ls -ltr {} \; # {} is a placeholder which take one by one file and print their permission \; end of command to be executed.
19.Find the file using regular expression pattern
find ~ -type f -regex “.*docker.*”
This command searches file from user home directory and find the file based on pattern
Keep learning keep growing !
Comments