前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >文件查找与打包压缩

文件查找与打包压缩

作者头像
用户8639654
修改2021-08-18 17:55:45
5870
修改2021-08-18 17:55:45
举报
文章被收录于专栏:云计算运维

grep: 文件内容过滤

代码语言:javascript
复制
 [root@qfedu.com ~]# grep 'root' /etc/passwd  #从/etc/passwd文件中过滤root字段
 root:x:0:0:root:/root:/bin/bash
 operator:x:11:0:operator:/root:/sbin/nologin

查找命令

代码语言:javascript
复制
 [root@qfedu.com ~]# which ls
 alias ls='ls --color=auto'
         /usr/bin/ls
 [root@qfedu.com ~]# which cd
 /usr/bin/cd
 [root@qfedu.com ~]# which rm
 alias rm='rm -i'
         /usr/bin/rm

查询命令和配置文件的位置

代码语言:javascript
复制
 [root@qfedu.com ~]# whereis rpm 
 rpm: /usr/bin/rpm /usr/lib/rpm /etc/rpm /usr/share/man/man8/rpm.8.gz
 [root@qfedu.com ~]# whereis passwd
 passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz

一、find详解: 文件查找,针对文件名

代码语言:javascript
复制
 语法:
 #find 路径 条件 跟条件相关的操作符   [-exec 动作]
 路径:
 1.默认不写路径时查找的是当前路径.
 2.加路径。
 条件:
 1.指定的名称  -name
 2.文件类型 - type
 3.权限
 4.时间

1.1.按文件名

代码语言:javascript
复制
 从根开始找文件
 [root@qfedu.com ~]# find / -name “file2” #从根开始找文件
 /root/file2
 /var/tmp/file2
 [root@qfedu.com ~]# find /etc -name "ifcfg-ens33" #以名字的方式查找 
 [root@qfedu.com ~]# find /etc -iname "Ifcfg-ens33" #-i忽略大小写

熟用*通配符

代码语言:javascript
复制
 [root@qfedu.com ~]# find /etc -iname "*.txt"
 参数解释:
 *:表示所有字符

1.2.按文件大小 -size

代码语言:javascript
复制
 [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:add
 [root@qfedu.com ~]# find / -size -1M -o -size +80M #查找/下面小于1M或者大于80M的文件
 -o:or
 [root@qfedu.com ~]# find / -size -3M -a -name "*.txt" #查找/ 下面小于3M而且名字是.txt的文件

1.3按时间查找

代码语言:javascript
复制
 按时间找(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分钟之内

1.4按文件类型

代码语言:javascript
复制
 [root@qfedu.com ~]# find /dev -type f   #f普通文件
 [root@qfedu.com ~]# find / -type f -size -1M -o -name "*.txt"
 ​
 [root@qfedu.com ~]# find /dev -type d   #d目录
 [root@qfedu.com ~]# find /etc/ -type d -name "*.conf.d"
 ​
 [root@qfedu.com ~]# find /dev -type l   #l链接
 ​
 [root@qfedu.com ~]# find /dev -type b   #b块设备
 [root@qfedu.com ~]# find /dev/ -type b -name "sd*"
 ​
 [root@qfedu.com ~]# find /dev -type c   #c字符设备
 [root@qfedu.com ~]# find /dev -type s   #s套接字

1.5按文件权限

代码语言:javascript
复制
 [root@qfedu.com ~]# find . -perm 644            #.是当前目录    精确查找644  
 [root@qfedu.com ~]# find /usr/bin  -perm -4000  #包含set uid
 [root@qfedu.com ~]# find /usr/bin  -perm -2000  #包含set gid
 [root@qfedu.com ~]# find /usr/bin  -perm -1000  #包含sticky

1.6找到后处理的动作 ACTIONS

代码语言:javascript
复制
 -name "ifcfg*" | xargs
 -name "ifcfg*" -print   #打印
 [root@qfedu.com ~]# find /etc -name "ifcfg*" -exec cp -rf {} /tmp \; #exec命令对之前查找出来的文件做进一步操作-----  查找带ifcfg开头的文件复制到tmp下
 [root@qfedu.com ~]# touch /home/test{1..20}.txt
 [root@qfedu.com ~]# find /home/ -name test* -exec rm -rf {} \; #{}为前面查找到的内容,\; 格式

find使用xargs

代码语言:javascript
复制
 [root@qfedu.com ~]# find . -name "yang*.txt" |xargs rm -rf #找到之后删除处理xargs 参数传递
 [root@qfedu.com ~]# touch /home/test{1..20}.txt
 [root@qfedu.com ~]# # find /home/ -name "test*" | xargs -i cp {} /tmp/

案例1: 分别找出test5 和除了test5的文件

代码语言:javascript
复制
 [root@qfedu.com ~]# find /home/ -name *test5*
 [root@qfedu.com ~]# find /home/ ! -name "test5*" # !--取反

二、打包压缩

window打包压缩工具:

代码语言:javascript
复制
 结尾:.rar     .zip
 打包工具:winrar zip 7zip 好压

linux打包压缩工具:

代码语言:javascript
复制
 结尾:.tar.gz      .tar.bz2     .zip
 工具:gzip  bzip2(只压缩) 和 tar(打包)

打包

代码语言:javascript
复制
 #tar cvf file.tar 被打包的文件/目录 ...
 c :create  创建
 v :verbose 详细信息
 f :file  文件

解包

代码语言:javascript
复制
 #tar xvf 打包文件 [-C /root/Desktop]
 x: extract  加压缩  解包
 -C: 指定解包路径

案例

代码语言:javascript
复制
 [root@qfedu.com ~]# tar cvf dir1.tar /home/dir10/ #打包目录dir10,将包命名为dir1.tar
 [root@qfedu.com ~]# tar xf dir1.tar -C /usr/local/ #将dir1包解压到指定目录

压缩

代码语言:javascript
复制
 gzip bzip2
 压缩:
     #gzip  源文件      #格式  file.gz结尾
     #bzip2 源文件      #格式  file.bz2结尾

bzip2需要安装

代码语言:javascript
复制
 [root@qfedu.com ~]# yum -y install bzip2  #打包bzip2需要安装

解压缩

代码语言:javascript
复制
 #gunzip    压缩文件
 #bunzip2   压缩文件
 #gzip   -d 压缩文件  
 #bzip2  -d 压缩文件
 -d:dicompress 解压缩

案例

代码语言:javascript
复制
 [root@qfedu.com ~]# gzip file1  #压缩
 [root@qfedu.com ~]# gzip -d file1.gz #解压缩
 [root@qfedu.com ~]# gunzip file1.gz  #也是解压缩包
 [root@qfedu.com ~]# gzip -c file1 > /usr/local/file1.gz  #压缩到指定位置(注意以.gz结尾)
 [root@qfedu.com ~]# gunzip -c /usr/local/file1.gz > /opt/file1 #解压到指定位置(解压出的名字可以自定义)
 -c, --stdout

打包压缩一起:

代码语言:javascript
复制
 #tar cvzf file.tar.gz  源文件 ...
 #tar cvjf file.tar.bz2 源文件 ...
 z:表示gz压缩
 j:表示bz2压缩

解压解包一起:

代码语言:javascript
复制
 #tar xvzf 压缩文件 [-C 解压路径]
 #tar xvjf 压缩文件 [-C 解压路径]

案例

代码语言:javascript
复制
 [root@qfedu.com ~]# tar czf dir1.tar.gz dir1              #打包并压缩
 [root@qfedu.com ~]# tar xzf dir1.tar.gz -C /usr/local/    #解压到指定位置

打包到指定路径

代码语言:javascript
复制
 [root@qfedu.com ~]# tar czf /tmp/`date +%F-%T`-etc.tar.gz /etc/  #将打包的文件放到/tmp目录下,

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • grep: 文件内容过滤
    • 查找命令
      • 查询命令和配置文件的位置
        • 一、find详解: 文件查找,针对文件名
          • 1.1.按文件名
            • 1.2.按文件大小 -size
              • 1.3按时间查找
                • 1.4按文件类型
                  • 1.5按文件权限
                    • 1.6找到后处理的动作 ACTIONS
                    • 二、打包压缩
                    相关产品与服务
                    文件存储
                    文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档