Linux学习之路-locate、find、xargs、压缩工具、tar【4】---20171203

locate

  • 非实时查找,效率非常高

    查询系统上预建的文件索引数据库

    /var/lib/mlocate/mlocate.db ----->查找的数据库
    ``[root@Centos6app]#ll /var/lib/mlocate/mlocate.db -h
    -rw-r-----. 1 root slocate 3.0M 12月 1 03:20 /var/lib/mlocate/mlocate.db

    依赖于事先构建的索引

    索引的构建是在系统较为空闲时自动进行(周期性任务),管理员手动更新数据库(updatedb) br/>[root@Centos6app]#updatedb
    [root@Centos6app]#ll /var/lib/mlocate/mlocate.db -h
    -rw-r-----. 1 root slocate 3.0M 12月 1 18:13 /var/lib/mlocate/mlocate.db

    索引构建过程需要遍历整个根文件系统,极消耗资源
    工作特点:

    •查找速度快
    •模糊查找
    •非实时查找
    •搜索的是文件的全路径,不仅仅是文件名
    •可能只搜索用户具备读取和执行权限的目录

  • locate KEYWORD

    有用的选项

    -i 不区分大小写的搜索
    -n N 只列举前N个匹配项目
    -r 使用正则表达式

搜索名称或路径中带有“conf”的文件

locate conf

使用Regex来搜索以“.conf”结尾的文件

locate -r ‘.conf$’

find 命令

实时查找工具,通过遍历指定路径完成文件查找
工作特点:
• 查找速度略慢
• 精确查找
• 实时查找
• 可能只搜索用户具备读取和执行权限的目录

语法:
find [ OPTION ] ... [查找路径] [查找条件] [处理动作]
查找路径:指定具体目标路径;默认为当前目录、也默认为递归查找
查找条件:指定的查找标准,可以是文件名、大小、类型、权限等,默认为找出指定路径下的所有文件。
处理动作:对符合条件的文件做操作,默认输出至屏幕

  • 搜索层级

-maxdepth level 最大搜索目录深度,指定目录为第1级
-mindepth level 最小搜索目录深度

[root@Centos6/]#find /etc -name "network"
/etc/sysconfig/network
/etc/rc.d/init.d/network
[root@Centos6/]#find /etc -maxdepth 2 -name "network"
/etc/sysconfig/network
  • 根据文件名和inode查找

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

[root@Centos6/]#find / -inum 2 -ls         -----------> 按inode 号查找 
     2    4 dr-xr-xr-x  26 root     root         4096 12月  1 01:22 /
     2    0 drwxr-xr-x   4 root     root            0 12月  1 01:23 /sys/fs
     2    0 -rw-r--r--     1 root     root            0 12月  1 01:22 /proc/sys/fs/binfmt_misc/status
find: “/proc/7694/task/7694/fd/5”: 没有那个文件或目录
find: “/proc/7694/task/7694/fdinfo/5”: 没有那个文件或目录
find: “/proc/7694/fd/5”: 没有那个文件或目录
find: “/proc/7694/fdinfo/5”: 没有那个文件或目录
     2    0 c---------    1 root      root              12月  1  2017 /dev/pts/ptmx
     2    4 dr-xr-xr-x    5 root     root          4096 11月 28 22:02 /boot
     2    4 drwxr-xr-x   3 root     root         4096 11月 28 21:51 /app
[root@Centos6app]#mkdir d1
[root@Centos6app]#touch a
[root@Centos6app]#ln a d1/aa
[root@Centos6app]#ll
总用量 4
-rw-r--r--.   2 root root    0    12月  1 06:37 a
drwxr-xr-x. 2 root root 4096 12月  1 06:37 d1
[root@Centos6app]#find -samefile a         ---------> 相同inode号的文件
./d1/aa
./a
[root@Centos6app]# find -links 2              ---------> **搜索链接数为2的文件**
./d1
./d1/aa
./a
[root@Centos6app]#find /etc -regex ".*\.txt$"   ----------> 查找/etc下以.txt结尾的文件
/etc/pki/nssdb/pkcs11.txt

排除某个目录查询其他目录下的指定文件
查找/etc/下,除/etc/sane.d目录的其它所有.conf后缀的文件
find /etc -path ‘/etc/sane.d’ -a -prune -o -name “*.conf”
查找/etc/下,除/etc/sane.d和/etc/fonts两个目录的其它所有.conf后缀的文件 (可排除多个)
find /etc (–path ‘/etc/sane.d’ –o –path ’/etc/fonts’ )

[root@Centos6app]#find /etc -path '/etc/sane.d' -a -prune -o -name "*.conf"
/etc/pm-utils-hd-apm-restore.conf
......
/etc/sane.d                               ------> 除了sane.d的目录,搜索其他目录.conf结尾的文件
......
/etc/abrt/plugins/python.conf
  • 根据属主、属组查找

-user USERNAME : 查找属主为指定用户(UID)的文件
-group GRPNAME : 查找属组为指定组(GID)的文件
-uid UserID : 查找属主为指定的UID号的文件
-gid GroupID : 查找属组为指定的GID号的文件
-nouser : 查找没有属主的文件
-nogroup : 查找没有属组的文件

  • 根据文件类型查找

-type TYPE :
f :普通文件
d :目录文件
l :符号链接文件
s : 套接字文件
b : 块设备文件
c : 字符设备文件
p : 管道文件

  • 组合条件

与 : -a
或 : -o
非 : -not,!

  • 德 摩根定律:

(非A)或(非B)= 非( A 且 B )
(非A)且(非B)= 非( A 或 B )
例如:!A -a !B = !( A -o B) 、!A -o !B = !(A -a B)

  • 根据文件大小来查找

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

查询 / 下大于50M小于100M的文件

[root@Centos6app]#find / -size +50M -size -101M 
  • 根据时间戳:

    以“天”为单位;
    -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传递查找到的文件至后面指定的命令时,查找到所有符合条件的文件一次性传递给后面的命令

[root@Centos6app]#find -name "*.conf" -ok cp {} /app/d1/{}.bak \;
< cp ... ./f2.conf > ? y
< cp ... ./f1.conf > ? y
[root@Centos6app]#cd d1
[root@Centos6d1]#ls
aa  f1.conf.bak  f2.conf.bak

参数替换xargs

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

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

  • 练习

1、查找/var目录下属主为root,且属组为mail的所有文件

[root@Centos6~]#find /var -user root -group mail
/var/spool/mail

2、查找/var目录下不属于root、lp、gdm的所有文件

[root@Centos6~]#find /var -not  \( -user root -o -user lp -o -user gdm \)

3、查找/var目录下最近一周内其内容修改过,同时属主不为root,也不是postfix的文件

[root@Centos6~]#find /var -mtime -8  -not \( -user root -o -user postfix \)

4、查找当前系统上没有属主或属组,且最近一个周内曾被访问过的文件

find / -atime -8 -nouser -nogroup 

5、查找/etc目录下大于1M且类型为普通文件的所有文件

[root@Centos6~]#find /etc -size +1M -type f
/etc/selinux/targeted/policy/policy.24
/etc/selinux/targeted/modules/active/policy.kern
/etc/gconf/gconf.xml.defaults/%gconf-tree.xml

6、查找/etc目录下所有用户都没有写权限的文件

[root@Centos6~]#find /etc ! -perm /222 

7、查找/etc目录下至少有一类用户没有执行权限的文件

[root@Centos6~]#find /etc ! -perm -111 | wc -l

8、查找/etc/init.d目录下,所有用户都有执行权限,且其它用户有写权限的文件

[root@Centos6~]#find /etc/init.d -perm -111 -perm -002
/etc/init.d

压缩工具

  • compress 命令:最老的压缩工具

compress [-dfvcVr] [-b maxbits] [file ...]

-d: 解压缩,相当于uncompress
-c: 结果输出至标准输出,不删除原文件
-v: 显示详情

uncompress 解压缩
zcat file.Z >file

  • gzip/gunzip 命令

gzip [OPTION]... FILE ...

-d: 解压缩,相当于gunzip
-c: 将压缩或解压缩的结果输出至标准输出
-#:1-9,指定压缩比,值越大压缩比越大

zcat:不显式解压缩的前提下查看文本文件内容
实例:

gzip -c messages >messages.gz
gzip -c -d messages.gz > messages
zcat messages.gz > messages

  • bzip2/bunzip2/bzcat

bzip2 [OPTION]... FILE ...

-k: keep, 保留原文件
-d:解压缩
-#:1-9,压缩比,默认为9

bzcat:不显式解压缩的前提下查看文本文件内容

[root@Centos6app]#bzip2 f1
[root@Centos6app]#ls
f1.bz2  f1.gz
[root@Centos6app]#ll -h
总用量 264K
-rw-r--r--. 1 root root 1.4M 12月  1 16:08 f1
-rw-r--r--. 1 root root  74K 12月  1 16:08 f1.bz2
-rw-r--r--. 1 root root 186K 12月  1 15:43 f1.gz
  • xz/unxz/xzcat (比较新的压缩工具)

xz [OPTION]... FILE ...

-k: keep, 保留原文件
-d:解压缩
-#:1-9,压缩比,默认为6

xzcat: 不显式解压缩的前提下查看文本文件内容

[root@Centos6app]#xz -k f1
[root@Centos6app]#ll -h -S
总用量 1.7M
-rw-r--r--. 1 root root 1.4M 12月  1 16:08 f1
-rw-r--r--. 1 root root 186K 12月  1 15:43 f1.gz
-rw-r--r--. 1 root root  74K 12月  1 16:08 f1.bz2
-rw-r--r--. 1 root root  48K 12月  1 16:08 f1.xz
  • zip/unzip (可以压缩文件夹)

    打包压缩 zip –r /testdir/sysconfig /etc/sysconfig/
    解包解压缩

    unzip sysconfig.zip
    cat /var/log/messages | zip messages -
    unzip -p message > message

[root@Centos6app]#zip -r sysconfig /etc/sysconfig
  adding: etc/sysconfig/ (stored 0%)
  adding: etc/sysconfig/irqbalance (deflated 45%)
    .......
  adding: etc/sysconfig/udev (deflated 17%)
[root@Centos6app]#ll 
总用量 1764
-rw-r--r--. 1 root root 1387116 12月  1 16:08 f1
-rw-r--r--. 1 root root   75491 12月  1 16:08 f1.bz2
-rw-r--r--. 1 root root  190403 12月  1 15:43 f1.gz
-rw-r--r--. 1 root root   49044 12月  1 16:08 f1.xz
-rw-r--r--. 1 root root   95201 12月  1 16:27 sysconfig.zip
注意:
1、解压缩时要注意文件后缀,解压缩命令使用必须带后缀。
2、压缩工具容易丢失一些信息,比如acl权限等。

tar工具(Tape ARchive,磁带归档的缩写)

tar [OPTION]...
-c 把一个目录或几个指定的文件打包到指定的文件中,-c就是创建这个文件
-f -f后面表示打包到哪个文件中
-v 查看打包过程
-t 查看归档及归档压缩文件的文件列表
-r 追加新的文件至归档(但不支持对压缩文件追加)
-x 解压缩归档及压缩文件,默认为当前目录下
-C 如果想解压缩到特定的目录下,使用-C选项

(1) 创建归档 tar -c -f /PATH/TO/SOMEFILE.tar FILE...
(2) 追加文件至归档:

注:不支持对压缩文件追加
tar -r -f /PATH/TO/SOMEFILE.tar FILE...

(3) 查看归档文件中的文件列表

tar -t -f /PATH/TO/SOMEFILE.tar

(4) 展开归档

tar -x -f /PATH/TO/SOMEFILE.tar
tar -x -f /PATH/TO/SOMEFILE.tar -C /PATH/

(5) 结合压缩工具实现:归档并压缩

-j: bzip2,
-z: gzip,
-J: xz

[root@Centos6app]#du -sh /etc/sysconfig/       ----------> du命令查看目录大小
476K    /etc/sysconfig/
[root@Centos6app]#tar -cvf /app/sysconfig.tar /etc/sysconfig
tar: 从成员名中删除开头的“/”
/etc/sysconfig/
/etc/sysconfig/irqbalance
/etc/sysconfig/htcacheclean
......
[root@Centos6app]#ll
-rw-r--r--. 1 root root 276480 12月  1 16:49 sysconfig.tar
[root@Centos6app]#tar -zcvf /app/sysconfig.tar.gz /etc/sysconfig
tar: 从成员名中删除开头的“/”
/etc/sysconfig/
/etc/sysconfig/irqbalance
........
[root@Centos6app]#ll -h
-rw-r--r--. 1 root root 270K 12月  1 16:49 sysconfig.tar
-rw-r--r--. 1 root root  56K 12月  1 17:21 sysconfig.tar.gz

-T选项指定输入文件
-X选项指定包含要排除的文件列表

tar zcvf mybackup.tgz -T /root/includefilelist -X /root/excludefilelist

[root@Centos6app]#vim filelist.txt
[root@Centos6app]#ll
-rw-r--r--. 1 root root     45 12月  1 17:33 filelist.txt
[root@Centos6app]#tar -T filelist.txt -Jcvf a.tar.xz 
tar: 从成员名中删除开头的“/”                            -------> 避免后期解压后覆盖原始文件
/etc/issue
/etc/passwd
/root/anaconda-ks.cfg
[root@Centos6app]#tar tvf a.tar.xz 
-rw-r--r-- root/root        47 2017-03-28 18:25 etc/issue
-rw-r--r-- root/root      1606 2017-11-28 22:15 etc/passwd
-rw------- root/root      1536 2017-11-28 22:02 root/anaconda-ks.cfg
  • split 命令

    分割大的 tar 文件为多份小文件:

    split –b Size(要切割的大小) –d tar-file-name(要切割的文件) prefix-name(切割后文件前缀)
    split -b 1M –d mybackup.tgz mybackup-parts
    split -b 1M mybackup.tgz mybackup-parts

    合并: cat mybackup-parts* > mybackup.tar.gz

[root@Centos6app]#tar cvf etc.tar /etc/
tar: 从成员名中删除开头的“/”
/etc/
[root@Centos6app]#ll -h
-rw-r--r--. 1 root root  37M 12月  1 17:40 etc.tar
[root@Centos6app]#split -b 5M etc.tar etc.tar             -----> 默认切割为字母后缀
[root@Centos6app]#ll -h
-rw-r--r--. 1 root root  37M 12月  1 17:40 etc.tar
-rw-r--r--. 1 root root 5.0M 12月  1 17:43 etc.taraa
-rw-r--r--. 1 root root 5.0M 12月  1 17:43 etc.tarab
-rw-r--r--. 1 root root 5.0M 12月  1 17:43 etc.tarac
-rw-r--r--. 1 root root 5.0M 12月  1 17:43 etc.tarad
-rw-r--r--. 1 root root 5.0M 12月  1 17:43 etc.tarae
-rw-r--r--. 1 root root 5.0M 12月  1 17:43 etc.taraf
-rw-r--r--. 1 root root 5.0M 12月  1 17:43 etc.tarag
-rw-r--r--. 1 root root 1.9M 12月  1 17:43 etc.tarah
[root@Centos6app]#cat etc.tara* > etc2.tar              ----------->合并文件
[root@Centos6app]#ll -h
-rw-r--r--. 1 root root  37M 12月  1 17:46 etc2.tar
-rw-r--r--. 1 root root  37M 12月  1 17:40 etc.tar
[root@Centos6app]#split -b 10M -d etc.tar etc.tar       ---------> -d以数字为后缀
[root@Centos6app]#ll -h
-rw-r--r--. 1 root root  10M 12月  1 17:47 etc.tar00
-rw-r--r--. 1 root root  10M 12月  1 17:47 etc.tar01
-rw-r--r--. 1 root root  10M 12月  1 17:47 etc.tar02
-rw-r--r--. 1 root root 6.9M 12月  1 17:47 etc.tar03
  • cpio

功能:复制文件从或到归档
cpio命令是通过重定向的方式将文件进行打包备份,还原恢复的工具,它可以解压以“.cpio”或者“.tar”结尾的文件
cpio [选项] > 文件名或者设备名
cpio [选项] < 文件名或者设备名
选项

-o 将文件拷贝打包成文件或者将文件输出到设备上
-i 解包,将打包文件解压或将设备上的备份还原到系统
-t 预览,查看文件内容或者输出到设备上的文件内容
-v 显示打包过程中的文件名称。
-d 解包生成目录,在cpio还原时,自动的建立目录
-c 一种较新的存储方式

例如:

将etc目录备份: find ./etc -print |cpio -ov >etc.cpio
内容预览 cpio –tv < etc.cpio
解包文件 cpio –idv < etc.cpio ------> 支持选定解压

[root@Centos6app]#find | cpio -ov > etc.cpio    ----->打包文件,cpio支持管道
.
./etc
./etc/purple
.......
[root@Centos6app]#ll -h
-rw-r--r--.   1 root root 71M 12月  1 17:53 etc.cpio
[root@Centos6app]#cpio -tv < etc.cpio             -------> 预览文件列表
drwxr-xr-x   3 root     root            0 Dec  1 17:53 .
drwxr-xr-x 120 root     root            0 Dec  1 17:52 etc    ----->默认删除\ ,以免覆盖原始文件
drwxr-xr-x   2 root     root            0 Dec  1 17:52 etc/purple
-rw-r--r--   1 root     root         1950 Dec  1 17:52 etc/purple/prefs.xml
.......

个人总结内容杂乱,总结信息为个人理解及老师课件内容,请勿见怪!!

转载于:https://blog.51cto.com/exia00linux/2047007

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值