linux 文件管理打包 作业

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

# gzip压缩
[root@linux10 ~]# gzip hello.txt
[root@linux10 ~]# ll
total 16
drwxr-xr-x 75 root root 8192 Jul  2 09:22 conf_dir
-rw-r--r--  1 root root  384 Jul  2 09:10 hello.txt.gz


# gzip解压
[root@linux10 ~]# gzip -d hello.txt.gz
[root@linux10 ~]# ll
total 16
drwxr-xr-x 75 root root 8192 Jul  2 09:22 conf_dir
-rw-r--r--  1 root root  834 Jul  2 09:10 hello.txt

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

# 目录压缩
# -rq 表示递归打包目录并且不显示打包过程
[root@linux10 ~]# zip -rq conf_dir.zip conf_dir
[root@linux10 ~]# ll
total 11744
drwxr-xr-x 75 root root     8192 Jul  2 09:22 conf_dir
-rw-r--r--  1 root root 12006608 Jul  2 09:34 conf_dir.zip
-rw-r--r--  1 root root      834 Jul  2 09:10 hello.txt


# 文件压缩
[root@linux10 ~]# zip -q hello.txt.zip hello.txt
[root@linux10 ~]# ll
total 11748
drwxr-xr-x 75 root root     8192 Jul  2 09:22 conf_dir
-rw-r--r--  1 root root 12006608 Jul  2 09:34 conf_dir.zip
-rw-r--r--  1 root root      834 Jul  2 09:10 hello.txt
-rw-r--r--  1 root root      524 Jul  2 09:37 hello.txt.zip

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

[root@linux10 ~]# touch /opt/mpd.txt

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

[root@linux10 ~]# tar -cf test_opt.tar.gz /opt
tar: Removing leading `/' from member names # 把根自动去掉了

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

[root@linux10 ~]# tar -tf test_opt.tar.gz
opt/
opt/oldboy.txt/
opt/mpd.txt

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

[root@linux10 ~]# tar xf test_opt.tar.gz -C /tmp

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

[root@linux10 ~]# find /etc -type f | xargs tar -cf etc_file.tar
tar: Removing leading `/' from member names

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

[root@linux10 ~]# cat > a.txt << eof
> paaswd
> shadow
> eof
[root@linux10 ~]# tar -cf etc.tar /etc -X a.txt
tar: Removing leading `/' from member names
[root@linux10 ~]# ll
total 27544
-rw-r--r-- 1 root root       14 Jul  2 11:01 a.txt
-rw-r--r-- 1 root root 28200960 Jul  2 11:02 etc.tar

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

[root@linux10 ~]# find /etc/ -type f -name p* | xargs tar -cf etc.tar
tar: Removing leading `/' from member names
#多数的命令是无法接收管道传递的参数。如echo,rm ,mkdir和ls
# xargs就很好的解决这个问题了

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

[root@linux10 ~]# find /etc -type f -size +1M | xargs tar -cf etc_min_1m 
tar: Removing leading `/' from member names
[root@linux10 ~]# ll
total 16260
-rw-r--r-- 1 root root 16650240 Jul  2 12:27 etc_min_1m
posted @ 2020-07-02 14:28  nick_xm  阅读(274)  评论(0编辑  收藏  举报