红帽RHCE考试上午-RHCSA(RH200)
servera.example.com 任务
11. 查找文件
• 查找属于 harry 用户所属的目录,并拷贝到/root/findlist/目录下
实操演示过程:
[root@servera tmp]# mkdir /root/findlist
[root@servera ~]# find / -user harry -type d -exec cp -av {} /root/findlist/ \;
命令解析:
find:查找需要的内容
/:表示需要查找的范围
-user:表示匹配的用户规则
-type:表匹配的类型
d:表示匹配目录
-exec:表示查找匹配之后需要关联的动作
{}:表示前面find查到的内容形成的数组
\;:表示结束标志
验证
[root@servera ~]# cd /root/findlist/
[root@servera findlist]# ll
total 0
drwx------. 2 root root 62 May 2 02:14 harry
find语法:
find 路径 选项 动作
find 查询的路径 查询的条件 条件相关的参数 动作
路径:
不指定路径 默认查找当前目录的文件 在查找的时候是递归查找(目录及其子目录下查找)
动作:
对find找到的文件执行指定的操作,可以是复制、删除等
选项:
选项的作用:定义以名称、类型、大小、时间、权限、用户、组等各种条件来查找。
-name 文件名 //按文件名查找,
实例如下
[root@root ~]# find /etc -name host* 在/etc目录下查找名称以host开头的所有文件
[root@root ~]# find / -name *.conf 在/根目录下查找名称以.conf结尾的所有文件
-iname 文件名 //按文件名查找 不区分大小写
[root@root ~]# find /etc -iname HOST*
-size 大小[cwbkMG] //按文件大小查找
+3G 大于3G的文件
-3G 小于3G的文件
3G 等于3G的文件
[root@root ~]# find / -size +3G //在根目录下查找文件大于3G的文件
[root@root ~]# find /boot -size +10M //在/boot目录下查找文件大于10M的文件
多条件查找 逻辑符号表达式
expr1 -and expr2 逻辑与 -and == -a
expr1 -or expr2 逻辑或 -or == -o
!expr 取反
-not expr 不是
例:文件大小大于3G且小于4G的文件
[root@root ~]# find / -size +3G -a -size -4G
以时间戳 查找
-amin 访问时间 //min 分钟
-atime 访问时间 //time 天数
-cmin 改动时间
-ctime 改动时间
-mmin 修改时间
-mtime 修改时间
[root@root ~]# find / -atime 2
n 2 列出在2天之前的“一天之内”被访问过的文件
+n +2 列出2天之前被访问过的文件
-n -2 列出2天之内被访问过的文件
-perm 文件权限查找
[root@root ~]# find / -perm -4000 查找文件权限设置了suid的文件
[root@root ~]# find / -perm -2000 查找文件权限设置了sgid的文件
[root@root ~]# find / -perm -0755 查找权限等于0755的文件
-type 根据文件类型查找
d 目录(即文件夹)
f 普通文件,即word文档、txt文本文档、jpg图片、mp3音乐、avi视频。find /etc -type f
b 块设备文件,即硬盘、u盘、硬盘分区,例:find /dev -type b
c 字符设备文件,即终端tty,例:find /dev -type c
p 管道文件
l 符号链接(也称软链接),即快捷方式,例:find / -type l
s 套接字,ip+port。例:find / -type s
[root@root ~]# find /etc -type d 在/etc目录下查找类型为d目录的文件
[root@root ~]# find / -type c -a -user root 在/根目录下查找类型为c且属主为root的文件
以文件的属主或属组查找:
-gid 用户的gid //文件的所属组
-group 用户组名 //文件的所属组
-uid 用户的uid //文件的所有者
-user 用户的名字 //文件的所有者
-inum inode号 //查找指定inode号是多少的文件,inode是文件的索引号,
显示索引号 //ls -li /etc/hosts
[root@root ~]# find / -uid 1000 在根目录下查找uid(属主)为1000的文件
[root@root ~]# find / -inum 16777341
动作:对查找到的文件做额外的操作(可以cp复制、mv移动、rm删除、ls -l等)
[-exec | -ok 要操作的命令 {} \;] //非交互式 或 交互式
[ | xargs -i 动作 ] //非交互式的 -i支持使用{}
解释:{}代表用find命令找到的文件,\;是动作的结束符号
-exec 操作时无提示
-ok 操作是有提示
要操作的命令
ls -l {} \;
cp -rv {} /tmp/ \;
例:
在/etc目录下查找host*文件,将找到的文件用ls -ld查看属性
[root@root ~]# find /etc -name host* -exec ls -ld {} \;
-rw-r--r--. 1 root root 9 6月 7 2013 /etc/host.conf
-rw-r--r-- 1 root root 187 2月 1 18:36 /etc/hosts
-rw-r--r--. 1 root root 370 6月 7 2013 /etc/hosts.allow
-rw-r--r--. 1 root root 460 6月 7 2013 /etc/hosts.deny
-rw-r--r--. 1 root root 9003 11月 21 2015 /etc/selinux/targeted/modules/active/modules/hostname.pp
-rw-r--r-- 1 root root 5 2月 1 17:39 /etc/hostname
将找到的文件复制到/tmp目录中
[root@root ~]# find /etc -name host* -exec cp -rv {} /tmp/ \;
"/etc/host.conf" -> "/tmp/host.conf"
"/etc/hosts" -> "/tmp/hosts"
"/etc/hosts.allow" -> "/tmp/hosts.allow"
"/etc/hosts.deny" -> "/tmp/hosts.deny"
"/etc/selinux/targeted/modules/active/modules/hostname.pp" -> "/tmp/hostname.pp"
"/etc/hostname" -> "/tmp/hostname"