Linux学习之find使用

实现的工具有locate和find
1、locate工具——命令 - find files by name

1、locate工具依赖于事先构建好的索引库(或叫数据库和文化whatis一样)
		此索引库可由系统自动实现——周期性任务进行更新
		也可手动创建更新数据库——updatedb
2、工作特特性
	查找快速、依赖数据库查找、模糊查找、非实时查找
3、使用格式
	 locate [OPTION]... PATTERN...
4、option
	locate -b:只匹配路径中的基名
	例:
		[root@hu /]# locate -b "lib"
		/var/lib/alternatives/libwbclient.so.0.12-64
		/var/log/libvirt
	locate -c:统计出总有多少个符合条件的文件
	例:
		[root@hu /]# locate -c "lib"
		32013
	locate -r:BRE据此编写模式
	例:
		[root@hu ~]# locate -r "\<version\>"
		/etc/yum/version-groups.conf
		/home/hu/.cache/tracker/db-version.txt
	注意:索引构建过程中需要遍历整个根文件系统,及其消耗资源

2、find命令:- search for files in a directory hierarchy

find命令用于在文件系统上查找符合条件的文件而不是文件类容

1、工作特性
	实时查找工具,通过遍历指定起始路径下文件系统层级结构
	完成文件查找
	查找速度略慢
	精确查找
2、使用格式
	find [option]…[path][查找条件][处理动作]
	例:
		[root@hu ~]# find		——>若没加任何条件指查找当前路径下的所有文件路径
		.
		./Music
		./.bash_profile
		./.bash_logout
	path——查找条件:指定具体搜索目标的起始路径,默认为当前目录
	查找条件:指定查找条件——可以是文件名、大小、类型、从属关系、权限等——默认为找出指定路径下的所有文件
	处理动作:对符合查找条件的文件作出的操作——删除等——默认为输出至标准输出
3、查找条件
	表达式组成:表达式由选项(影响整体操作而不是特定文件的处理,并始终返回true),测试(返回true或false值)和操作(具有副作用并返回a)组成。
	A、据文件名查找文件
	
		-name “patern”:区分文件名大小写
		例:
			[root@hu recover]# find ./ -name "Test.sh"
			[root@hu recover]# find ./ -name "test.sh"
			./test.sh
			
		-iname “pattern”:不区分文件名大小写
		例:
			[root@hu recover]# find ./ -iname "Test.sh"
			./test.sh
			
		-regex pattern:基于正则表达式查找文件,匹配范围是整个路径,而非其名字,且不忽略大小写
		
		-iregex pattern:基于正则表达式查找文件,匹配范围是整个路径,而非其名字,忽略大小写
		find . -regex ".*\(\.txt\|\.pdf\)$"
		find . -iregex ".*\(\.txt\|\.pdf\)$"
		例:
			[root@hu recover]# find / -regex ".*\(\.txt\)$"	==>查找根目录下以.txt结尾的文件路径
			/root/.cache/tracker/db-locale.txt
			/root/.cache/tracker/first-index.txt
	B、根据文件从属关系查找
		-user username——>即查找属主指定用户的所有文件
		例:
			[root@hu recover]# find /home -user hu
			/home/hu
			/home/hu/Music
		-group Groupname——>即查找属组为指定组的所有文件
		-uid——>UID查找指定UID的属主的所有文件
		-gid——>GID查找指定GID的属组的所有文件
		-nouser——>查找没有属主的文件
		-nogroup——>查找没有属组的文件
	C、局文件类型查找
		-type f——>查找普通文件
		例:
			[root@hu recover]# find /home -type f -ls
			791770    4 -rw-r--r--   1 ur1      ur1           193 Nov 20  2015 /home/ur1/.bash_profile
		-type d——>查找目录文件
		-type l——>符号链接文件
		-type b——>块设备文件
		-type c——>字符设备文件
		-type p——>管道文件
		-type s——>套接字文件
4、组合测试
	A、与==> -a——>默认与组合逻辑,必须同时满足两个条件
	例:
		[root@hu recover]# find /tmp -nouser -type f
		[root@hu recover]# find /tmp -nouser -a -type f
	B、或==> -o——>或组合逻辑满足其中条件之一即可
	例:
		[root@hu recover]# find /tmp -nouser -o -type f
		/tmp/mytest.TyTf
		/tmp/.X0-lock
	C、非==> -not 或!——>非组合逻辑与此条件相反的条件
	例:
		[root@hu recover]# find . -not -nouser
		.
		./cptest
		./cptest/testcp
5、根据文件大小查找
	A、使用格式:find /path -size [+|-]#unit——#表示数值,unit为单位
	常用单位有:K、M、G
		a、当无 [+|-]号时;#unit表示范围为(#-1,#]<==>#-1<#≤#
		b、当有 [+|-]号时;
			- #unit表示范围为[0,#-1]<==>0≤#≤#-1
			+ #unit表示范围为(#,∞)<==>#<#≤∞
		例:
			[root@hu recover]# find . -size -10
			.
			./cptest
			./cptest/testcp
			./cptest/cp.txt
			./cptest/test.txt
			./test.patch
			./test1.sh
			./tt
			./test.sh
			./io
			./io/test1.txt
			./io/test.txt
			./cp.txt
			[root@hu recover]# find . -size 10
			[root@hu recover]# find . -size +10
			./.cp.txt.swp
			./.text.txt.swp
6、据时间戳查找
	A、以“天”为单位查找
		-atime [+|-]#
		-mtime [+|-]#
		-ctime [+|-]#
		#:[#,#-1)——#≤#<#-1
		+#:(#,0]——#<#≤0
		-#:(∞,#)——∞<#<#
		例:
			[root@hu recover]# find . -atime -7
			.
			./cptest
			./cptest/testcp
	B、以“分钟”为单位
		-amin [+|-]#
		-mmin [+|-]#
		-cmin [+|-]#
		[+|-]#同上
7、据权限查找
	-perm [/ | -]mode
	A、mode —精确查找
	例:
		[root@hu recover]# find . -perm 755 -ls
		261629    4 drwxr-xr-x   5 root     root         4096 Dec  1 04:53 .
		264277    4 drwxr-xr-x   2 root     root         4096 Nov 29 08:46 ./cptest
		261660    4 -rwxr-xr-x   1 root     root          205 Nov 30 09:18 ./test1.sh
		261653    4 drwxr-xr-x   2 root     root         4096 Dec  1 01:14 ./tt
	B、/mode —任何一类用户(u、g、o)的权限中的任何一位(r、w、x)符合权限既满足条件(权限之间为或关系)
	例:
		[root@hu recover]# find . -perm /755 -ls
		261629    4 drwxr-xr-x   5 root     root         4096 Dec  1 04:53 .
		264277    4 drwxr-xr-x   2 root     root         4096 Nov 29 08:46 ./cptest
		264280    0 -rw-r--r--   1 root     root            0 Nov 24 10:01 ./cptest/testcp
		261649    0 -rw-r--r--   1 root     root            0 Nov 29 08:46 ./cptest/cp.txt
		261652    4 -rw-r--r--   1 root     root           39 Nov 29 08:46 ./cptest/test.txt
		261633    4 -rw-r--r--   1 root     root           24 Nov 30 09:19 ./test.patch
		261630   12 -rw-r--r--   1 root     root        12288 Dec  1 05:12 ./.cp.txt.swp
		261660    4 -rwxr-xr-x   1 root     root          205 Nov 30 09:18 ./test1.sh
		261653    4 drwxr-xr-x   2 root     root         4096 Dec  1 01:14 ./tt
		261393    4 -rwxrwxr-x   1 root     root          192 Nov 30 09:42 ./test.sh
		261126    4 drwxr-xr--   2 root     root         4096 Nov 28 05:07 ./io
	C、-mode—每一类用户(u、g、o)的权限中的每一位(r、w、x)对应于条件的每一位相同既满足条件(权限之间为与关系)
	例:
		[root@hu recover]# find . -perm -755 -ls
		261629    4 drwxr-xr-x   5 root     root         4096 Dec  1 04:53 .
		264277    4 drwxr-xr-x   2 root     root         4096 Nov 29 08:46 ./cptest
		261660    4 -rwxr-xr-x   1 root     root          205 Nov 30 09:18 ./test1.sh
		261653    4 drwxr-xr-x   2 root     root         4096 Dec  1 01:14 ./tt
		261393    4 -rwxrwxr-x   1 root     root          192 Nov 30 09:42 ./test.sh
8、动作处理
	-print——输出至标准输出,默认的动作
	-ls——类似于对查找到的文件执行“ls -l”命令
	-delete——删除查找到的文件
	-fls /path——将查找到的所有文件的详细信息保存到指定路径的文件里
	-ok command {}\——对查找到的每个文件执行由command表示的命令,每次操作都由用户确认
									( {}\为固定格式)
	-exec command {}\——对查找到的每个文件执行由command表示的命令,每次操作不由用户确认
									( {}\为固定格式)
	例:
		[root@hu recover]# find / -nouser -a -nogroup -ok chown root:root {}\
		-rw-r--r--. 1 root root    0 Dec  1 13:20 ttt
		(chown:更改文件的属主和属组)
注意:find传递查找到的文件路径至后面的命令时,是先查找出所有符合条件的文件路径,并一次性传递给后面
	 的命令但是此命令不能接受过长的参数,若参数过长命令执行会失败,换另一种方式可规避此问题
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值