2018年12月02日 16:35:07 mccok_MC 阅读数 616
1、查找文件
find / -name ‘filename’
2、查找目录
find / -name ‘path’ -type d
3、查找内容
find . | xargs grep -ri ‘content’ //find . | xargs grep -ril ‘content’ 只显示文件名称 ---------------------------------------------------------------------------------------------------------------------
常用的一些find命令的选项:
find顾名思义就是查找,Linux下find命令提供相当多的查找条件,因此功能比较强大,可以在众多文件或目录下查找你想要的任何文件或目录。
find 命令格式:find pathname -options [-print -exec -ok...]
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 参数后面跟的是command命令,它的终止是以;为结束标志的,所以这句命令后面的分号是不可缺少的,考虑到各个系统中分号会有不同的意义,所以前面加反斜杠。 {} 花括号代表前面find查找出来的文件名。 使用find时,只要把想要的操作写在一个文件里,就可以用exec来配合find查找,很方便的。在有些操作系统中只允许-exec选项执行诸如l s或ls -l这样的命令。大多数用户使用这一选项是为了查找旧文件并删除它们。建议在真正执行rm命令删除文件之前,最好先用ls命令看一下,确认它们是所要删除的文件。 exec选项后面跟随着所要执行的命令或脚本,然后是一对儿{ },一个空格和一个\,最后是一个分号。为了使用exec选项,必须要同时使用print选项。如果验证一下find命令,会发现该命令只输出从当前路径起的相对路径及文件名。
命令: 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选项的安全模式。它将在对每个匹配到的文件进行操作之前提示你。
命令: 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选项的安全模式。它将在对每个匹配到的文件进行操作之前提示你。
命令: 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键不删除。
命令: 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用户。
命令: 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]#
命令: 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]#