您的位置:首页 > 运维架构 > Linux

linux之find命令详解

2015-11-04 20:19 507 查看
用法

  find [path] 内容

参数

  -print: find命令将匹配的文件输出到标准输出
  -exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' { } \;,注意{ }和\;之间的空格。

    find -exec rm -rf {} \; 将找到的文件删除
  -ok: 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。 

    [root@mysql test]# find -ok rm -rf {} \;
    < rm ... . > ? y 删除之前先询问

  -depth: #使查找在进入子目录前先行查找完本目录

  -maxdepth 指定最多查找几层

     find -maxdepth 1 不对当前目录下的子目录进行查找

     find -maxdepth 2 只对当前目录下的一级目录进行查找

按文件名称查找

  -name 支持通配符

查找以“.sh结尾的文件”
[root@mysql ~]# find -name '*.sh'
./tar.sh
[root@mysql ~]#


按文件时间戳查找

  -mtime -n +n #按文件更改时间来查找文件,-n指n天以内,+n指n天以前

  -atime -n +n #按文件访问时间来查

  -ctime -n +n #按文件创建时间来查找文件,-n指n天以内,+n指n天以前

  -mmin -n +n #按文件更改时间来查找文件,-n指n分钟以内,+n指n分钟以前

  -amin -n +n #按文件访问时间来查

  -cmin -n +n #按文件创建时间来查找文件,-n指n分钟以内,+n指n分钟以前

[root@mysql ~]# find -cmin -4   查找4分钟内创建的文件
.
./hhh
[root@mysql ~]# find -ctime +3   查找3天创建的文件
./.cshrc
./.bashrc
./.bash_profile
./.bash_logout
./anaconda-ks.cfg
./install.log.syslog
./.tar
./tar.sh
[root@mysql ~]#


按文件类型查找

  -type d/l/b/c/f/s

[root@mysql ~]# find -maxdepth 1 -type d 查看当前目录下都有哪些文件夹
.
./test
./.subversion
[root@mysql ~]#

按文件的属主,属组查找

  -nogroup #查无有效属组的文件,即文件的属组在/etc/groups中不存在
  -nouser #查无有效属主的文件,即文件的属主在/etc/passwd中不存

  -user username #按文件属主来查找
  -group groupname #按组来查找

按文件权限查找

  -perm

[root@mysql ~]# find -perm 600 -exec ls -l {} \; 查找当前文件下权限为600的文件并以长格式显示
-rw------- 1 root root 49 11? 3 18:57 ./.lesshst
-rw-------. 1 root root 21820 11? 3 16:22 ./.bash_history
-rw------- 1 root root 19599 11? 1 01:38 ./.mysql_history
-rw------- 1 root root 5125 11? 3 00:18 ./.viminfo
-rw-------. 1 root root 1087 6? 13 21:14 ./anaconda-ks.cfg
[root@mysql ~]#

按文件的大小查找

  -size [-/+]n

  'b' for 512-byte blocks (this is the default if no suffix is used)

  'c' for bytes

  'w' for two-byte words

  'k' for Kilobytes (units of 1024 bytes)

  'M' for Megabytes (units of 1048576 bytes)

  'G' for Gigabytes (units of 1073741824 bytes)

[root@mysql ~]# find -size +20000c    查找大于20000c字节的文件
./.bash_history
./install.log
[root@mysql ~]#

[root@mysql ~]# find -size 20000c    查找刚好20000c字节的文件

[root@mysql ~]# find -size -20000c   查找小于20000c字节的文件


查找的时候排除某个目录

  find /root/ -path "/root/test" -prune -o -print 查找除了/root/test目录之外的/root目录下的文件

查找的时候排除多个目录

[root@BASE test]# pwd
/root/test
[root@BASE test]# tree
.
|-- aaa
|-- test1
|   `-- 1
|-- test2
|   `-- 2
`-- test3
`-- 3

3 directories, 4 files
[root@BASE test]# find /root/test/ \( -path "/root/test/test1" -o -path "/root/test/test2" \) -prune -o -type f -print
/root/test/aaa
/root/test/test3/3
[root@BASE test]#


查找的时候排除某一类文件

  “!”表示非

[root@mysql ~]# find -maxdepth 1 ! -name "\.*"  显示不是隐藏属性的文件
./test
./hhh
./install.log
./EOF
./test2
./anaconda-ks.cfg
./install.log.syslog
./tar.sh
[root@mysql ~]#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: