练习
1、查找/var目录下属主为root,且属组为mail的所有文件
[root@centos8 ~]# find /var/ -user root -group mail -ls
2、查找/var目录下不属于root、lp、gdm的所有文件
[root@centos8 ~]# find /var/ ! \( -user root -o -user lp -o -user gdm \) -ls
或
[root@centos8 ~]# find /var/ ! -user root ! -user lp ! -user gdm -ls
3、查找/var目录下近一周内其内容修改过,同时属主不为root,也不是postfix的文件
[root@centos8 ~]# find /var/ -mtime -7 ! \( -user root -o -user postfix \) -ls
或
[root@centos8 ~]# find /var/ -mtime -7 ! -user root ! -user postfix -ls
4、查找当前系统上没有属主或属组,且近一个周内曾被访问过的文件
[root@centos8 ~]# find / -nouser -o -nogroup -a -atime -7 -ls
5、查找/etc目录下大于1M且类型为普通文件的所有文件
[root@centos8 ~]# find /etc -size +1M -type f -ls
6、查找/etc目录下所有用户都没有写权限的文件
[root@centos8 ~]# find /etc ! -perm /222 -ls
7、查找/etc目录下至少有一类用户没有执行权限的文件
[root@centos8 ~]# find /etc ! -perm -111 -ls
8、查找/etc/init.d目录下,所有用户都有执行权限,且其它用户有写权限的文件
[root@centos8 ~]# find /etc/init.d -perm -113 -ls
实验一:多条件查找文件
【例1】查找/var目录下属主为root,且属组为mail的所有文件
[root@centos8 ~]# find /var -user root -group mail -ls
【例2】查找/var目录下不属于root、lp、gdm的所有文件
[root@centos8 ~]# find /var ! \( -user root -o -user lp -o -user gdm \) -ls
【例3】找出/home/var/log目录下删除所有以.log结尾的文件并无需人工确认删除
[root@centos8 ~]# find /home/var/log -name "*.log" -exec rm {} \;
【例4】找出/etc目录下所有以.conf结尾的文件
[root@centos8 ~]# find /etc -name "*.conf" -ls
【例5】将30天前的.log文件移动到old目录中
[root@centos8 ~]# find ./ -type f -name "*.log" -ctime +30 -exec mv {} old \;
【例6】查找/tmp目录下,u,g,o任何一类用户(至少有一类)中有读或写权限的文件;
[root@centos8 ~]# find /tmp -prem /666 -ls
【例7】查找/tmp目录下,权限为104的文件
[root@centos8 ~]# find /tmp -perm 104 -ls
【例8】查找/tmp目录下,任何一类用户的任何一位权限有执行或读权限的文件;
[root@centos8 ~]# find /tmp -perm /555 -ls
【例9】查找/tmp目录下,属主有执行权限同时其它有读权限的文件
[root@centos8 ~]# find /tmp -perm 104 -ls
实验二:压缩归档文件
【例1】把old整个目录打包压缩且文件名为bak.tar
[root@centos8 ~]# tar cvf bak.tar old
【例2】把httpd压缩归档文件,展开到/tmp目录下
[root@centos8 ~]# tar xvf httpd-2.4.43.tar.bz2 -C /tmp
【例3】使用gzip归档压缩,将test目录归档压缩到tmp目录下名为test.tar.gz
[root@centos8 ~]# tar zcvf /tmp/test.tar.gz /test
【例4】使用bzip2归档压缩,将test目录归档压缩到tmp目录下名为test.tar.bz2
[root@centos8 ~]# tar jcvf /tmp/test.tar.bz2 /test
【例5】使用xz归档压缩,将test目录归档压缩到tmp目录下名为test.tar.xz
[root@centos8 ~]# tar Jcvf /tmp/test.tar.xz /test
【例6】使用zip打包压缩,把testdir目录,打包压缩名称为testdir.zip
[root@centos8 ~]# zip -r testdir.zip /testdir
【例7】解压testdir.zip文件
[root@centos8 ~]# unzip testdir.zip