find命令使用

find命令使用

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

find常用参数
-name 按文件名查找
[root@gsh /]# find . -name "*.txt"
./etc/pki/nssdb/pkcs11.txt
./root/test.txt
./root/passwd.txt
./root/333.txt
./root/test_softlink.txt
./var/cache/yum/x86_64/7/base/mirrorlist.txt
./var/cache/yum/x86_64/7/timedhosts.txt
./var/cache/yum/x86_64/7/extras/mirrorlist.txt
./var/cache/yum/x86_64/7/updates/mirrorlist.txt
./tmp/test.txt
[root@gsh /]#
-type 按文件类型查找

b 块文件

c 字符文件

l 软连接文件

f 普通文件

d 目录文件

[root@gsh /]# find /dev -type b
/dev/sr0
/dev/sda3
/dev/sda2
/dev/sda1
/dev/sda
[root@gsh /]#
-mtime 按文件的修改时间查找
[root@gsh ~]# find /root -mtime +3 -exec ls -l {} \;
-rw-r--r--. 1 root root 18 Dec 29  2013 /root/.bash_logout
-rw-r--r--. 1 root root 176 Dec 29  2013 /root/.bash_profile
-rw-r--r--. 1 root root 176 Dec 29  2013 /root/.bashrc
-rw-r--r--. 1 root root 100 Dec 29  2013 /root/.cshrc
-rw-r--r--. 1 root root 129 Dec 29  2013 /root/.tcshrc
-rw-------. 1 root root 4555 Sep 14 10:47 /root/.viminfo
-rw-------. 1 root root 35 Sep 21 12:01 /root/.lesshst
-rw-r--r--. 1 root root 6 Sep 22 10:27 /root/1
-rw-r--r--. 1 root root 23 Sep 22 15:02 /root/passwd.txt
-r--r--r--. 1 root root 0 Sep 22 16:07 /root/333.txt
total 0
[root@gsh ~]#
-size 按文件大小来查找 (+10M指超过10M,-10M是指小于10M)
[root@gsh /boot]# find . -size +10M |xargs ls -lh
-rw-------. 1 root root 56M Sep  3 16:36 ./initramfs-0-rescue-62a5d1b1d99b43dda6e521dec7b1537d.img
-rw-------. 1 root root 19M Sep  3 16:37 ./initramfs-3.10.0-1127.el7.x86_64.img
-rw-------. 1 root root 11M Sep  3 16:38 ./initramfs-3.10.0-1127.el7.x86_64kdump.img
[root@gsh /boot]#
find与xargs、-exec连用
xargs 使用
   xargs option  [command [initial-arguments]]
-i 将前一段结果作为命令(需要两个参数)的参数
[root@gsh /boot]# find . -size +10M |xargs -i cp {} /root
[root@gsh /boot]# ls /root/
1                                                        ls.1.gz
333dir                                                   passwd.txt
333.txt                                                 test_softlink.txt
initramfs-0-rescue-62a5d1b1d99b43dda6e521dec7b1537d.img test.txt
initramfs-3.10.0-1127.el7.x86_64.img                     test.txt_hardlink
initramfs-3.10.0-1127.el7.x86_64kdump.img
[root@gsh /boot]# find . -size +10M |xargs ls -lh
-rw-------. 1 root root 56M Sep  3 16:36 ./initramfs-0-rescue-62a5d1b1d99b43dda6e521dec7b1537d.img
-rw-------. 1 root root 19M Sep  3 16:37 ./initramfs-3.10.0-1127.el7.x86_64.img
-rw-------. 1 root root 11M Sep  3 16:38 ./initramfs-3.10.0-1127.el7.x86_64kdump.img
-n指定每行的最大参数量为n个
-d指定分隔符
[root@gsh /boot]# echo "123a123a123a" | xargs -n 2 -d a
123 123
123

[root@gsh /boot]#
find命令结合-exec和xargs使用的区别
区别xargs-exec
区别一 该命令是将找到的结果一次性传给后面的命令去执行,命令执行效率高,可以使用-n参数控制一次应传输的个数 该参数是将找找到的结果文件名诸葛传递给后面的命令来执行,如果文件个数多时,则执行效率底下
区别二 处理特殊的文件名,需要采取特殊的方式-0参数find . -size +10M | xargs -0 ls -l 文件名如果有空格等也可以照常处理
     
posted @ 2020-09-27 15:59  Gsh-123  阅读(161)  评论(0)    收藏  举报