翼度科技»论坛 云主机 LINUX 查看内容

快速上手Linux核心命令(九):文件备份与压缩

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
目录

这期呢主要说一说Linux中文件备份与压缩命令,一共6个命令。这6个命令都是平常工作中非常非常常用的。

tar 打包备份

1、简介
tar 可以将多个文件压缩打包、压缩。是工作中经常用到的命令
2、语法格式
  1. tar [参数选项] [文件或目录]
复制代码
3、参数说明
参数参数说明z通过gzip压缩或解压c创建新的tar包v显示详细的tar命令执行过程f指定压缩文件名字t不解压查看tar包的内容p保持文件的原有属性j通过bzip2命令压缩或解压x解开tar包C指定解压的目录路径--exclude=PATTERN打包时排除不需要处理的文件或目录-h打包软连接文件指向的真实源文件-hard-dereference打包硬链接文件4、实践操作
① 打包博客文件(将/home/hexoBlog 打包成hexoBlog.tar.gz)
  1. [root@xiezhr home]# tar zcvf hexoBlog.tar.gz hexoBlog
  2. hexoBlog/
  3. hexoBlog/page/
  4. hexoBlog/page/3/
  5. hexoBlog/page/3/index.html
  6. hexoBlog/page/2/
  7. hexoBlog/page/2/index.html
  8. hexoBlog/contact/
  9. hexoBlog/contact/index.html
  10. hexoBlog/baidusitemap.xml
  11. hexoBlog/movies/
  12. hexoBlog/movies/index.html
  13. hexoBlog/images/
  14. hexoBlog/images/0.jpg
  15. hexoBlog/images/2020年年终总结.jpg
  16. hexoBlog/archives/
  17. hexoBlog/archives/87a8e6ef.html
  18. hexoBlog/archives/93a2b0e4.html
  19. hexoBlog/archives/b55ad976.html
  20. hexoBlog/archives/page/
  21. hexoBlog/archives/page/3/
  22. [root@xiezhr home]# ll
  23. drwxr-xr-x 30 git    git        4096 Jun  6 20:42 hexoBlog
  24. -rw-r--r--  1 root   root   43478804 Jun 14 22:43 hexoBlog.tar.gz
复制代码
② 不解压查看压缩包的内容
  1. [root@xiezhr home]# tar ztvf hexoBlog.tar.gz
  2. drwxr-xr-x git/git           0 2022-06-06 20:42 hexoBlog/
  3. drwxr-xr-x git/git           0 2022-02-26 16:43 hexoBlog/page/
  4. drwxr-xr-x git/git           0 2022-06-03 14:57 hexoBlog/page/3/
  5. -rw-r--r-- git/git       56427 2022-06-03 14:57 hexoBlog/page/3/index.html
  6. drwxr-xr-x git/git           0 2022-06-03 14:57 hexoBlog/page/2/
  7. -rw-r--r-- git/git       63574 2022-06-03 14:57 hexoBlog/page/2/index.html
  8. drwxr-xr-x git/git           0 2022-06-03 14:57 hexoBlog/contact/
  9. -rw-r--r-- git/git       43745 2022-06-03 14:57 hexoBlog/contact/index.html
  10. -rw-r--r-- git/git        4169 2022-06-03 14:57 hexoBlog/baidusitemap.xml
  11. drwxr-xr-x git/git           0 2022-06-03 14:57 hexoBlog/movies/
  12. -rw-r--r-- git/git       43246 2022-06-03 14:57 hexoBlog/movies/index.html
  13. drwxr-xr-x git/git           0 2021-02-13 19:32 hexoBlog/images/
  14. -rw-r--r-- git/git      570852 2020-11-29 16:42 hexoBlog/images/0.jpg
  15. -rw-r--r-- git/git      133945 2021-02-13 19:32 hexoBlog/images/2020年年终总结.jpg
  16. drwxr-xr-x git/git           0 2022-06-06 20:42 hexoBlog/archives/
  17. -rw-r--r-- git/git       97587 2022-06-03 14:57 hexoBlog/archives/87a8e6ef.html
复制代码
③ 解压文件到指定目录
  1. [root@xiezhr home]# tar -zxvf hexoBlog.tar.gz -C /home/test/
  2. hexoBlog/
  3. hexoBlog/page/
  4. hexoBlog/page/3/
  5. hexoBlog/page/3/index.html
  6. hexoBlog/page/2/
  7. hexoBlog/page/2/index.html
  8. hexoBlog/contact/
  9. hexoBlog/contact/index.html
  10. hexoBlog/baidusitemap.xml
  11. hexoBlog/movies/
  12. hexoBlog/movies/index.html
  13. hexoBlog/images/
  14. hexoBlog/images/0.jpg
  15. hexoBlog/images/2020年年终总结.jpg
  16. hexoBlog/archives/
  17. hexoBlog/archives/87a8e6ef.html
  18. [root@xiezhr home]# cd /home/test/
  19. [root@xiezhr test]# ll
  20. total 1
  21. drwxr-xr-x 30 git    git      4096 Jun  6 20:42 hexoBlog
复制代码
④ 排除指定文件后打包
  1. # hexoBlog/books 目录下的文件将被排除后打包
  2. [root@xiezhr home]#  tar zcvf hexoBlog.tar.gz  hexoBlog --exclude=hexoBlog/books
  3. hexoBlog/
  4. hexoBlog/page/
  5. hexoBlog/page/3/
  6. hexoBlog/page/3/index.html
  7. hexoBlog/page/2/
  8. hexoBlog/page/2/index.html
复制代码
⑤ 使用-h参数打包链接文件
  1. [root@xiezhr etc]# tar zcfh local_h.tar.gz ./rc.local
  2. [root@xiezhr etc]# tar tfv local_h.tar.gz
  3. -rwxr-xr-x root/root       749 2022-03-22 09:10 ./rc.local
复制代码
tar 使用小结

  • 打包一个目录时,一般需要进入到目录的上级目录,然后再执行打包命令。解压时需要非常注意,文件是否会覆盖原文件
  • 打包记忆:**tar zcvf  /路径/篮子.tar.gz    相对路径/鸡蛋 **     【把鸡蛋放到篮子里】
gzip 压缩或解压文件

1、简介
gzip 命令用于将一个大的文件通过压缩算法变成一个小文件。
注意: gzip 命令不能直接压缩目录,如果要压缩目录时,先要用tar 将目录打包成一个文件,然后tar 再调用gzip压缩
2、语法格式
  1. gzip [参数选项] [文件]
复制代码
3、参数说明
参数参数说明-d解压文件-v显示命令执行的过程-l列出压缩文件的内容信息-c将内容输出到标准输出,不改变原始文件-r对目录下的所有文件递归进行压缩-数字指定压缩率,默认是6,值越大压缩率越高-t测试,检查压缩文件是否完整4、实践操作
① 把目录下的每个文件都单独压缩成.gz文件
  1. [root@xiezhr dir3]# ls
  2. a.txt  b.txt  c.txt  money.java  test.java  test.txt  tt.txt  user.java  xiezhr.java  xiezhr.txt
  3. [root@xiezhr dir3]# gzip *.txt
  4. [root@xiezhr dir3]# ls
  5. a.txt.gz  b.txt.gz  c.txt.gz  money.java  test.java  test.txt.gz  tt.txt.gz  user.java  xiezhr.java  xiezhr.txt.gz
复制代码
② 不解压显示压缩文件信息
  1. [root@xiezhr dir3]# gzip -l xiezhr.txt.gz
  2.          compressed        uncompressed  ratio uncompressed_name
  3.                  31                   0   0.0% xiezhr.txt
复制代码
③解压文件,并显示解压过程
  1. [root@xiezhr dir3]# ls
  2. a.txt.gz  b.txt.gz  c.txt.gz  money.java  test.java  test.txt.gz  tt.txt.gz  user.java  xiezhr.java  xiezhr.txt.gz
  3. [root@xiezhr dir3]# gzip -dv *.gz
  4. a.txt.gz:         0.0% -- replaced with a.txt
  5. b.txt.gz:         0.0% -- replaced with b.txt
  6. c.txt.gz:         0.0% -- replaced with c.txt
  7. test.txt.gz:     15.9% -- replaced with test.txt
  8. tt.txt.gz:        0.0% -- replaced with tt.txt
  9. xiezhr.txt.gz:    0.0% -- replaced with xiezhr.txt
  10. [root@xiezhr dir3]# ls
  11. a.txt  b.txt  c.txt  money.java  test.java  test.txt  tt.txt  user.java  xiezhr.java  xiezhr.txt
复制代码
注: 以上实践我们看到gzip命令在压缩和解压文件时,都会把源文件删除。当然也有其他方法可以解决这个问题,但是使用起来有点麻烦。
zip 打包和压缩文件

1、简介
zip 压缩格式是Windows和Linux通用的压缩格式。和gzip命令相比,zip命令压缩文件不会删除源文件,还可以压缩目录。所以相比gzip命令,我们使用zip命令会更多一些
2、语法格式
  1. zip [参数选项] [文件或目录]
复制代码
3、参数说明
参数参数说明-r指定目录下的所有文件和子目录一并压缩-x压缩文件时排除某个文件-q不显示压缩信息4、实践操作
① 压缩文件
  1. [root@xiezhr testdir]# ls
  2. xiezhr.txt
  3. [root@xiezhr testdir]# zip xiezhr.zip xiezhr.txt
  4.   adding: xiezhr.txt (deflated 17%)
  5. [root@xiezhr testdir]# ls
  6. xiezhr.txt  xiezhr.zip
复制代码
②压缩目录
  1. # 这里只将testdir这个目录压缩了,目录下的文件并没有压缩
  2. [root@xiezhr test]# zip testdir.zip ./testdir/
  3.   adding: testdir/ (stored 0%)
  4. # 使用-r参数可以将目录及目录下的文件也递归压缩了
  5. [root@xiezhr test]# zip -r testdir.zip ./testdir/
  6. updating: testdir/ (stored 0%)
  7.   adding: testdir/xiezhr.txt (deflated 17%)
复制代码
③ 排除压缩
  1. # 将abc.txt 文件排除到压缩之外
  2. [root@xiezhr testdir]# ll
  3. total 8
  4. -rw-r--r-- 1 root root  49 Jun 26 16:01 abc.txt
  5. -rw-r--r-- 1 root root 527 Jun 26 15:50 xiezhr.txt
  6. [root@xiezhr testdir]# cd ..
  7. [root@xiezhr test]# zip -r testdir.zip ./testdir/ -x testdir/abc.txt
  8. updating: testdir/ (stored 0%)
  9. updating: testdir/xiezhr.txt (deflated 17%)xxxxxxxxxx -[root@xiezhr testdir]# lltotal 8-rw-r--r-- 1 root root  49 Jun 26 16:01 abc.txt-rw-r--r-- 1 root root 527 Jun 26 15:50 xiezhr.txt[root@xiezhr testdir]# cd ..[root@xiezhr test]# zip -r testdir.zip ./testdir/ -x testdir/abc.txt updating: testdir/ (stored 0%)updating: testdir/xiezhr.txt (deflated 17%)shell
复制代码
unzip 解压zip文件

1、简介
unzip 命令可以解压zip格式的文件,一般与zip配套使用
2、语法格式
  1. unzip [参数选项] [压缩文件]
复制代码
3、参数选项
参数参数说明-l在不解压的情况下显示压缩包的内容-o解压时不提示是否覆盖文件-d指定解压目录-v解压时显示详细信息4、实践操作
①不解压查看压缩包里的文件
  1. [root@xiezhr test]# unzip -l testdir.zip
  2. Archive:  testdir.zip
  3.   Length      Date    Time    Name
  4. ---------  ---------- -----   ----
  5.         0  06-26-2022 16:01   testdir/
  6.       527  06-26-2022 15:50   testdir/xiezhr.txt
  7. ---------                     -------
  8.       527                     2 files
复制代码
② 解压文件
  1. # 不带参数解压文件时,会提示是否覆盖文件
  2. # [y]es, [n]o, [A]ll, [N]one, [r]ename  
  3. # y--是  n--否  A--所有文件都替换 N--所有文件都不替换 r--重命名
  4. [root@xiezhr test]# unzip testdir.zip
  5. Archive:  testdir.zip
  6. replace testdir/xiezhr.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
  7.   inflating: testdir/xiezhr.txt
  8. # -o 参数,解压时不提示覆盖
  9. [root@xiezhr test]# unzip -o testdir.zip
  10. Archive:  testdir.zip
  11.   inflating: testdir/xiezhr.txt
复制代码
③ 指定解压目录解压文件
  1. # 将testdir.zip 解压到指定目录dir7下
  2. [root@xiezhr test]# mkdir dir7
  3. [root@xiezhr test]# unzip -d /home/test/dir7/ testdir.zip
  4. Archive:  testdir.zip
  5.    creating: /home/test/dir7/testdir/
  6.   inflating: /home/test/dir7/testdir/xiezhr.txt  
  7. [root@xiezhr test]# cd dir7/
  8. [root@xiezhr dir7]# ls
  9. testdir
复制代码
scp 远程文件复制

1、简介
scp 命令用于不同主机之间文件复制。scp命令每次都是全量完整复制,因此效率不高,适合第一次复制时候使用,增量复制建议使用rsync命令
2、语法格式
  1. scp [参数选择] [用户@主机1:文件1] [用户@主机2:文件2]
复制代码
3、参数选项
参数参数说明-C压缩传输-l指定传输占用的宽带,单位Kbit/s-P 指定传输的端口号-p传输后保留文件原始属性-q不显示传输进度-r递归复制整个目录4、实践操作
① 将本地文件或目录复制到远程服务器
  1. [root@xiezhr ~]# scp  /home/test/xie.txt  42.192.46.248:/tmp
  2. root@42.192.46.248's password:
复制代码
② 从远程服务器将数据复制到本地
  1. [root@xiezhr ~]# scp 42.192.21.248:/tmp .
复制代码
rsync 文件同步工具

1、简介
rsync 可以实现全量或增量的文件复制的开源工具。 可以在本地计算机与远程计算机之间,或者两个本地目录之间同步文件 。 它也可以当作文件复制工具,替代cp和mv命令
2、语法格式
  1. # 1、本地模式
  2. rsync [参数选项] [源文件] [目标文件]
  3. # 2、远程同步模式
  4. # 拉取文件
  5. rsync [参数选项] [用户@主机:源文件] [目标文件]
  6. # 推送文件
  7. rsync [参数选项] [源文件] [用户@主机:目标文件]
复制代码
3、参数选项
参数参数说明-r递归方式同步目录-a可以替代-r参数,除了可以递归同步,还可以同步文件属性(修改时间、权限等)-n不确定 rsync 执行后会产生什么结果,可以先用-n参数模拟执行结果--delete使目标目录内容和源保持目录一致,删除不同文件--exclude同步时,排除不需要的文件或目录--include同步时,必须包含的文件4、实践操作
注: 有些Linux系统中可能没有默认安装rsync ,我们只需按照如下命令安装即可
  1. yum -y install rsync
复制代码
① 将当前目录下的hexoBlog 同步到 dir7目录下
  1. [root@xiezhr test]# rsync -r hexoBlog/ dir7/hexoBlog
  2. [root@xiezhr test]# cd dir7/
  3. [root@xiezhr dir7]# ll
  4. total 4
  5. drwxr-xr-x 30 root root 4096 Jun 29 20:54 hexoBlog
复制代码
② 将当前目录下的logo.png 和hexoBlog 同步到dir8/test 目录下
  1. [root@xiezhr test]# rsync -r logo.png hexoBlog/  dir8/test
  2. [root@xiezhr test]# cd dir8/test/
  3. [root@xiezhr test]# ll
  4. total 1420
  5. drwxr-xr-x  2 root root   4096 Jun 29 21:00 about
  6. drwxr-xr-x  6 root root   4096 Jun 29 21:00 archives
  7. drwxr-xr-x  2 root root   4096 Jun 29 21:00 artitalk
复制代码
注: -r表示递归,即包含子目录。注意,-r是必须的,否则 rsync 运行不会成功
③-a 参数,递归同步,同步修改时间、权限等信息
  1. # rsync -a hexoBlog hexoBlog_new
  2. # 目标目录hexoBlog_new 如果不存在,rsync 会自动创建。
  3. # 执行上面的命令后,源目录hexoBlog被完整地复制到了目标目录hexoBlog_new下面,hexoBlog_new/hexoBlog的目录结构。
  4. [root@xiezhr test]# ll
  5. total 16
  6. drwxr-xr-x  2 xiezhr xiezhr 4096 Mar 26 21:46 dir
  7. drwxr-xr-x 30 git    git    4096 Jun  6 20:42 hexoBlog
  8. -rw-r--r--  1 root   root     35 May  9 21:36 xie.txt
  9. -rw-r--r--  1 root   root    527 Apr  9 10:55 xiezhr.txt
  10. [root@xiezhr test]# rsync -a hexoBlog hexoBlog_new
  11. [root@xiezhr test]# ll
  12. total 20
  13. drwxr-xr-x  2 xiezhr xiezhr 4096 Mar 26 21:46 dir
  14. drwxr-xr-x 30 git    git    4096 Jun  6 20:42 hexoBlog
  15. drwxr-xr-x  3 root   root   4096 Jul  2 09:15 hexoBlog_new
  16. -rw-r--r--  1 root   root     35 May  9 21:36 xie.txt
  17. -rw-r--r--  1 root   root    527 Apr  9 10:55 xiezhr.txt
  18. #如果只想同步源目录 hexoBlog 里面的内容到目标目录hexoBlog_new,则需要在源目录后面加上斜杠。
  19. [root@xiezhr test]# rsync -a hexoBlog/ hexoBlog_new
  20. #上面命令执行后,hexoBlog目录里面的内容,就都被复制到了hexoBlog_new目录里面,并不会在hexoBlog_new下面创建一个hexoBlog子目录。
复制代码
④ -n 参数模拟执行结果
注:  如果不确定 rsync 执行后会产生什么结果,可以先用-n 参数模拟执行结果
  1. [root@xiezhr test]# rsync -anv xie.txt testdir
  2. sending incremental file list
  3. xie.txt
  4. sent 49 bytes  received 19 bytes  136.00 bytes/sec
  5. total size is 35  speedup is 0.51 (DRY RUN)
  6. # -n参数模拟命令执行的结果,并不真的执行命令。-v参数则是将结果输出到终端,这样就可以看到哪些内容会被同步。
复制代码
⑤ --delete 参数的使用,保持源目录和目标目录文件一致
  1. [root@xiezhr test]# rsync -av --delete hexoBlog hexoBlog_0702
  2. sending incremental file list
  3. created directory hexoBlog_0702
  4. hexoBlog/
  5. hexoBlog/Staticfile.txt
  6. hexoBlog/baidu_urls.txt
  7. hexoBlog/baidusitemap.xml
  8. hexoBlog/favicon.png
  9. hexoBlog/tags/问卷调查/index.html
  10. sent 63,638,655 bytes  received 8,246 bytes  42,431,267.33 bytes/sec
  11. total size is 63,597,216  speedup is 1.00
复制代码
注: 默认情况下,rsync 只确保源目录的所有内容(明确排除的文件除外)都复制到目标目录。它不会使两个目录保持相同,并且不会删除文件。使用--delete参数,这将删除只存在于目标目录、不存在于源目录的文件。
⑥ --exclude 参数 的使用,同步时排除某些文件
  1. [root@xiezhr test]# rsync -av --exclude '*.txt' hexoBlog hexoBlog_test
  2. sending incremental file list
  3. created directory hexoBlog_test
  4. hexoBlog/
  5. hexoBlog/baidusitemap.xml
  6. hexoBlog/favicon.png
  7. hexoBlog/favicon1.png
  8. hexoBlog/tags/问卷调查/index.html
  9. sent 63,638,026 bytes  received 8,208 bytes  127,292,468.00 bytes/sec
  10. total size is 63,596,717  speedup is 1.00
复制代码
注: 上面脚本将hexoBlog 目录下除了.txt外的文件都同步到了hexoBlog_test 目录下
⑦--include 参数的使用,同步时必须包含某些文件
  1. [root@xiezhr test]# rsync  -av --include "*.txt" --exclude "*" hexoBlog hexo_xie
  2. sending incremental file list
  3. sent 18 bytes  received 12 bytes  60.00 bytes/sec
  4. total size is 0  speedup is 0.00
复制代码
注:上述命令将hexoBlog目录下的所有文件排除,但包含txt文件同步到hexo_xie目录下
⑧远程同步资源
注: rsync 命令除了可以本地同步之外,还支持远程同步资源
  1. # 以下命令是将本地资源source 同步到远程服务器destination
  2. [root@xiezhr test]# rsync -av source/ username@remote_host:destination
  3. # 以下命令是将远程服务器资源source同步到本地 destination
  4. [root@xiezhr test]# rsync -av username@remote_host:source/ destination
复制代码
由于博主比较穷,手上只有一台云服务器,这里就没法做具体演示了
来源:https://www.cnblogs.com/xiezhr/p/17343249.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具