Find command
1
[[email protected] ~]# ls
AAA aaa.sh anaconda-ks.cfg Install.log Install.log.syslog
[[email protected] ~]# find./*-name "[aa]*.sh"-exec rm {} \;
[[email protected] ~]# ls
AAA anaconda-ks.cfg Install.log Install.log.syslog
Note RM Space {} space \; (seal number), one can not be less!.
-exec is the command that will find the file to be executed for some kind of command!
The-name parameter is followed by the name of the file you want to find with "", and the file is generally not "".
The-name parameter can be used with regular expressions, such as the "[aa]*.sh", the. sh suffix file that begins with AA.
2
[[email protected] ~]# ls
AAA anaconda-ks.cfg Install.log Install.log.syslog
[Email protected] ~]# find ~/*-mtime-1-type f-print
/root/aaa
-mtime find files by change time +n means n days ago n =
-type indicates that the file type parameters b/d/c/p/l/f are block devices, directories, character devices, pipelines, symbolic links, normal files,-print printing.
[Email protected] ~]# find./*-mtime +1-type F-print
./anaconda-ks.cfg
./install.log
./install.log.syslog
3
[Email protected] ~]# ls-l |grep AAA
-rw-r--r--1 root root 134 August 16:25 AAA
[Email protected] ~]# chmod 0777 AAA
[Email protected] ~]# ls-l |grep AAA
-RWXRWXRWX 1 root root 134 August 16:25 AAA
[Email protected] ~]# find./*-cmin-1-type F-print
./aaa
[Email protected] ~]#
-cmin-ctime the file creation time to find the file-cminb represents the number of minutes-ctime represents the number of days-1 for Min, the above example changes the properties of AAA.
4
[Email protected] share]# LS-LH
Total dosage 28M
-rw-r--r--1 root root 54 July 23:23 Aaa.txt
-rw-r--r--1 root root 6 July 23:19 BBB
-rw-r--r--1 root root 9 July 23:21 ccc.txt
-rw-r--r--1 root root 28M August 18:27 linux_firewall.pdf
[[email protected] share]# find. -type f-size +10-exec ls-l {} \;
-rw-r--r--1 root root 28663077 August 18:27./linux_firewall.pdf
[Email protected] share]#
[[email protected] share]# find. -type f-size-10-exec ls-l {} \;
-rw-r--r--1 root root 54 July 23:23./aaa.txt
-rw-r--r--1 root root 6 July 23:19./bbb
-rw-r--r--1 root root 9 July 23:21./ccc.txt
In the example above,-size find file size By default is the block parameter is N[c], where C stands for bytes, 1 blocks = 512 bytes =512*8 (bit)
-exec executes the command ls-l the found results to show the details: +10=10 files larger than 10 blocks-10 files smaller than 10 are examples of files that look for file sizes of 6 bytes:
[[email protected] share]# find. -type f-size 6c-exec ls-l {} \;
-rw-r--r--1 root root 6 July 23:19./bbb
5
[Email protected] share]# ls-l
Total dosage 28004
-rwxrwxrwx 1 root root 54 July 23:23 Aaa.txt
-rw-r--r--1 root root 6 July 23:19 BBB
-rw-r--r--1 root root 9 July 23:21 ccc.txt
-rw-r--r--1 root root 28663077 August 18:27 linux_firewall.pdf
[Email protected] share]# find-perm 0777-print
.
./aaa.txt
[Email protected] share]#
-perm by Execute permissions to find 0777= u=rwx,g=rwx,o=rwx and print, you can see one more. This point represents the current directory, is the system default does not matter with LS-A can see each folder will have two hidden files. And.. 。
6
[[email protected] share]# find. -type f-size +10-exec rm {} \;
Locate the file that is larger than 10 in the current path and delete it.
[Email protected] share]# ls-l
-rwxrwxrwx 1 root root 54 July 23:23 Aaa.txt
-rw-r--r--1 root root 6 July 23:19 BBB
-rw-r--r--1 root root 9 July 23:21 ccc.txt
Then the sort-n is sorted by numeric size-R is descending, by default ascending, Head-n-n is the display of the first nth row.
[[email protected] share]# find. -type f-exec ls-l {} \; | Sort-n-R | Head-2
-rwxrwxrwx 1 root root 54 July 23:23./aaa.txt
-rw-r--r--1 root root 9 July 23:21./ccc.txt
---------------------------------------------------------------------
Linux Find Command Example