day10-Linux系统打包和时间命令及案例

1.tar 打包压缩

1.1 -zcvf 压缩打包

-z:压缩,gzip算法

-c:创建文件包

-v:显示命令执行过程

-f:针对文件

1.1.1 练习:把/etc目录打包到/opt下

bash 复制代码
[root@oldboy ~]# tar -zcvf /opt/etc.zip.gz /etc/
[root@oldboy ~]# ll /opt/ -h
总用量 11M
-rw-r--r--. 1 root root 11M 1月   1 19:08 etc.zip.gz

1.2 -tf 查看包内容

bash 复制代码
[root@oldboy opt]# tar -tf etc.zip.gz | head -10
etc/
etc/fstab
etc/crypttab
etc/mtab
etc/resolv.conf
etc/grub.d/
etc/grub.d/00_header
etc/grub.d/01_users
etc/grub.d/10_linux
etc/grub.d/20_linux_xen

-t:list,显示包内容

1.3 -zxvf 解包

-x:解压缩包

bash 复制代码
[root@oldboy /]# tar -zxvf etc.zip.gz 

-C:指定解压结果的目录

bash 复制代码
[root@oldboy opt]# tar -zxf etc.zip.gz -C /home/oldboy/
[root@oldboy opt]# ls /home/oldboy/
etc

1.4 其他参数

-p:保持文件属性

--exclude=文件名:打包时排除文件

bash 复制代码
[root@oldboy opt]# ll
总用量 10756
-rw-r--r--. 1 root root 11006560 1月   1 19:08 etc.zip.gz
-rw-r--r--. 1 root root        0 1月   1 20:12 file1
-rw-r--r--. 1 root root        0 1月   1 20:12 file2
-rw-r--r--. 1 root root        0 1月   1 20:12 file3
-rw-r--r--. 1 root root        0 1月   1 20:12 file4
-rw-r--r--. 1 root root        0 1月   1 20:12 file5
-rw-r--r--. 1 root root      200 1月   1 20:13 test.zip.gz
[root@oldboy opt]# tar -zcvf ~/test.zip.gz ./ --exclude=*.gz
./
./file1
./file2
./file3
./file4
./file5
[root@oldboy opt]# ls ~
anaconda-ks.cfg  file1   file2  file4  file6  file8  rm        test.zip.gz
a.txt            file10  file3  file5  file7  file9  test.txt  uniq
[root@oldboy opt]# tar tf ~/test.zip.gz 
./
./file1
./file2
./file3
./file4
./file5

-X:从文件中排除指定的文件

bash 复制代码
[root@oldboy opt]# ls test
etc.zip.gz  file1  file2  file3  file4  file5
[root@oldboy opt]# ll
总用量 4
drwxr-xr-x. 2 root root 89 1月   1 20:28 test
-rw-r--r--. 1 root root 11 1月   1 20:29 test.log
[root@oldboy opt]# tar -zcvf ./test.zip.gz ./test/ -X test.log 
./test/
./test/file1
./test/file2
./test/file3
./test/file4
./test/file5
[root@oldboy opt]# ll
总用量 8
drwxr-xr-x. 2 root root  89 1月   1 20:28 test
-rw-r--r--. 1 root root  11 1月   1 20:29 test.log
-rw-r--r--. 1 root root 186 1月   1 20:37 test.zip.gz
[root@oldboy opt]# tar -tf test.zip.gz 
./test/
./test/file1
./test/file2
./test/file3
./test/file4
./test/file5

-h:打包软链接


2.date 日期

工作中的服务器的时间是定时和互联网时间同步的。

-s:修改日期或时间

bash 复制代码
[root@oldboy opt]# date +%Y-%m-%d\ %H:%M:%S
2025-01-01 21:30:04
#空格前需要转义字符
#简写:
[root@oldboy opt]# date +%F\ %T
2025-01-01 21:34:12
#周几
[root@oldboy opt]# date +%w
3

-d:显示指定的时间

3.企业案例1

每天打包/etc目录,到/backup目录备份,要求压缩包的名字按照每天的日期命名。

bash 复制代码
[root@oldboy opt]# tar -zcf ./etc_`date +%F`.zip.gz /etc
tar: 从成员名中删除开头的"/"
[root@oldboy opt]# ll
总用量 10760
-rw-r--r--. 1 root root 11006567 1月   1 21:47 etc_2025-01-01.zip.gz
drwxr-xr-x. 2 root root       89 1月   1 20:28 test
-rw-r--r--. 1 root root       11 1月   1 20:29 test.log
-rw-r--r--. 1 root root      186 1月   1 20:37 test.zip.gz

4.企业案例2

查找/etc下,扩展名是.conf的所有文件,打包备份到/backup,文件名含有日期。

方法一:

bash 复制代码
[root@oldboy backup]# tar -zcf ./bak_etc_conf_$(date +%F).zip.gz `find /etc -type f -name *.conf`
tar: 从成员名中删除开头的"/"
[root@oldboy backup]# ll
总用量 76
-rw-r--r--. 1 root root 75908 1月   1 22:01 bak_etc_conf_2025-01-01.zip.gz

方法二:

bash 复制代码
[root@oldboy backup]# find /etc -name *.conf | xargs tar -zcf /backup/conf_bak_$(date +%F).zip.gz 
[root@oldboy backup]# ll
总用量 152
-rw-r--r--. 1 root root 75908 1月   1 22:01 bak_etc_conf_2025-01-01.zip.gz
-rw-r--r--. 1 root root 76551 1月   1 22:10 conf_bak_2025-01-01.zip.gz

xargs:将管道前的数据作为管道后命令的参数

5.命令测试题

老男孩Linux77期第二周 周末基础命令测试题:

01)请在/opt下创建oldboyedu目录,并在oldboyedu目录下创建dir1到dir5,共5个目录。

02)请用一条命令完成创建多级目录/opt/oldboy/data/test123。

03)请在/opt/oldboyedu目录下创建f1到f5,共5个文件。

04)查看/opt下的完整目录结构,并只显示子目录目录。

05)切换到/opt/oldboyedu目录下,并打印当前所在的路径。

06)编辑f1文件,增加"老男孩教育77期,历史最优班级"内容。

07)使用重定向追加的方式给f2追加内容,"I am 30."。

08)使用cat命令配合重定向,追加如下内容到f3.

192.168.0.12

192.168.0.22

192.168.0.25

192.168.0.12

192.168.0.25

192.168.0.25

192.168.0.38

09)把/etc/passwd内容重定向到f4里面,并过滤出含有halt的行。

10)浏览f4内容,并查看行号。

11)浏览文件头部2行,和尾部2行。并跟踪f4文件尾部的变化。

12)把oldboyedu目录复制到/opt/oldboy/data下

13)删除/opt/oldboy/data下的test123目录

14)对f3文件按ip地址排序,并且生成新文件ip.txt

15)统计f3文件中重复的IP,并生成新文件chongfu.txt

16)打包oldboyedu目录,到备份目录/backup下,并按日期命名压缩包。

17)删除/backup下7天前的扩展名为.tar.gz的所有文件。

18)打包oldboyedu目录下,所有以f开头的文件,并移动到/opt下的data目录。

19)为重启网络命令设置别名net。并取消系统的cp别名。

20)清空所有历史记录,删除/opt下所有内容。

相关推荐
用户9718356334662 小时前
银河麒麟 KY10 申威(SW64) 安装 nginx-1.16.1-2.p01.ky10.sw_64.rpm 详细步骤
linux
猪脚踏浪3 小时前
linux 拷贝文件或目录到指定的位置
linux
大树8819 小时前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠19 小时前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质20 小时前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
bush420 小时前
嵌入式linux学习记录十四、术语
linux·嵌入式
载数而行52020 小时前
Linux 11 动态监控指令top
linux
小宇宙Zz20 小时前
Maven依赖冲突
java·服务器·maven
Inhand陈工21 小时前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
酣大智21 小时前
ARP代理--工作原理
运维·网络·arp·arp代理