说道查找命令,第一个想到的就是find,用法多样,加上-exec,你能够玩的很开心。小缺点是须要遍历目录下全部,查找会比较慢。例如遍历/下查找,尤为是系统中文件很是多时,你能够先去听首歌,再来看结果(固然了,这有点夸张!)。vim
[root@salt-master test03]# find /etc/ -name network /etc/sysconfig/network /etc/vmware-tools/scripts/vmware/network /etc/rc.d/init.d/network
通常状况下,查找范围越大,目录层级越深,查找速度就越慢。bash
[root@salt-master test03]# time find / -name ifcfg-eth0 real 0m0.850s user 0m0.239s sys 0m0.598s
注意:real远大于user加上sys,由于find须要遍历各个目录,须要大量的I/O操做,而磁盘I/O一般是最慢的环节,所以大部分时间find进程都在等待磁盘I/O完成。less
-name: 按名字查找 -iname:忽略大小写
[root@salt-master test03]# find /etc -name networkmanager [root@salt-master test03]# find /etc -iname networkmanager /etc/NetworkManager
f: 普通文件 d: 目录 l: 软链接 ......
[root@salt-master test03]# ll total 4 drwxr-xr-x 2 root root 4096 Dec 13 15:47 01 -rw-r--r-- 1 root root 0 Dec 13 15:47 02 [root@salt-master test03]# find . -type f ./02 [root@salt-master test03]# find . -type d . ./01
[root@salt-master test03]# stat 文件名 能够查看文件的三个时间 atime:访问时间,cat more less ... ... mtime:文件的内容发生变化的时间,vim ... ... (也是 ll 所显示的时间) ctime:文件的属性发生变化的时间,好比:权限改变、大小改变、全部者、所属组等改变 -atime n 以天为单位 -amin n 以分钟为单位 -mtime n 以天为单位 n为数字,前面能够带+或者-号 +n:n+1天以前 n :n到n+1天之间 -n:n天之内
1.查找/find下修改时间在24小时(1天)以内的文件和目录ide
[root@salt-master test03]# find . -mtime -1 ./01 ./02
2.查找/find下修改时间在2天前的普通文件 ui
[root@salt-master test03]# ll total 8 drwxr-xr-x 2 root root 4096 Dec 13 15:47 01 -rw-r--r-- 1 root root 0 Dec 13 15:47 02 -rw-r--r-- 1 root root 193 Apr 12 2019 11.txt [root@salt-master test03]# find . -type f -mtime +1 ./11.txt
-user 用户名 -group 组名 -uid uid -gid gid -nouser:孤儿文件,没有全部者的文件 -nogroup:没有所属组的文件
1.查找系统中全部者是finduser的文件this
[root@salt-master test03]# useradd finduser [root@salt-master test03]# find / -user finduser -type f /var/spool/mail/finduser /home/finduser/.bash_logout /home/finduser/.bashrc /home/finduser/.bash_profile
2.查找系统中的孤儿文件code
[root@salt-master test03]# userdel finduser [root@salt-master test03]# find /home/ -type f -nouser /home/finduser/.bash_logout /home/finduser/.bashrc /home/finduser/.bash_profile
+ 大于 - 小于 直接数字 等于 ‘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@salt-master test03]# ll -h total 5.1M drwxr-xr-x 2 root root 4.0K Dec 13 15:47 01 -rw-r--r-- 1 root root 0 Dec 13 15:47 02 -rw-r--r-- 1 root root 5.0M Dec 13 16:40 04 -rw-r--r-- 1 root root 193 Apr 12 2019 11.txt [root@salt-master test03]# find . -type f -size 3M [root@salt-master test03]# find . -type f -size -3M ./02 ./11.txt [root@salt-master test03]# find . -type f -size 3M [root@salt-master test03]# find . -type f -size +3M ./04
-exec 动做 -- 找到结果以后直接执行动做 -ok 动做 -- 执行动做以前先提示,即须要交互
[root@salt-master test03]# find . -type f -size +3M ./04 [root@salt-master test03]# find . -type f -size +3M -exec ls -l {} \; -rw-r--r-- 1 root root 5242880 Dec 13 16:40 ./04 {} —— 用来代替找到的结果 \; —— 表示结束标志
[root@salt-master test03]# find . -type f -size +3M -ok ls -l {} \; < ls ... ./04 > ? y -rw-r--r-- 1 root root 5242880 Dec 13 16:40 ./04 [root@salt-master test03]# find . -type f -size +3M -ok ls -l {} \; < ls ... ./04 > ? n
例如,有时候只想修改当前目录下全部文件权限为644,但不想修改目录权限,能够以下操做
find ./* -type f -exec chmod 644 {} \;进程
了解了-exec后,find和其余命令的组合起来使用,会使它的功能变得会很是强大和方便。ip