Find files with read-only permissions for group users (using octal permission form).
1 |
find . -perm 040 -type f -exec ls -l {} \; |
2 |
----r----- 1 root root 0 2009-02-19 20:27 ./others_can_only_read |
8. Find all empty files (0-byte files) in the home directory and subdirectories
Most of the output files of the following commands are locked file boxes Place hoders created by other programs
Only list empty files in your home directory.
1 |
find . -maxdepth 1 -empty |
# Only list non-hidden empty files in the current directory.
1 |
find . -maxdepth 1 -empty -not -name ".*" |
9. Find the 5 largest files
The following command lists the 5 largest files in the current directory and subdirectories. This can take a while, depending on the number of files the command needs to process.
1 |
find . -type f -exec ls -s {} \; | sort -n -r | head -5 |
10. Find the 5 smallest files
The method is similar to the method of finding the 5 largest files, the only difference is that the sort order is descending order.
1 |
find . -type f -exec ls -s {} \; | sort -n | head -5 |
In the above command, it is very likely that what you see is just an empty file (0-byte file). So, you can use the following command to list the smallest files instead of 0 byte files.
1 |
find . -not -empty -type f -exec ls -s {} \; | sort -n | head -5 |
11. Use -type to find files of the specified file type
Only search for socket files
Find all directories
Find all general files
Find all hidden files
1 |
find . -type f -name ".*" |
Find all hidden directories
1 |
find -type d -name ".*" |
12. Find files by comparing modification times with other files
Display files that were modified after the specified file. The find command below will display all files created and modified after ordinary_file.
##03
-rw-r----- 1 root root 0 2009-02-19 20:27 others_can_also_read |
|
##04
----r----- 1 root root 0 2009-02-19 20:27 others_can_only_read
|
|
##05
-rw------- 1 root root 0 2009-02-19 20:29 ordinary_file
|
|
##06
-rw-r--r-- 1 root root 0 2009-02-19 20:30 everybody_read
|
|
##07
-rwxrwxrwx 1 root root 0 2009-02-19 20:31 all_for_all
|
| ##08
---------- 1 root root 0 2009-02-19 20:31 no_for_all
| ##10
| # find -newer ordinary_file
##14
./no_for_all |
|
13. Find files by file size
Use the -size option to find files by file size.
Find files larger than the specified file |
|
1
find ~ -size +100M
Find files smaller than the specified file
Find files matching the given size
Note: – means smaller than the given size, + means larger than the given size. No symbol indicates exactly the same size as a given size.
14. Give aliases to commonly used find operations
If you find something useful, you can give it an alias. And execute it anywhere you want.
Commonly used to delete the a.out file.
1 |
alias rmao="find . -iname a.out -exec rm {} \;" |
Delete the core file generated by the c program.
1 |
alias rmc="find . -iname core -exec rm {} \;" |
15. Use the find command to delete large packaged files
The following command deletes *.zip files larger than 100M.
1 |
find / -type f -name *.zip -size +100M -exec rm -i {} \;" |
Use the alias rm100m to delete all *.tar files of Heavy Rain 100M. Using the same idea, you can create a category name of rm1g, rm2g, and rm5g to delete all files larger than 1G, 2G, and 5G.
1 |
alias rm100m="find / -type f -name *.tar -size +100M -exec rm -i {} \;" |
2 |
# alias rm1g="find / -type f -name *.tar -size +1G -exec rm -i {} \;" |
##3
| # alias rm2g="find / -type f -name *.tar -size +2G -exec rm -i {} \;"
|
##4
# alias rm5g="find / -type f -name *.tar -size +5G -exec rm -i {} \;" |
|
##6
# rm5g
|
Find command example (Part 2) |
|