Find to delete files
For example, to find all files ending with. txt from the current directory and display them on the screen, command behavior:
Find. -name ' *.txt '-print
To find the two-suffix file, change to the following usage:
Find. (-name *.xml-o-name *.sh)
If you look for a file that is connected by a symbol from the root directory and delete it, the command behavior:
Find/-type l-exec rm {};
Only find the file, does not contain the directory (the line, can not add--print, added will be an error):
Find. -type F
Also, if you are looking for all of Tom's files from the current directory and are displayed on the screen, the command behavior:
Find. -user ' Tom '-print
Also, if you display the. c file name in the current directory that is greater than 20 bytes, the command behavior:
Find. -name "*.C"-size +20c-print
Displays the name of the file that was accessed exactly 10 days ago in the current directory, and the command behavior:
Find. -atime 10-print
Displays the name of the file that was accessed less than 10 days ago in the current directory, and the command behavior:
Find. -atime-10-print
Look for files or directories with permission 640 under the/home directory, command behavior:
Find/home-perm 640
Search for files larger than 100KB in the root directory and display, command behavior:
Find/-size +100k-print
Search for files that are less than 500KB in the root directory, command behavior:
Find/-size-500k-print
Delete files with a file size of 0
Rm-i ' find./-size 0 '
Find./size 0 exec rm {};
Find./size 0|xargs RM-RF
Find all file names in the current directory with a. doc ending, and change the time at more than 5 days, after finding the delete, and before deleting the prompt, command behavior:
Find. -name ' *.doc '-mtime +5-ok rm {};
Find. -mtime +5-name "*.*"-exec rm-f {};
Automatically delete files from current directory five days ago
Note: The back of the backslash can not be less, or will report "find: Missing the"-exec "parameters."
Finds all linked files in the current directory and displays the basic information of the file in a long format, command behavior:
Find. -type l-exec ls-l {};
lrw-rw-r--1 root 07-27 14:34./example2
lrw-rw-r--1 root root 07-27 14:36./example3
lrw-rw-r--1 root 07-27 14:36./example1
Finding a file name in the current directory consists of a lowercase letter, an uppercase alphabet, and two digits, and a file with the. doc extension, and displays the behavior of the command:
Find. -name ' [A-z][a-z][0-9][0-9].doc '-print
To find an empty folder:
Find-type D-empty
But in the use of find to operate, it is not difficult to discover two problems: one is found to spend a relatively long time, the second is find query more than the general use of the script resources to be relatively large. It is also not very good for the processing of documents in a certain time period. So it's more practical to use common scripts.
For example: I want to delete all the June files in a directory, and I can use the following script to implement:
for filename in *; do if [' date-r $filename +%m ' = ' = '];then rm-f $filename; Fi done
Date-r, the--reference= file displays the last modification time of the file's specified file. +%m indicates that only the month is displayed, and if the result is the same as 06, then the subsequent deletion is performed.
And to do so, we can also be accurate to a few points, delete this directory 16 points generated files:
for filename in *; do if [' date-r $filename +%h ' = ' = '];then rm-f $filename; Fi done
Delete all files generated 16:22:
for filename in *; do if [' date-r $filename +%h%m = = "1622"];then rm-f $filename; Fi done
Similarly, you can use the same method to delete all files from a certain month.