压缩


gzip 压缩命令

Usage: gzip [OPTION]... [FILE]...

1.gzip 只能压缩文件,不能压缩目录

2.gzip 压缩文件之后,源文件会消失

3.gzip 解压文件后,压缩包会消失

4.gzip 压缩文件,后面可以跟多个文件名,但是会分别压缩一个包

5.使用zcat可以直接查看压缩包中文件内容

6.打包的文件在哪个路径下,然后就把包放在源文件所在的目录中

单个文件压缩

[root@localhost ~]# gzip file1

# 多个文件压缩

[root@localhost ~]# gzip file2 file3

# 解压

[root@localhost ~]# gzip -d file1.gz

# 查看文件类型

[root@localhost ~]# file file1.gz


zip压缩命令

Usage:zip [option] [filename] [file]...

1.zip 压缩文件,源文件不会消失

2.zip 解压后,如果目录下存在和压缩包内相同的文件名,则询问是否覆盖

3.zip 压缩目录,什么参数都不加的情况下,只能打包空目录,如果想要打包目录下所有内容加 -r参数

4.如果打包的是绝对路径,那么会把绝对路径的内容都打包进去,并且删除/,打包,在哪个目录下执行,就会打包到哪个目录下

zip 行李箱 衣服...

-r:递归打包

-q:不输出打包过程

# 打包单个文件

[root@localhost ~]# zip f.zip file1

# 打包多个文件

[root@localhost ~]# zip f.zip file1 file8

# 打包目录

[root@localhost ~]# zip -r zls_dir.zip /root/zls_dir


tar 归档

用法: tar [选项...] [FILE]...

c:创建新的归档文件(create)

x:解压归档文件

f:指定包文件名(行李箱),如果是多个选项,f一定要放在最后

v:显示过程

z:归档之后使用gzip压缩

P:不删/ ,使用绝对路径打包

t:查看压缩包中的压缩项目

C:指定解压路径

j:

J:

h:打包软链接

X:将要排除的目录或者文件,写到文件中,然后使用X指定该文件,即可排除

--exclude:直接写要排除的文件或者目录


练习题:

1.如何使用gzip命令对文件进行压缩、解压

压缩:gzip filename

解压:gzip -d filename

2.如何用zip命令对文件以及目录进行压缩、解压

压缩文件:zip 包名.zip file1 file2  file3

压缩目录:zip -r  包名.zip dir

解压:unzip 包名.zip

3.创建一个自己名字的文件至/opt目录

touch /opt/zls

4.打包opt整个目录,并命名test_opt.tar.gz

tar zcf test_opt.tar.gz /opt

5.查看打包好的test_opt.tar.gz里的文件

tar tf test_opt.tar.gz

6.将打包好的test_opt.tar.gz内容指定解压至/tmp目录

tar xf test_opt.tar.gz -C /tmp

7.打包etc一级目录下的所有文件,不要目录只要文件

[root@localhost etc]# cd /etc && tar zcfh etc_2.tgz $(ls -F /etc |grep -v '/$')


1.linux下常见的压缩包类型有哪些

gzip

zip

tar

02.执行这条命令  echo "qiandao da shuai bi" > oldboy.txt

(1)使用gzip命令进行打包

[root@oldboy ~]# gzip oldboy.txt

(2)查看压缩包的内容

[root@oldboy ~]# zcat oldboy.txt.gz

(3)解压刚才的压缩包(至少用两种方式解压)

[root@oldboy ~]# gzip -d oldboy.txt.gz

[root@oldboy ~]# gunzip oldboy.txt.gz

03.用gzip命令打包oldboy目录

抱头痛哭

[root@oldboy ~]# gzip oldboy/

gzip: oldboy/ is a directory -- ignored

04.查看打包之后的/etc/hosts的文件内容,在不解压的情况下查看

zcat  hosts.gz

05.使用zip打包/etc目录,包名为etc.zip

[root@oldboy ~]# zip -r etc.zip /etc/

06.将/etc/passwd /etc/resolv.conf /etc/hosts这三个文件复制到/opt目录下,将/opt下的这3个文件打包,包名为qiandao.zip,并把压缩包放在/mnt目录下

[root@oldboy ~]# zip /mnt/qiandao.zip /opt/hosts /opt/resolv.conf /opt/passwd

  adding: opt/hosts (deflated 65%)

  adding: opt/resolv.conf (deflated 14%)

  adding: opt/passwd (deflated 59%)

07.用zip打包/opt目录,要求不显示打包过程

[root@oldboy ~]# zip -q  /mnt/qiandao1.zip /opt/hosts /opt/resolv.conf /opt/passwd

08.将/etc/hosts文件用tar格式打包

[root@oldboy ~]# tar zcf hosts.tar.gz /etc/hosts

09.使用tar打包/var/log/目录

[root@oldboy ~]# tar zcf var.tar.gz /var/log/

10.查看/var/log/目录的压缩包中有哪些内容

[root@oldboy ~]# tar tf var.tar.gz

11.将/var/log/目录解压到/opt目录中

[root@oldboy ~]# tar xf var.tar.gz -C /opt/

12.打包/etc/目录,要求是.bz2格式

[root@oldboy ~]# tar cjf etc.tar.bz2 /etc/

13.打包/var/log目录,要求是.xz格式

[root@oldboy ~]# tar cJf etc.tar.xz /etc/

14.使用tar命令打包/etc/时,会出现一个删根的操作,怎样打包不会进行删根的操作

[root@oldboy ~]# tar zcfP var1.tar.gz /var/log/

15.打包/etc/目录,要求不打包/etc/hosts这个文件

[root@oldboy ~]# tar zcf etc.tar.gz /etc/ --exclude=/etc/hosts

16.打包/etc/目录,要求不打包/etc/hosts和/etc/hostname这两个文件

[root@oldboy ~]# tar zcf etc1.tar.gz /etc/ --exclude=/etc/hosts --exclude=/etc/hostname

17.已知/etc/grub2.cfg文件是个软连接文件,在你不知道的情况下,请问怎么打包该文件的真实文件

[root@oldboy ~]# ll /etc/grub2.cfg

lrwxrwxrwx. 1 root root 22 Sep 24  2019 /etc/grub2.cfg -> ../boot/grub2/grub.cfg

[root@oldboy ~]# cd ../boot/grub2

[root@oldboy /boot/grub2]# pwd

/boot/grub2

[root@oldboy /boot/grub2]# ll

[root@oldboy /boot/grub2]# tar zcf xxxx.tar.gz grub.cfg

18.把/var/log/目录中所有.log的文件进行打包成一个压缩包,名称定义为log.tar.gz的压缩包

#方法一

[root@oldboy /var/log]# ls /var/log/|grep -E "*\.log$" | xargs tar zcf log.tar.gz

#方法二

[root@oldboy ~]# tar zcf log.tar.gz $(find /var/log/ -maxdepth 1 -type f  -name "*.log")

#方法三

[root@oldboy ~]# tar czf  log1.tar.gz  /var/log/*.log

19.打包/etc/目录,命令以ip地址+当前时间方式的压缩包

    比如: 10.0.0.100_2019-12-24_etc.tar.gz

[root@oldboy ~]# tar zcf $(hostname -I|awk '{print $1}')_$(date +%F)_etc.tar.gz /etc/

20.创建/data/bak目录,然后复制如下文件到/data/bak目录下

/etc/hosts

/etc/resolv.conf

/etc/fstab

/etc/bashrc

/etc/profile

/etc/rc.local

/etc/sudoers

21.接22题,使用tar命令对/data/bak目录下的文件及目录以gzip的格式进行归档压缩到/data目录下(压缩包的名字以自己名字命名)

[root@oldboy /data]# tar zcf qzg.tar.gz bak/

22.使用tar命令查看上题/data目录下压缩包内的内容

[root@oldboy /data]# tar tf qzg.tar.gz

bak/

bak/hosts

bak/resolv.conf

bak/fstab

bak/bashrc

bak/profile

bak/rc.local

bak/sudoers

23.把第22题/data目录下的压缩包,解压到/backup目录下

[root@oldboy /data]# tar xf qzg.tar.gz  -C /backup

24.再次使用tar命令把/data/bak目录下的文件及目录以gzip的格式进行归档压缩到/data目录下,但是在进行归档压缩时,排除文件“sudoers”,然后查看该压缩包内容是否存在文件“sudoers”(压缩包名自行拟定)

[root@oldboy /data]# tar zcf qzg1.tar.gz bak/  —exclude=bak/sudoers

25.打包/etc/目录到/opt/目录下,名称要求以当前主机名和ip地址命名,

    例:oldboy_10.0.0.100.tar.gz

tar zcf  /opt/$(hostname)_$(hostname -I|awk '{print $1}’).tar.gz /etc

##曾导题

26.打包etc目录下的所有文件,不要目录只要文件

grep -v

find

27.打包etc目录下的所有文件,排除passwd,shadow

- -exclude=xxx

28.打包etc目录下的所有以p开头的文件

[root@oldboy ~]# tar zcf etc33.tar.gz $(find /etc/ -maxdepth 1 -type f -name "p*")

29.打包etc目录下所有大于1M的文件

[root@oldboy ~]# tar zcf cls.tar.gz $(find /etc/ -type f -size +1M)

30.列出linux下面常用的打包工具并写出相应的压缩解压参数(两种)

tar

tar zcf

tar tf

tar xf

-C 

—exclude

-X

zip

-r

unzip

gzip

#回顾题

31. 创建/data/test目录,已知/data目录不存在,请给出命令?

-p

32.查看/etc/hosts文件的内容。(请使用三种方法实现)

cat  less  more    tail (特殊一点)

33.给文件的每一行结尾加一个标识符。(两种方法实现)

#方法一

vim

:%s#$#\##g

#方法二

[root@oldboy ~]# sed -i "s#\$#\##g" qzg.txt

34.把/etc/中的所有目录(仅目录)  复制到/tmp下,目录结构不变?

[root@oldboy ~]# find /etc/ -type d -exec mkdir  /tmp/{} \;

[root@oldboy /tmp]# find /etc/ -type d |xargs -I {} mkdir -p /tmp/{}

[root@oldboy ~]# find /tmp/etc/ ! -type d -delete

35.显示系统中所有以tre开头的软件包?(两种方法)

[root@oldboy ~]# yum list |grep "^tre"

tree.x86_64                              1.6.0-10.el7                @base   

tre.x86_64                                0.8.0-18.20140228gitc2f5d13.el7

tre-common.noarch                        0.8.0-18.20140228gitc2f5d13.el7

tre-devel.x86_64                          0.8.0-18.20140228gitc2f5d13.el7

treelayout.noarch                        1.0.3-4.el7                  epel   

treelayout-demo.noarch                    1.0.3-4.el7                  epel   

treelayout-javadoc.noarch                1.0.3-4.el7                  epel   

tremulous.x86_64                          1.2.0-0.15.beta1.el7        epel   

tremulous-data.noarch                    1.2.0-0.9.beta1.el7          epel   

36.列举你熟悉的服务器性能查看命令(不低于8个)

w  uptime  df  free  iostat  top  htop  iotop  iftop  glances  netstat  vmstat  ps

37.快速返回到上一次所在的目录的命令为( )

cd -

38.把/data 目录复制到 /tmp目录下并改名为data_20200327 (20200327为当天时间)?

[root@oldboy ~]# date +%Y%m%d

20200330

[root@oldboy ~]# cp -r /data/ /tmp/data_$(date +%Y%m%d)

39.Linux文件系统中每个文件用()或者称为()来标识?

inode

索引

40.操作系统历史命令记录中,执行次数最多的5条?

[root@oldboy ~]# history |awk '{print $2}'|sort |uniq -c|sort -nrk 1|head -5

    140 ll

    83 sed

    74 cat

    51 vim

    50 grep

你可能感兴趣的:(压缩)