Centos7中find命令的用法,以及find+exec,find+xargs

Centos7中find命令的用法,以及find+exec,find+xargs

2018年12月02日 16:35:07 mccok_MC 阅读数 616shell

一、查找文件安全

find / -name ‘filename’bash

二、查找目录spa

find / -name ‘path’ -type d操作系统

三、查找内容.net

find . | xargs grep -ri ‘content’
//find . | xargs grep -ril ‘content’ 只显示文件名称
---------------------------------------------------------------------------------------------------------------------get

经常使用的一些find命令的选项:io

find顾名思义就是查找,Linux下find命令提供至关多的查找条件,所以功能比较强大,能够在众多文件或目录下查找你想要的任何文件或目录。class

find 命令格式:find pathname -options [-print -exec -ok...]test

pathname表示find命令所查找的文件或目录的路径。例如:/home/tools/tongyan

-print: find命令将匹配的文件输出到标准输出。
-exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' { } \;,注意{ }和\;之间的空格。
-ok: 和-exec的做用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每个命令以前,都会给出提示,让用户来肯定是否执行。

find经常使用命令格式:

-name   filename             #查找名为filename的文件
-perm                             #按执行权限来查找
-user    username            #按文件属主来查找
-group groupname           #按组来查找
-mtime   -n +n                #按文件更改时间来查找文件,-n指n天之内,+n指n天之前
-atime    -n +n                #按文件访问时间来查GIN: 0px">

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

-nogroup                        #查无有效属组的文件,即文件的属组在/etc/groups中不存在
-nouser                          #查无有效属主的文件,即文件的属主在/etc/passwd中不存
-newer   f1 !f2                # 找文件,-n指n天之内,+n指n天之前 
-ctime    -n +n               #按文件建立时间来查找文件,-n指n天之内,+n指n天之前 
-nogroup                       #查无有效属组的文件,即文件的属组在/etc/groups中不存在
-nouser                         #查无有效属主的文件,即文件的属主在/etc/passwd中不存
-newer   f1 !f2               #查更改时间比f1新但比f2旧的文件
-type    b/d/c/p/l/f         #查是块设备、目录、字符设备、管道、符号连接、普通文件
-size      n[c]                 #查长度为n块[或n字节]的文件
-depth                          #使查找在进入子目录前先行查找完本目录
-fstype                         #查更改时间比f1新但比f2旧的文件
-type    b/d/c/p/l/f        #查是块设备、目录、字符设备、管道、符号连接、普通文件
-size      n[c]                #查长度为n块[或n字节]的文件
-depth                         #使查找在进入子目录前先行查找完本目录
-fstype                        #查位于某一类型文件系统中的文件,这些文件系统类型一般可 在/etc/fstab中找到
-mount                       #查文件时不跨越文件系统mount点
-follow                        #若是遇到符号连接文件,就跟踪连接所指的文件
-cpio                          #查位于某一类型文件系统中的文件,这些文件系统类型一般可 在/etc/fstab中找到
-mount                       #查文件时不跨越文件系统mount点
-follow                        #若是遇到符号连接文件,就跟踪连接所指的文件
-cpio                          #对匹配的文件使用cpio命令,将他们备份到磁带设备中
-prune                       #忽略某个目录

下面具体说明

查找方式有不少种,好比按文件类型(-type),文件名(-name),文件属主(-user),文件所属组)(-group),文件权限(-perm),文件长度(-size),文件修改时间(-mtime)等查找。

文件类型包括:d  目录文件。

                    l  符号连接(指向另外一个文件,相似于瘟下的快捷方式) 。 
                    s  套接字文件。
                    b  块设备文件,二进制文件。
                    c  字符设备文件。
                    p  命名管道文件。
                    -  普通文件,或更准确地说,不属于以上几种类型的文件。

文件权限包括:r    读文件

                    w   写文件

                    x    执行文件操做

exec解释:

-exec 参数后面跟的是command命令,它的终止是以;为结束标志的,因此这句命令后面的分号是不可缺乏的,考虑到各个系统中分号会有不一样的意义,因此前面加反斜杠。
{} 花括号表明前面find查找出来的文件名。
使用find时,只要把想要的操做写在一个文件里,就能够用exec来配合find查找,很方便的。在有些操做系统中只容许-exec选项执行诸如l s或ls -l这样的命令。大多数用户使用这一选项是为了查找旧文件并删除它们。建议在真正执行rm命令删除文件以前,最好先用ls命令看一下,确认它们是所要删除的文件。 exec选项后面跟随着所要执行的命令或脚本,而后是一对儿{ },一个空格和一个\,最后是一个分号。为了使用exec选项,必需要同时使用print选项。若是验证一下find命令,会发现该命令只输出从当前路径起的相对路径及文件名。

应用实例

实例1:ls -l命令放在find命令的-exec选项中

命令:
find . -type f -exec ls -l {} \;
输出:

[root@localhost test]# find . -type f -exec ls -l {} \; 
-rw-r--r-- 1 root root 127 10-28 16:51 ./log2014.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-2.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-3.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-1.log
-rw-r--r-- 1 root root 33 10-28 16:54 ./log2013.log
-rw-r--r-- 1 root root 302108 11-03 06:19 ./log2012.log
-rw-r--r-- 1 root root 25 10-28 17:02 ./log.log
-rw-r--r-- 1 root root 37 10-28 17:07 ./log.txt
-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-2.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-3.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-1.log
[root@localhost test]#


说明:
在shell中用任何方式删除文件以前,应当先查看相应的文件,必定要当心!当使用诸如mv或rm命令时,可使用-exec选项的安全模式。它将在对每一个匹配到的文件进行操做以前提示你。

实例2:在目录中查找更改时间在n日之前的文件并删除它们

命令:
find . -type f -mtime +14 -exec rm {} \;
输出:

[root@localhost test]# ll
总计 328
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root     33 10-28 16:54 log2013.log
-rw-r--r-- 1 root root    127 10-28 16:51 log2014.log
lrwxrwxrwx 1 root root      7 10-28 15:18 log_link.log -> log.log
-rw-r--r-- 1 root root     25 10-28 17:02 log.log
-rw-r--r-- 1 root root     37 10-28 17:07 log.txt
drwxr-xr-x 6 root root   4096 10-27 01:58 scf
drwxrwxrwx 2 root root   4096 10-28 14:47 test3
drwxrwxrwx 2 root root   4096 10-28 14:47 test4
[root@localhost test]# find . -type f -mtime +14 -exec rm {} \;
[root@localhost test]# ll
总计 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
lrwxrwxrwx 1 root root      7 10-28 15:18 log_link.log -> log.log
drwxr-xr-x 6 root root   4096 10-27 01:58 scf
drwxrwxrwx 2 root root   4096 11-12 19:32 test3
drwxrwxrwx 2 root root   4096 11-12 19:32 test4
[root@localhost test]# 
说明:
在shell中用任何方式删除文件以前,应当先查看相应的文件,必定要当心!当使用诸如mv或rm命令时,可使用-exec选项的安全模式。它将在对每一个匹配到的文件进行操做以前提示你。

实例3:在目录中查找更改时间在n日之前的文件并删除它们,在删除以前先给出提示

命令:
find . -name “*.log” -mtime +5 -ok rm {} \;
输出:

[root@localhost test]# ll
总计 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
lrwxrwxrwx 1 root root      7 10-28 15:18 log_link.log -> log.log
drwxr-xr-x 6 root root   4096 10-27 01:58 scf
drwxrwxrwx 2 root root   4096 11-12 19:32 test3
drwxrwxrwx 2 root root   4096 11-12 19:32 test4
[root@localhost test]# find . -name "*.log" -mtime +5 -ok rm {} \;
< rm ... ./log_link.log > ? y
< rm ... ./log2012.log > ? n
[root@localhost test]# ll
总计 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
drwxr-xr-x 6 root root   4096 10-27 01:58 scf
drwxrwxrwx 2 root root   4096 11-12 19:32 test3
drwxrwxrwx 2 root root   4096 11-12 19:32 test4
[root@localhost test]#
说明:
在上面的例子中, find命令在当前目录中查找全部文件名以.log结尾、更改时间在5日以上的文件,并删除它们,只不过在删除以前先给出提示。 按y键删除文件,按n键不删除。

实例4:-exec中使用grep命令

命令:
find /etc -name “passwd*” -exec grep “root” {} \;
输出:

[root@localhost test]# find /etc -name "passwd*" -exec grep "root" {} \;
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
[root@localhost test]#
说明:
任何形式的命令均可以在-exec选项中使用。 在上面的例子中咱们使用grep命令。find命令首先匹配全部文件名为“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,而后执行grep命令看看在这些文件中是否存在一个root用户。

实例5:查找文件移动到指定目录

命令:
find . -name “*.log” -exec mv {} .. \;
输出:

[root@localhost test]# ll
总计 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 22:49 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test]# cd test3/
[root@localhost test3]# ll
总计 304
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root     61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log
[root@localhost test3]# find . -name "*.log" -exec mv {} .. \;
[root@localhost test3]# ll
总计 0[root@localhost test3]# cd ..
[root@localhost test]# ll
总计 316
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root     61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root   4096 10-27 01:58 scf
drwxrwxr-x 2 root root   4096 11-12 22:50 test3
drwxrwxr-x 2 root root   4096 11-12 19:32 test4
[root@localhost test]#
 

实例6:用exec选项执行cp命令

命令:
find . -name “*.log” -exec cp {} test3 \;
输出:

[root@localhost test3]# ll 总计 0[root@localhost test3]# cd .. [root@localhost test]# ll 总计 316 -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log -rw-r--r-- 1 root root     61 11-12 22:44 log2013.log -rw-r--r-- 1 root root      0 11-12 22:25 log2014.log drwxr-xr-x 6 root root   4096 10-27 01:58 scf drwxrwxr-x 2 root root   4096 11-12 22:50 test3 drwxrwxr-x 2 root root   4096 11-12 19:32 test4 [root@localhost test]# find . -name "*.log" -exec cp {} test3 \; cp: “./test3/log2014.log” 及 “test3/log2014.log” 为同一文件 cp: “./test3/log2013.log” 及 “test3/log2013.log” 为同一文件 cp: “./test3/log2012.log” 及 “test3/log2012.log” 为同一文件 [root@localhost test]# cd test3 [root@localhost test3]# ll 总计 304 -rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log -rw-r--r-- 1 root root     61 11-12 22:54 log2013.log -rw-r--r-- 1 root root      0 11-12 22:54 log2014.log [root@localhost test3]#  

相关文章
相关标签/搜索