Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1681612
  • 博文数量: 2270
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 22829
  • 用 户 组: 普通用户
  • 注册时间: 2020-11-26 14:30
个人简介

更多python、Linux、网络安全学习内容,可移步:www.oldboyedu.com或关注\"老男孩Linux\"公众号

文章分类

全部博文(2270)

文章存档

2024年(115)

2023年(643)

2022年(693)

2021年(734)

2020年(80)

我的朋友

分类: LINUX

2022-01-19 16:23:59

  Linux里面归档命令是什么?在Linux操作系统中,归档命名为tar,它有很多的好处,方便使用、易于管理,那么你知道归档命令如何使用吗?本篇文章为大家分享一下,感兴趣的朋友一起来看看吧。

  Linux里面归档命令是tar

  详细内容如下:

  1、tar 打包压缩

  1)【打包】 为什么要打包,压缩?

  -zcvf 打包组合 zcf

  z 压缩(gzip压缩)

  c 创建

  v 显示过程

  f 针对文件

  语法:

  命令 选项

  tar zcf 【压缩包的名字.tar.gz】 【要打包的东西】

  装东西的筐 苹果

  练习1:把/etc/目录打包到/opt下面。

  ls -d /etc #检查有没有要打包的目录

  cd / #切换到打包目录的上一级

  tar zcf /opt/etc.tar.gz ./etc

  执行过程:

  [root@oldboy ~]# cd /

  [root@oldboy /]# tar zcvf /opt/etc.tar.gz ./etc/

  [root@oldboy /]# ls /opt/

  etc etc.tar.gz services

  练习2:把/usr/local目录打包到/tmp下面。

  [root@oldboy /]# ls -ld /usr/local/

  drwxr-xr-x. 12 root root 131 5月 7 2021 /usr/local/

  [root@oldboy /]# cd /usr

  [root@oldboy usr]# tar zcf /tmp/local.tar.gz ./local/

  v 显示过程 工作中和领导汇报工作。

  关键:

  1)结果:赚100万。

  2)过程:怎么赚的100万?

  2)【查看包里内容】

  t 查看包里内容

  tar tf 包名

  [root@oldboy usr]# tar tf /tmp/local.tar.gz

  3)【解包】

  组合zxvf,缩写xf

  x 解压

  cd /opt

  tar zxvf etc.tar.gz #解压到了当前目录

  tar zxf etc.tar.gz

  tar xf etc.tar.gz

  -C 指定路径解压

  [root@oldboy opt]# tar xf etc.tar.gz -C /home/oldboy/

  [root@oldboy opt]# ls /home/oldboy/

  -p 保持属性(zcfp)

  -j 通过bzip2命令压缩或解压(非常少了)

  tar jcvf 包名.tar.bz2 包名

  [root@oldboy opt]# tar jcvf /tmp/a.tar.bz2 /etc/hosts #打包实践

  tar: 从成员名中删除开头的“/”

  /etc/hosts

  [root@oldboy opt]# ls /tmp/

  a.hosts a.tar.bz2 etc local.tar.gz services

  [root@oldboy opt]# cd /tmp/

  [root@oldboy tmp]# tar zxvf a.tar.bz2

  gzip: stdin: not in gzip format

  tar: Child returned status 1

  tar: Error is not recoverable: exiting now

  两种解压方法:

  [root@oldboy tmp]# tar jxvf a.tar.bz2

  etc/hosts

  [root@oldboy tmp]# tar xf a.tar.bz2 #gzip,bzip统一解压方法。

  排除打包--exclude=

  tar zcvf /tmp/pai.tar.gz ./oldboy/ --exclude=file1

  命令 参数 包名 目录 排除的文件

  环境准备

  [root@oldboy tmp]# mkdir /oldboy -p

  [root@oldboy tmp]# cd /oldboy/

  [root@oldboy oldboy]# ls

  [root@oldboy tmp]# mkdir /oldboy -p

  [root@oldboy tmp]# cd /oldboy/

  [root@oldboy oldboy]# ls

  [root@oldboy oldboy]# touch file{1..5}

  [root@oldboy oldboy]# ls

  file1 file2 file3 file4 file5

  [root@oldboy oldboy]# cd ../

  练习排除打包

  排除file1

  [root@oldboy /]# tar zcvf /tmp/pai.tar.gz ./oldboy/ --exclude=file1

  ./oldboy/

  ./oldboy/file2

  ./oldboy/file3

  ./oldboy/file4

  ./oldboy/file5

  排除file1到file3 ###--exclude=file{1..3}

  [root@oldboy /]# tar zcvf /tmp/pai.tar.gz ./oldboy/ --exclude=file{1..3}

  ./oldboy/

  ./oldboy/file4

  ./oldboy/file5

  指定要排除的文件### --exclude=file{1,3,5}

  [root@oldboy /]# tar zcvf /tmp/pai.tar.gz ./oldboy/ --exclude=file{1,3,5}

  ./oldboy/

  ./oldboy/file2

  ./oldboy/file4

  ###--exclude=file1 --exclude=file2

  [root@oldboy /]# tar zcvf /tmp/pai.tar.gz ./oldboy/ --exclude=file1 --exclude=file2

  ./oldboy/

  ./oldboy/file3

  ./oldboy/file4

  ./oldboy/file5

  从文件中排除打包--exclude-from(-X)

  建立排除的文件paichu.log,内容就是排除的文件名

  cat >paichu.log<

  file1

  file5

  EOF

  [root@oldboy /]# cat paichu.log

  file1

  file5

  [root@oldboy /]# tar zcvfX /tmp/pai.tar.gz paichu.log ./oldboy/

  ./oldboy/

  ./oldboy/file2

  ./oldboy/file3

  ./oldboy/file4

  tar zcvfX /tmp/pai.tar.gz paichu.log ./oldboy/

  存储排除文件名的文件

  -h 打包软链接

  /etc/rc.local是软链接文件,指向真实路径/etc/rc.d/rc.local

  默认打包的时候,只打包了软链接文件。

  tar zcvf /backup/rc.local_1.tar.gz /etc/rc.local

  【而加上-h打包,可以打包软链接对应的真实文件】

  [root@oldboy etc]# tar zcvfh /backup/rc.local_1.tar.gz /etc/rc.local

  tar: 从成员名中删除开头的“/”

  /etc/rc.local

  [root@oldboy etc]# cd /backup/

  [root@oldboy backup]# tar xf rc.local_1.tar.gz

  [root@oldboy backup]# cat etc/rc.local

  #!/bin/bash

  touch /var/lock/subsys/local

阅读(376) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~