运维基础-查找压缩

find能做什么?

1、实时的查找(不指定目录在当前目录下查找)

2、多条件尽可能精确地定位搜索对象(不指定条件列出目录中的所有文件)

3、find 可以连接命令对查找出的结果直接处理

1)、-exec or -ok
2)、{}匹配查询出的结果
3)、整条命令的结尾Space\;

find常见表达式

find   目录  条件  动作

-type 类型(f d l p c b)

[root@localhost ~]# find -type d
.
./.cache
./.cache/dconf
./.cache/abrt
./.cache/mozilla
./.cache/mozilla/firefox
./.cache/mozilla/firefox/tv48miwc.default
./.cache/mozilla/firefox/tv48miwc.default/cache2
./.cache/mozilla/firefox/tv48miwc.default/cache2/entries
./.cache/mozilla/firefox/tv48miwc.default/cache2/doomed
./.cache/mozilla/firefox/tv48miwc.default/thumbnails
./.cache/mozilla/firefox/tv48miwc.default/startupCache
./.cache/mozilla/firefox/tv48miwc.default/OfflineCache
./.cache/mozilla/firefox/tv48miwc.default/safebrowsing
./.dbus
./.dbus/session-bus
./.config
./.config/abrt

-name 名称(-iname 对文件名的大小写不敏感)

[root@localhost ~]# find /etc/ -name passwd
/etc/pam.d/passwd
/etc/passwd
[root@localhost ~]# 

-size 大小 +1M 大于1M, -1M 小于1M,1M 等于1M

[root@localhost ~]# find /etc/ -name passwd
/etc/pam.d/passwd
/etc/passwd
[root@localhost ~]# find /boot -size +1M
/boot/grub2/fonts/unicode.pf2
/boot/System.map-3.10.0-693.el7.x86_64
/boot/vmlinuz-3.10.0-693.el7.x86_64
/boot/initrd-plymouth.img
/boot/initramfs-0-rescue-17864a38d41d4b6e860d389f6c75531b.img
/boot/vmlinuz-0-rescue-17864a38d41d4b6e860d389f6c75531b
/boot/initramfs-3.10.0-693.el7.x86_64.img
/boot/initramfs-3.10.0-693.el7.x86_64kdump.img
[root@localhost ~]# 

-user 文件拥有者

[root@localhost ~]# find /home -user root
/home
/home/ateam-text
/home/ateam-text/newfile1
/home/ateam-text/newdir1
/home/ateam-text/newfile2
/home/ateam-text/newdir2
/home/ateam-text/newfile3
/home/ateam-text/newdir3
/home/ateam-text/newfile4
/home/ateam-text/newdir4
[root@localhost ~]# 

-group 文件属组

[root@localhost ~]# find /var/ -group mail
/var/spool/mail
/var/spool/mail/rpc
/var/spool/mail/zhang
/var/spool/mail/alex
/var/spool/mail/root
[root@localhost ~]# 

-perm权限

[root@localhost ~]# find -perm 755
./.cache/abrt
./.cache/mozilla/firefox/tv48miwc.default/startupCache
./.cache/mozilla/firefox/tv48miwc.default/safebrowsing
./.config
./.config/abrt
./.mozilla
./.mozilla/extensions
./.mozilla/extensions/{ec8030f7-c20a-464f-9b0e-13a3a9e97384}
./.mozilla/firefox/tv48miwc.default/storage
./.mozilla/firefox/tv48miwc.default/storage/permanent
./.mozilla/firefox/tv48miwc.default/storage/permanent/chrome
./.mozilla/firefox/tv48miwc.default/storage/permanent/chrome/idb
./.mozilla/firefox/tv48miwc.default/storage/permanent/chrome/idb/2918063365piupsah.files
./Desktop
[root@localhost ~]# 

-perm +222 anyone +代表(或)三组权限匹配其中之一

-perm -222 everyone -代表(与)三组权限同时匹配

-mtime +3 从当天向历史天数推算的第三天前

-atime -3 从当前向历史天数推算的前三天至当天这个段范围

-ctime 3 从当天向历史天数推算的第三天

-o 或

-not 非

-ls 详细信息

[root@localhost ~]# find /var/mail/ \( -user zhang -o -user root \) -ls
50333136    0 drwxrwxr-x   2 root     mail           54 8月 24 09:22 /var/mail/
51743355    0 -rw-rw----   1 zhang    mail            0 8月 20 19:23 /var/mail/zhang
51751716    8 -rw-------   1 root     mail         5485 8月 24 09:20 /var/mail/root
[root@localhost ~]# 

find找到后处理的动作actions

find /etc/ -perm -002 -exec chmod o-w {} \;
find /etc/ -name "*.conf" -ok cp {} {}.bak \;
find /tmp/ -size +100M -exec rm {} \;
find /tmp -user user1 -atime +3 -exec rm {} \;
find -not -perm +111 -name "*.sh" -exec chmod u+x {} \;

find结合xargs

用rm删除太多文件的时候,可能会得到一个错误信息:/bin/rm Argument list too long,用xargs去避免这个问题

[root@localhost ~]# find /tmp/ -name '*.txt' -print0 | xargs -0 rm -f
[root@localhost ~]# 

xargs  -0将\0作为定界符

查找所有的TXT文件,并压缩

[root@localhost ~]# find  . -name '*.txt' -print | xargs tar -acvf file.tar.gz
[root@localhost ~]# 

文件归档、

tar类Unix操作系统上的打包工具,可以将多个文件合并为一个文件,便于传输,备份

-c   --create 创建新的tar文件

-x    --extract, --get 解开tar文件

-t    --list列出tar文件中包含的文件的信息

-r    --append 附加新的文件到tar文件中

-f    --file[主机名:]文件名 指定要处理的文件名

-j    --bzip2调用bzip2执行压缩

-z    --gzip 调用gizip执行

创建

[root@localhost test]# tar -cvf home_backup.tar /home/zhang/
[root@localhost test]# 

查看

[root@localhost test]# tar -tf home_backup.tar ^C
[root@localhost test]# 文件太多,不展示

释放

[root@localhost test]# tar -xvf home_backup.tar 
[root@localhost test]# 

其他的压缩包操作类似,具体请百度。。

 

posted @ 2018-08-24 17:38  一石数字欠我15w!!!  阅读(278)  评论(0编辑  收藏  举报