Linux系统上的软链接以及find命令

软链接

[root@centos ~]# echo "This is my1" > my1
[root@centos ~]# echo "This is my2" > my2
[root@centos ~]# echo "This is my3" > my3
[root@centos ~]# ls
anaconda-ks.cfg  cr  my1  my2  my3  test
[root@centos ~]# cd test
[root@centos test]# ls
[root@centos test]# ln -s ../my1 m1		#相对路径创建软链接
[root@centos test]# ln -s /root/my2 m2  #绝对路径创建软链接
[root@centos test]# ln -s my3 m3  	#此处创建的软链接是错误的
[root@centos test]# ll -a
total 4
drwxr-xr-x. 2 root root   33 Mar  7 07:15 .
dr-xr-x---. 3 root root 4096 Mar  7 07:14 ..
lrwxrwxrwx. 1 root root    6 Mar  7 07:15 m1 -> ../my1
lrwxrwxrwx. 1 root root    9 Mar  7 07:15 m2 -> /root/my2
lrwxrwxrwx. 1 root root    3 Mar  7 07:15 m3 -> my3
[root@centos test]# cat m1
This is my1
[root@centos test]# cat m2
This is my2
[root@centos test]# cat m3
cat: m3: No such file or directory

在这里插入图片描述

find命令操作

find命令的用法:

find [OPTION]… [查找路径] [查找条件] [处理动作]

查找路径 指定具体目标路径;默认为当前目录
处理动作 对符合条件的文件做操作,默认输出至屏幕
查找条件 指定的查找标准,可以文件名、大小、类型、权限等标准进行;默认为找出指定路径下的所有文件

我们在使用–help查看find命令都有哪些用法时仅仅依靠find --help命令显示出来的结果是不全面的,这时候我们就要使用man find命令来更多的显示用法,这样得出来的显示结果我们可以利用“/”后加我们要查询的相应选项来进行匹配我们要查看的那部分的内容解释。
在这里插入图片描述
在这里插入图片描述

指定搜索层级

-maxdepth 最大搜索目录深度
-mindepth 最小搜索目录深度

对应数据库上的语句就是:select * from stdio where maxdepth(>/<)深度

[root@centos test]# find / -maxdepth 3 -name cr
/root/cr
[root@centos test]# find / -maxdepth 4 -name cr
/root/cr
/usr/share/locale/cr
[root@centos test]# find / -mindepth 3 -name cr
/usr/share/locale/cr
[root@centos test]# find / -mindepth 2 -name cr
/root/cr
/usr/share/locale/cr

在这里插入图片描述

根据文件名和inode号来进行查找

-name “文件名称” 支持使用glob*, ?, [], [^]
-inum n 按inode号查找
-samefile name 相同inode号的文件
-links n 链接数为n的文件
-iname"文件名称" 不区分字母大小写
-regex “PATTERN” 以PATTERN匹配整个文件路径字符串,而不仅仅是文件名称

对应数据库上的语句就是:select * from stdio where inode=“number”
在这里插入图片描述

根据属组、属主查找

-user USERNAME 查找属主为指定用户(UID)的文件
-group GRPNAME 查找属组为指定组(GID)的文件
-uidUserID 查找属主为指定的UID号的文件
-gidGroupID 查找属组为指定的GID号的文件
-nouser 查找没有属主的文件
-nogroup 查找没有属组的文件

对应数据库上的语句就是:select * from stdio where user="username"

[first@centos test1]$ touch f{1..5}
[first@centos test1]$ ls
f1  f2  f3  f4  f5
[root@centos test1]# ls
f1  f2  f3  f4  f5
[root@centos test1]# touch f{6..10}
[root@centos test1]# ls
f1  f10  f2  f3  f4  f5  f6  f7  f8  f9
[root@centos test1]# find / -user first
/home/first/test1/f1
/home/first/test1/f2
/home/first/test1/f3
/home/first/test1/f4
/home/first/test1/f5

在这里插入图片描述

根据文件类型查找

-type TYPE:

f 普通文件
d 目录文件
l 符号链接文件
s 套接字文件
b 块设备文件
c 字符设备文件
p 管道文件
[root@centos ~]# find / -type f
[root@centos ~]# find / -type d

在这里插入图片描述在这里插入图片描述

组合条件

组合条件:

-a
-o
-not, !

德·摩根定律:
(非A) 或(非B) = 非(A 且B)
(非A) 且(非B) = 非(A 或B)
示例:
!A -a !B = !(A -o B)
!A -o !B = !(A -a B)

找出/tmp目录下,属主不是root,且文件名不以f开头的文件
	find /tmp\( -not -user root -a -not -name 'f*' \) -ls
	find /tmp-not \( -user root -o -name 'f*' \) –ls
排除目录
示例:查找/etc/下,除/etc/sane.d目录的其它所有.conf后缀的文件
	find /etc -path ‘/etc/sane.d’ -a -prune
	-o -name “*.conf”
	find /etc \(–path ‘/etc/sane.d’ –o –path ’/etc/fonts’ \)
	-a prune –o name “*.conf”

根据文件大小来查找

-size [+/-]#UNIT 常用单位:k, M, G,c(byte)
#UNIT: (#-1, #] 如:6k 表示(5k,6k]
-#UNIT:[0,#-1] 如:-6k 表示[0,5k]
+#UNIT:(#,∞) 如:+6k 表示(6k,∞)

对应数据库上的语句就是:select * from stdio where size(>/<)数值

[root@centos ~]# find /root -size -5k| ll -a
total 68
dr-xr-x---.  3 root root 4096 Mar  7 07:49 .
dr-xr-xr-x. 17 root root 4096 Mar  6 22:00 ..
-rw-------.  1 root root 1132 Mar  6 22:01 anaconda-ks.cfg
-rw-------.  1 root root 1402 Mar  7 02:05 .bash_history
-rw-r--r--.  1 root root   18 Dec 28  2013 .bash_logout
-rw-r--r--.  1 root root  176 Dec 28  2013 .bash_profile
-rw-r--r--.  1 root root  200 Mar  7 02:03 .bashrc
-rw-r--r--.  1 root root  280 Mar  7 01:42 cr
-rw-r--r--.  1 root root  100 Dec 28  2013 .cshrc
-rw-r--r--.  2 root root   12 Mar  7 07:14 mmy1
-rw-r--r--.  2 root root   12 Mar  7 07:14 my1
-rw-r--r--.  1 root root   12 Mar  7 07:14 my2
-rw-r--r--.  1 root root   12 Mar  7 07:14 my3
-rw-r--r--.  1 root root  129 Dec 28  2013 .tcshrc
drwxr-xr-x.  2 root root   33 Mar  7 07:15 test
-rw-------.  1 root root 4622 Mar  7 02:36 .viminfo
-rw-r--r--.  1 root root   69 Mar  7 01:30 .vimrc

在这里插入图片描述

根据时间戳

以“天”为单位;
	-atime[+|-]#,
	#: [#,#+1)
	+#: [#+1,∞]
	-#: [0,#)
	-mtime
	-ctime
以“分钟”为单位:
	-amin
	-mmin
	-cmin

在这里插入图片描述

根据权限查找

-perm [/|-] MODE

MODE 精确权限匹配
/MODE 任何一类(u,g,o)对象的权限中只要能一位匹配即可,或关系,+ 从centos7开始淘汰
-MODE 每一类对象都必须同时拥有指定权限,与关系
0 表示不关注

find -perm 755会匹配权限模式恰好是755的文件
只要当任意人有写权限时,find -perm +222就会匹配
只有当每个人都有写权限时,find -perm -222才会匹配
只有当其它人(other)有写权限时,find -perm -002才会匹配

在这里插入图片描述

处理动作

-print 默认的处理动作,显示至屏幕
-ls 类似于对查找到的文件执行“ls -l”命令
-delete 删除查找到的文件
-fls file 查找到的所有文件的长格式信息保存至指定文件中
-ok COMMAND {} ; 对查找到的每个文件执行由COMMAND指定的命令,对于每个文件执行命令之前,都会交互式要求用户确认
-exec COMMAND {} ; 对查找到的每个文件执行由COMMAND指定的命令;{}: 用于引用查找到的文件名称自身;find传递查找到的文件至后面指定的命令时,查找到所有符合条件的文件一次性传递给后面的命令

参数替换

由于很多命令不支持管道|来传递参数,而日常工作中有这个必要,所以就有了xargs命令
xargs用于产生某个命令的参数,xargs可以读入stdin的数据,并且以空格符或回车符将stdin的数据分隔成为arguments
注意:文件名或者是其他意义的名词内含有空格符的情况
有些命令不能接受过多参数,命令执行可能会失败,xargs可以解决
示例:

ls f* |xargs rm
find /sbin-perm +700 |ls -l 这个命令是错误的
find /sbin-perm +700 | xargs ls –l
find和xargs格式:find | xargs COMMAND

示例:

find -name  “*.conf” -exec cp {} {}.orig \;
备份配置文件,添加.orig这个扩展名
find /tmp –ctime +3 –user joe –ok rm {} \;
提示删除存在时间超过3天以上的joe的临时文件
find~-perm-002 -execchmodo-w{} \;
在你的主目录中寻找可被其它用户写入的文件
find /data –type f -perm 644 -name “*.sh” –exec chmod 755 {} \;
find /home –type d -ls

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章