linux基础⑨-find命令

以名称查找

[root@lsy study]# find ./ -name "*eth0"		#查找当前目录下名称为  *eth0
[root@lsy study]# find ./ -iname "*eth0"		#i忽略大小写的查询方式

以大小查找

#查找/etc目录下文件大于5M,然后使用-ls参数以长格式显示(-ls和系统的ls不是一个命令)
[root@lsy study]# find /etc -size +5M -ls
#查找/etc目录下文件大于5M,使用系统的ls来以长格式显示
[root@lsy study]# find /etc -size +5M |xargs ls -lh

以类型查找


查找当前目录下类型是文件的,并忽略大小写的名称查找为 *-eth0,都以长格式显示
[root@lsy study]# find ./ -type f -iname "*-eth0" -ls
查找当前目录下类型是目录的,并忽略大小写的名称查找为 *-eth0,都以长格式显示
[root@lsy study]# find ./ -type d -iname "*-eth0" -ls

查找/bin下类型是链接文件的,忽略大小写查找名称为b*的,以长格式显示
[root@lsy study]# find /bin/ -type l -iname "b*" -ls
[root@lsy study]# find /dev/ -type b -ls
[root@lsy study]# find /dev/ -type c -ls
[root@lsy study]# find /etc/ -type f -size +3M -name "hw*"

以时间查找

+7,以当前时间为主,查找7天以前的内容
[root@lsy study]# find ./ -type f -name "file*" -mtime +7
[root@lsy study]# find ./ -type f -name "file*" -mtime +7 -delete

-7,查找最近7天的文件,会打印当天的文件
[root@lsy study]# find ./ -type f -name "file*" -mtime -7

找第7天文件
[root@lsy study]# find ./ -type f -mtime 7

以用户查找

#查找home目录下,类型是目录的并且属主是jack的,同时只查找一层
[root@lsy study]# find /home/ -maxdepth 1  -type d -user jack

#查找home目录下,类型是目录的并且属组是hr的,同时只查找一层
[root@lsy study]# find /home/ -maxdepth 1  -type d -group hr -ls

#查找home目录下,类型是目录的并且属主是jack属组是hr的,同时只查找一层
[root@lsy study]# find /home/ -maxdepth 1 -type d -user jack -group hr -ls

#查找home目录下,类型是目录的要么属主是jack,要么属组是hr
[root@lsy study]# find /home/ -maxdepth 1 -type d -user jack -o -group hr|xargs ls -ld

#查找home目录下,类型是目录没有属主的
[root@lsy study]# find /home/ -maxdepth 1 -type d -nouser -ls
#查找home目录下,类型是目录没有属组的
[root@lsy study]# find /home/ -maxdepth 1 -type d -nogroup -ls

#查找home目录下,类型是目录没有属主或没有属组
[root@lsy study]# find /home/ -maxdepth 1 -type d -nouser -o  -nogroup |xargs ls -ld

以权限查找

精确查找文件的权限为644
[root@lsy study]# find ./ -type f -perm 644
#包含444权限即可   即大于等于444
[root@lsy study]# find ./ -type f -name "file*" -perm -444
#查找全局可写(每位权限必须包含w)
[root@lsy study]# find . -perm -222 -ls
#包含set uid
[root@lsy study]# find  /usr/sbin -perm -4000 -ls
#包含set gid
[root@lsy study]# find  /usr/sbin -perm -2000 -ls
#包含sticky
[root@lsy study]# find  /usr/sbin -perm -1000 -ls

包含动作

-delte,只能删除文件,如果要删除目录,需要保证目录为空,否则无法删除
	[root@lsy study]# find ./log/ -type f -name "*.log" -delete

-ok,可以执行任何自定义命令,但是会提示是否确认.
	[root@lsy study]# find /etc/ -name "ifcfg*" -ok cp -vp {} /tmp \;

-exec
	[root@lsy study]# find /etc/ -name "ifcfg*" -exec cp -vp {} /tmp \;
	[root@lsy study]# find log/ -type d -exec cp -rpv {} /tmp \;
	[root@lsy study]# find test/ -type f -exec rm -f {} \;

更多例子


1.查找/tmp目录下,属主不是root,且文件名不以c开头的文件
[root@lsy study]# find /tmp/ ! -user root  ! -name "c*"
/tmp/ttt

2.查找/var目录下属主为root,且属组为mail的所有文件
[root@lsy study]# find /var/ -type f  -user root -a -group mail -ls

3.查找/var目录下不属于root、lp、gdm组  的所有文件
[root@lsy study]# find /var/ -type f  ! \( -group root -o  -group lp -o  -group gdm \) |xargs ls -ld

4.查找/var目录下最近一周修改过的文件,同时属主不为root,也不是postfix的文件
[root@lsy study]# find /var/ -type f -mtime -7  ! -user root ! -name "postfix"

5.查找/etc目录下大于1M且类型为普通文件的所有文件
[root@lsy study]# find /etc/ -type f -size +1M

6.将/etc/中的所有目录(仅目录)复制到/tmp下,目录结构不变
[root@lsy study]# find /etc/ -type d -exec mkdir -p /tmp/{} \;

7.将/etc目录复制到/var/tmp/,/var/tmp/etc的所有目录权限777/var/tmp/etc目录中所有文件权限666
[root@lsy study]# cp -rp /etc/ /var/tmp/
[root@lsy study]# find /var/tmp/etc/ -type d -exec chmod 777 {} \;
[[root@lsy study]# find /var/tmp/etc/ -type f -exec chmod 666 {} \;
[root@lsy study]# find /var/tmp/etc/ ! -type d -exec chmod 666 {} \;

8.保留/var/log/下最近7天的日志文件,其他全部删除
[root@lsy study]# find /var/log/ -type f -mtime +7 -delete

9.创建touch file{1..10}10个文件, 保留file9,其他一次全部删除
[root@lsy study]# find ./ -type f ! -name "file9" -delete

10.
mkdir /root/dir1
touch /root/dir1/file{1..10}
find /root/dir1 -type f -name "file5"
find /root/dir1 ! -name "file5"
find /root/dir1 -name "file5" -o -name "file9"
find /root/dir1 -name "file5" -o -name "file9" -ls
find /root/dir1 \( -name "file5" -o -name "file9" \) -ls
find /root/dir1 \( -name "file5" -o -name "file9" \) -exec rm -rvf {} \;
find /root/dir1  ! \( -name "file4" -o -name "file8" \) -exec rm -vf {}  \; 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章