文件查找与打包压缩

 

目录

 

一.文件查找find

1.按文件名查找  -name

2.按文件大小   -size

3.按照时间查找

4.根据深度查找        -maxdepth   n

5.根据文件属主,属组查找

6.按文件类型查找   -type        

7.进一步处理搜索结果     -exec.....{} \;

8.find使用xargs

9.取反 !

二.命令查找

1.which

2.whereis

三.grep文件过滤

四.打包压缩

①.gz格式

②.bz2格式

③.xz格式

④zip格式


一.文件查找find

        # find        路径        条件        跟条件相关的操作符        [-exec  动作]

        路径: 1.默认不写就是当前路径

                    2.指定路径

        条件: 1.指定名称        -name

                    2.文件类型        -type

                    3.权限                -perm

                    4.时间                 -atime

                                                -mtime

                                                -ctime

1.按文件名查找  -name


[root@myhost1 ~]# find / -name "file2"        #从根开始查找
/tmp/file2
/usr/local/php-7.3.6/ext/phar/tests/bug54289/in/dirAB/file2

[root@myhost1 ~]# find /etc -name "ifcfg-ens33"    # 以名字的方式查找
/etc/sysconfig/network-scripts/ifcfg-ens33
[root@qfedu.com ~]# find /etc -iname "Ifcfg-ens33"         #-i忽略大小写

熟用通配符

查找/etc/目录下以 .txt 结尾的文件
[root@qfedu.com ~]# find /etc -iname "*.txt"
参数解释:
*:表示所有字符

2.按文件大小   -size

[root@qfedu.com ~]# find /etc -size +5M        #大于5M
[root@qfedu.com ~]# find /etc -size 5M          #等于5M
[root@qfedu.com ~]# find /etc -size -5M         #小于5M
[root@qfedu.com ~]# find / -size +3M -a -size -5M  #查找/下面大于3M而且小于5M的文件
        -a:and
[root@qfedu.com ~]# find / -size -1M -o -size +80M #查找/下面小于1M或者大于80M的文件
        -o:or
[root@qfedu.com ~]# find / -size -3M -a -name "*.txt" #查找/ 下面小于3M而且名字是.txt的文件

3.按照时间查找

按时间找(atime,mtime,ctime)
-atime = access访问时间
-mtime = modify改变时间  内容修改时间会改变
-ctime = change修改时间   属性修改时间会改变

-amin  #分钟
-mmin
-cmin

[root@qfedu.com ~]# find    /opt    -mtime    +5        #修改时间5天之前
[root@qfedu.com ~]# find    /opt    -atime     +1     #访问时间1天之前
[root@qfedu.com ~]# find .  -mtime  -2        #修改时间2天之内

[root@qfedu.com ~]# find .  -amin  +1         #访问时间在1分钟之前
[root@qfedu.com ~]# find    /opt    -amin   -4      #访问时间在4分钟之内
[root@qfedu.com ~]# find    /opt    -mmin   -2      #修改时间在2分钟之内

4.根据深度查找        -maxdepth   n

[root@myhost1 ~]# find / -maxdepth  4 -a -name "ifcfg-en*"
/etc/sysconfig/network-scripts/ifcfg-ens33

5.根据文件属主,属组查找

[root@myhost1 home]# find  /home   -user  aren                #查找属主为aren的文件
/home/aren
/home/aren/.bash_logout

[root@myhost1 home]# find  /home   -group   tom                #查找属组为tom的文件
/home/tom
/home/tom/.bash_logout
/home/tom/.bash_profile

6.按文件类型查找   -type        

[root@myhost1 home]# find  /home   -type  d                #-d目录
/home
/home/aren

[root@myhost1 home]# find  /home   -type  f                #-f普通文件
/home/aren/.bash_logout
/home/aren/.bash_profile

7.进一步处理搜索结果     -exec.....{} \;

#查找出/tmp下以.txt结尾的文件并复制到当前目录下
[root@testhost ~]# find /tmp -name '*.txt' -exec cp {}  . \;

8.find使用xargs

[root@testhost opt]# touch /home/test{1..20}.txt
[root@testhost opt]# find /home/ -name "test*" | xargs -i cp {} /tmp/

-exec:参数是一个一个传递的,传递一个参数执行一次命令。
xargs:将前一个命令的标准输出传递给下一个命令,作为它的参数转换成下一个命令的参数列表。
===============
1、exec 每处理一个文件或者目录,它都需要启动一次命令,效率不好; 
2、exec 格式麻烦,必须用 {} 做文件的代位符,必须用 \来转义; 作为命令的结束符,书写不便。 
3、xargs不能操作文件名有空格的文件;

综上,如果要使用的命令支持一次处理多个文件,并且也知道这些文件里没有带空格的文件,那么使用 xargs比较方便; 否则,就要用 exec了。

9.取反 !

[root@testhost opt]# find /home  -name *test5*
/home/test5.txt
[root@testhost opt]# find /home ! -name 'test5*'

二.命令查找

1.which

[root@testhost ~]# which ls
alias ls='ls --color=auto'
        /usr/bin/ls

[root@testhost ~]# which  rpm
/usr/bin/rpm

2.whereis

[root@testhost ~]# whereis cat
cat: /usr/bin/cat /usr/share/man/man1/cat.1.gz

[root@testhost ~]# whereis  yum
yum: /usr/bin/yum /etc/yum /etc/yum.conf /usr/share/man/man8/yum.8

三.grep文件过滤

[root@testhost ~]# grep  'root'  /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

四.打包压缩

tar [选项...] [FILE]...

   

        c                        创建压缩包
        v                        显示执行过程        
        f                        指定文件
        z                        以gzip格式压缩
        j                        以bzip2格式压缩
        J                        以xz格式压缩
        x                        解压缩、提取包中的内容

打包到指定路径:

[root@testhost opt]# tar czf /tmp/`date +%F-%T`-etc.tar.gz /etc/
#将打包的文件放到/tmp目录下,并以当前时间开头命名

①.gz格式

方式一:

打包并压缩: tar -zcvf [目标文件名].tar.gz [原文件名/目录名]
解压并解包: tar -zxvf [原文件名].tar.gz
注:z代表用gzip算法来压缩/解压。

[root@testhost opt]# tar  czvf  etc.tar.gz  /etc   #打包并以gzip格式压缩
[root@testhost opt]# ll
总用量 10240
-rw-r--r-- 1 root root 10481665 9月   7 17:10 etc.tar.gz
[root@testhost opt]# tar xvf etc.tar.gz     #解压到当前目录

方式二:

利用前面已经打包好的tar文件,直接用压缩命令。

压缩:gzip [原文件名]

           gzip -c [源文件] > /home/[源文件].gz
解压:gunzip [原文件名].gz

           gzip -d  [原文件名].gz

[root@testhost opt]# gzip file.tar      #压缩
[root@testhost opt]# gzip -c file1 > /home/file1.gz  
#以gzip压缩文件并指定到/home下


[root@testhost opt]# gzip -d file.tar.gz    #解压
或
[root@testhost opt]# gunzip file.tar.gz    #解压

②.bz2格式

方式一:利用已经打包好的tar文件,直接执行压缩命令:

压缩:bzip2 [原文件名]
解压:bunzip2 [原文件名].bz2
            bzip2   -d   [原文件名].bz2

[root@testhost opt]# bzip2 file   
[root@testhost opt]# bunzip2 file.bz2 

方式二:一次性打包并压缩、解压并解包

打包并压缩: tar -cjvf [目标文件名].tar.bz2 [原文件名/目录名]
解压并解包: tar -xjvf [原文件名].tar.bz2
注:小写j代表用bzip2算法来压缩/解压。

        -C 指定解压目录

[root@testhost opt]# tar  cjvf  message.tar.bz  /var/log/messages

[root@testhost opt]# tar xjvf message.tar.bz  -C  /tmp    #解压到/tmp下

③.xz格式

方式一:利用已经打包好的tar文件,直接用压缩命令:

压缩:xz [原文件名]
解压:unxz [原文件名].xz

           xz -d   [原文件名].xz

[root@testhost opt]# xz file #以xz格式压缩
[root@testhost opt]# unxz file.xz     #解压xz格式的压缩包

方式二:一次性打包并压缩、解压并解包

打包并压缩: tar -Jcvf [目标文件名].tar.xz [原文件名/目录名]
解压并解包: tar -Jxvf [原文件名].tar.xz
注:大写J代表用xz算法来压缩/解压。

[root@testhost opt]# tar cJvf  etc.tar.xz   /etc
[root@testhost opt]# tar xJvf etc.tar.xz 

④zip格式

压缩:zip -r -q   [目标文件].zip    [目标文件]

解压:unzip      [目标文件].zip   -d    [目标路径]

[root@testhost opt]# zip -r -q file.zip  file
[root@testhost opt]# unzip  file.zip  -d  /home

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值