请输入图片描述
今天主要是find查找文件,tar打包压缩,tar解压,zip压缩解压。

find查找文件

1、find按照文件类型查找

语法结构:
find 从哪里找 按照什么方式查找 具体类型
find 路径 -type 类型
类型:
f 文件
d 目录
l 软链接
b 块设备
c 字节设备

! 查找取反 (基本不用)

示例1:查找当前目录下所有的文件
find ./ -type f

示例2:查找当前目录下除了所有的文件
find ./ ! -type f

示例3:查找当前目录的所有目录,包括目录下的目录,递归查找
find ./ -type d

2、find按照文件名称查找

语法结构:
find 路径 -name "名称" 
可以是查文件,也可以查目录
在根目录下查找会消耗性能
双引号是固定语法,最好用双引号

示例1:查找当前名称为1.log的文件
find ./ -name "1.log"

示例2:查找当前名称为2.log的文件,并忽略大小写
find ./ -iname "2.log"

示例3:查找test目录下名称为3.log的文件(默认为并且关系)
find test/ -type f -name "3.log"

3、find按照深度等级查找

查询test下最大深度等级为1级的目录
find test/ -maxdepth 1 -type d #顺序不能错

4、find按照inode号查找

find ./ -inum inode号

5、find按照文件大小进行查找

find 路径/ -size 10M  等于
find 路径/ -size +10M 大于
find 路径/ -size -10M 小于

注意加单位
默认会查目录下的文件,默认递归查找

并且 或者
find ./ -size +2M -size -20M      默认就是并且 -and -a
find ./ -size 10M -o -size +20M   -or -o      

在find里, {}不支持,用[],任意单个字符
[1-22]      1到2,或者2,只能匹配一个字符
[0-9][0-9]  0到99,[]是或者,不是区间   
[0-9a-z]    0到9,a到z
[0-9a-zA-Z] 0到9,a到z,A-Z    

6、find按照时间进行查找

find 路径 -mtime 7  距离现在刚好7天
find 路径 -mtime +7 7天前 上上周 上上上周......
find 路径 -mtime -7 7天内 上周

-mtime 文件修改时间
-ctime 文件属性变化或修改的时间
-atime 文件访问时间

示例1:查找7天前的文件
find ./ -type f -mtime +7     

小结:

按照文件类型查找
find ./ -type f

find ./ -type d

按照文件名称查找

find ./ -name "test.txt"

find /opt/ -name "*.txt"

find ./ -type f -name "*.txt"

忽略大小写

find ./ -type f -iname "*.txt"

按照inode号查找

find ./ -ium xxxxx

按照深度等级

find ./ -maxdepth 1 -type f

按照文件大小查找

find ./ -type f -size +10M

find ./ -type f -size -10M

find ./ -type f -size -10M -size +2M

find ./ -type d -size +1M(大于1M说明里面小文件数量特别多)

按照时间进行查找

find ./ -type f -mtime +7 查找7天前修改过的文件

企业中常用find语句:

find / -name "test.sh"

find /data/ -type f

find /data/ -type f -mtime +30

7、将查找到的文件交给其他命令

cp mv rm
示例1:查找名字为1.txt的文件并拷贝到/opt目录  覆盖无提示
cp 源文件 目标文件
find ./ -name all.txt|xargs -i cp {} /opt
 
示例2:查找名字为1.txt的文件查看或删除  覆盖无提示
find ./ -name 1.txt|xargs ls -l
find ./ -name 1.txt|xargs rm  
调用alias发现
alias rm='rm -i'
rm因为本身系统有这个命令
所以甩到后面去是不使用 -i的
就是默认不提示,不提示的话,就没有必要带 -f
但是如果删除目录是需要带 -r的
-f可以带,但是没有必要
如果没有带-r,错误信息照样提示,只是没有交互了
 
示例3:查找名字为1.txt的文件移动/opt目录
find ./ -name all.txt|xargs -i mv {} /opt
 
方法2:使用esec
示例1:查找名字为1.txt的文件并拷贝到/opt目录
find ./ -name all.txt -exec cp {} /opt \;
 
示例2:查找名字为1.txt的文件查看或删除
find ./ -name 1.txt -exec ls -l {} \;
find ./ -name 1.txt -exec rm {} \;
 
\撬棍的含义是还原本意
;在系统中有特殊的含义 是命令的分割符
但是在find中带-exec句式必须以普通的符号;结尾
 
;不管前面命令是否执行成功,都可以往后分割
命令;命令;命令
 
;  前面的命令不管成不成功都执行后面的命令
&& 前面的命令必须执行成功才会执行后面的命令 
|| 前面的命令必须执行失败才会执行后面的命令
cd test || mkdir test
 
方法3:``或者$()  覆盖有提示
先执行里面命令,留在原地,交给其他命令调用
cp `find ./ -name '1.log'` /opt
cp $(find ./ -name '1.log') /opt

tar打包压缩

zip传到linux可以正常使用,rar还需要安装别的东西

可以rar解压再打包成zip

Windows压缩格式:RAR,RAR4,RAR5,ZIP

Linux系统压缩格式:tar.gz,zip

tar 参数选项 压缩包名字.tar.gz 文件名称/目录
注意后缀名固定,先压缩包后文件,可以理解成,先有框子,再去找东西放
tar -zcvf test.tar.gz 1.txt 2.txt /opt/3.txt
    -z    使用gzip压缩
    -c    creat创建
    -v    verbose显示过程 可以省略
    -f    file指定文件名称
    -tf   查看压缩包里面的文件名
    -P    不提示并在打包时不去除根,但是压缩的时候也去除
    -xf    解压文件
    -C    解压到指定位置
 
示例1:tar压缩打包单个文件
[root@VMware-xiaopihai ~]#touch 1.txt
[root@VMware-xiaopihai ~]#tar zcvf test.tar.gz 1.txt 
1.txt
[root@VMware-xiaopihai ~]#ll
total 4
-rw-r--r-- 1 root root   0 Mar  7 15:20 1.txt
-rw-r--r-- 1 root root 108 Mar  7 15:20 test.tar.gz
[root@VMware-xiaopihai ~]#
 
示例2:压缩多个文件,可以包含目录
[root@VMware-xiaopihai ~]#touch 1.txt
[root@VMware-xiaopihai ~]#mkdir 2
[root@VMware-xiaopihai ~]#tar zcvf all.tar.gz 1.txt 2
1.txt
2/
[root@VMware-xiaopihai ~]#ll
total 4
-rw-r--r-- 1 root root   0 Mar  7 15:25 1.txt
drwxr-xr-x 2 root root   6 Mar  7 15:25 2
-rw-r--r-- 1 root root 135 Mar  7 15:25 all.tar.gz
[root@VMware-xiaopihai ~]#
 
示例3:指定压缩包的位置,加路径即可
[root@VMware-xiaopihai ~]#tar zcvf /opt/test.tar.gz 1.txt 2.txt
 
示例4:压缩尽量使用相对路径
[root@VMware-xiaopihai ~]#tar zcvf etc.tar.gz /etc/hosts /etc/passwd
 
[root@VMware-xiaopihai ~]#tar zcvf etc.tar.gz /etc/hosts /etc/passwd
tar: Removing leading `/' from member names
/etc/hosts
/etc/passwd
[root@VMware-xiaopihai ~]#    去掉了根目录
[root@VMware-xiaopihai ~]#tar zcvfP etc.tar.gz /etc/hosts /etc/passwd
/etc/hosts
/etc/passwd
[root@VMware-xiaopihai ~]#     连根目录一起压,解压还是会去掉根目录的
[root@VMware-xiaopihai ~]#cd /etc
[root@VMware-xiaopihai /etc]#tar zcvf test1.tar.gz hosts passwd
hosts
passwd
[root@VMware-xiaopihai /etc]#    用相对路径比较好

排除压缩某个文件

方法1:
tar zcvf all.tar.gz *txt --exclude=1.txt       排除压缩1.txt
tar zcvf all.tar.gz *txt --exclude={1..3}.txt   排除压缩1.txt 2.txt 3.txt 
tar zcvf all.tar.gz *txt --exclude={1,2,3,test1}.txt   排除压缩1.txt 2.txt 3.txt test1 

方法2:
将文件名纵向输入到文件123.txt中,删除需要的文件名
tar zcvf a.tar.gz ./* --exculed-from=123.txt

tar解压

tar xf 包.tar.gz
tar -c
示例1:解压
[root@VMware-xiaopihai ~]#tar xf test.tar.gz 

示例2:tar指定解压到目录
[root@VMware-xiaopihai ~]#tar xf test.tar.gz -C /opt/

示例3:在当前opt目录下解压root下的all.tar.gz
压缩包不在当前,也需要加大C,去解压到当前
[root@VMware-xiaopihai ~]#tar xf test.tar.gz -C .

zip压缩解压

压缩
[root@VMware-xiaopihai ~]#touch 1.txt 2.txt
[root@VMware-xiaopihai ~]#zip test.zip 1.txt 2.txt 
  adding: 1.txt (stored 0%)
  adding: 2.txt (stored 0%)
[root@VMware-xiaopihai ~]#
 
查看压缩包内容
zipinfo
[root@VMware-xiaopihai ~]#zip test.zip 1.txt 2.txt 
  adding: 1.txt (stored 0%)
  adding: 2.txt (stored 0%)
[root@VMware-xiaopihai ~]#zipinfo test.zip 
Archive:  test.zip
Zip file size: 298 bytes, number of entries: 2
-rw-r--r--  3.0 unx        0 bx stor 23-Mar-07 19:30 1.txt
-rw-r--r--  3.0 unx        0 bx stor 23-Mar-07 19:31 2.txt
2 files, 0 bytes uncompressed, 0 bytes compressed:  0.0%
[root@VMware-xiaopihai ~]#
 
解压
[root@VMware-xiaopihai ~]#unzip  test.zip 
Archive:  test.zip
 extracting: 1.txt                   
 extracting: 2.txt                   
[root@VMware-xiaopihai ~]#ls
1.txt  2.txt  test.zip
[root@VMware-xiaopihai ~]#
 
指定解压位置
[root@VMware-xiaopihai ~]#unzip  test.zip -d /opt/
Archive:  test.zip
 extracting: /opt/1.txt              
 extracting: /opt/2.txt              
[root@VMware-xiaopihai ~]#ls /opt
1.txt  2.txt
[root@VMware-xiaopihai ~]#

持续分享运维干货,感谢大家的阅读和关注!

发表评论

召唤看板娘