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下所有内容。

相关推荐
Dxy123931021612 小时前
Linux 压缩解压相关命令介绍
linux·运维·服务器
eleven_4u12 小时前
QPS如何计算,如何部署架构
java·linux·运维·jvm·中间件
云飞云共享云桌面12 小时前
非标自动化工厂研发部门怎么实现一台服务器带10个SolidWorks设计并发
运维·服务器·人工智能·3d·自动化·电脑·制造
分布式存储与RustFS12 小时前
你的网站备份只存一块盘?把 1Panel 的备份目标换成 RustFS
运维·开源·备份·对象存储·1panel·s3·rustfs
遇见小修修12 小时前
打印机无法连接故障:操作误区汇总与排查教程
运维·服务器
东城居士12 小时前
理解Linux RTC驱动
linux·嵌入式系统
Elastic 中国社区官方博客12 小时前
使用 OpenTelemetry 在 Elastic 中实现 Claude 平台的人工智能 agent 可观测性
大数据·运维·服务器·人工智能·elasticsearch·信息可视化·全文检索
hzxxxz12 小时前
3个模块搭稳一条自动化链路:定时任务、幂等与对账
运维·自动化
蘑菇小白12 小时前
Linux多线程开发
linux·开发语言·线程
Ruiery13 小时前
Linux 6.6内核内存管理深度解析(十):KSM 同页合并 — 内容去重,把 N 份相同内存合并成 1 份
linux·运维·服务器