find用法
find path -option
选项
| 选项 | 功能 | |
| -name filename | 查找文件名为filename的文件 | |
| -perm 777 | 按权限来查找 | |
| -user | 按文件属主来查找 | |
| -group | 按文件属组来查找 | |
| -mtime -n/+n | 按文件更改时间来查找文件,-n指n天以内,+n指那天以前 | |
| -atime | 按文件访问时间来查 | |
| -ctime | 按文件创建时间来查 | |
| -nogroup | 查无有效属组的文件,即文件属组在/etc/groups里不存在 | |
| -nouser | 查无有效属主的文件,即文件属主在/etc/passwd里不存在 | |
| -newer f1 !f2 | 查更改时间比f1新但比f2旧的文件 | |
| -type b/d/c/p/l/f | 查看块设备、目录、字符设备、管道、符号链接、普通文件 | |
| -size | 查看长度为n块的文件 | |
| -depth | 使查找在进入子目录前先行查找完本目录 | |
| -fstype | 查位于某一类型文件系统中的文件,这些文件系统类型通常可在/etc/fstab中找到 | |
| -mount | 查文件时不跨越文件系统mount点 |
查看目录下所有文件并判断文件类型
[root@web ~]# find . -type f|xargs file ./.bash_profile: ASCII English text ./install.log: ASCII text ./anaconda-ks.cfg: ASCII English text ./.bash_history: ASCII text, with very long lines ./install.log.syslog: ASCII text
在整个系统中查找内存信息转储文件(core dump) ,然后把结果保存到/tmp/core.log 文件中
[root@web ~]# find / -name "core" |xargs echo "" >/tmp/core.log
在当前目录下查找所有用户具有读、写和执行权限的文件,并收回相应的写权限
模拟环境:
[root@web test]# for n in `seq 9`;do touch log000$n.log;done [root@web test]# for n in `seq 4 9`;do chmod 777 log000$n.log;done [root@web test]# ll total 0 -rw-r--r-- 1 root root 0 Oct 27 15:52 log0001.log -rw-r--r-- 1 root root 0 Oct 27 15:52 log0002.log -rw-r--r-- 1 root root 0 Oct 27 15:52 log0003.log -rwxrwxrwx 1 root root 0 Oct 27 15:52 log0004.log -rwxrwxrwx 1 root root 0 Oct 27 15:52 log0005.log -rwxrwxrwx 1 root root 0 Oct 27 15:52 log0006.log -rwxrwxrwx 1 root root 0 Oct 27 15:52 log0007.log -rwxrwxrwx 1 root root 0 Oct 27 15:52 log0008.log -rwxrwxrwx 1 root root 0 Oct 27 15:52 log0009.log
执行命令和结果:
[root@web test]# find . -perm -7|xargs chmod o-w [root@web test]# ll total 0 -rw-r--r-- 1 root root 0 Oct 27 15:52 log0001.log -rw-r--r-- 1 root root 0 Oct 27 15:52 log0002.log -rw-r--r-- 1 root root 0 Oct 27 15:52 log0003.log -rwxrwxr-x 1 root root 0 Oct 27 15:52 log0004.log -rwxrwxr-x 1 root root 0 Oct 27 15:52 log0005.log -rwxrwxr-x 1 root root 0 Oct 27 15:52 log0006.log -rwxrwxr-x 1 root root 0 Oct 27 15:52 log0007.log -rwxrwxr-x 1 root root 0 Oct 27 15:52 log0008.log -rwxrwxr-x 1 root root 0 Oct 27 15:52 log0009.log
find . -type f|xargs grep ‘hostname‘
xargs和mv
[root@web test]# ll
total 4
-rw-r--r-- 1 root root 0 Oct 27 15:52 log0001.log
-rw-r--r-- 1 root root 0 Oct 27 15:52 log0002.log
-rw-r--r-- 1 root root 0 Oct 27 15:52 log0003.log
-rwxrwxr-x 1 root root 0 Oct 27 15:52 log0004.log
-rwxrwxr-x 1 root root 0 Oct 27 15:52 log0005.log
-rwxrwxr-x 1 root root 0 Oct 27 15:52 log0006.log
-rwxrwxr-x 1 root root 0 Oct 27 15:52 log0007.log
-rwxrwxr-x 1 root root 0 Oct 27 15:52 log0008.log
-rwxrwxr-x 1 root root 0 Oct 27 15:52 log0009.log
drwxr-xr-x 2 root root 4096 Oct 27 16:06 logs
[root@web test]# find . -perm 644|xargs -i mv {} logs
[root@web test]# tree
.
├── log0004.log
├── log0005.log
├── log0006.log
├── log0007.log
├── log0008.log
├── log0009.log
└── logs
├── log0001.log
├── log0002.log
└── log0003.log
1 directory, 9 files使用-i参数默认的前面输出用{}代替
原文地址:http://chboy.blog.51cto.com/9959876/1706919