【每天一个Linux命令之---tar】

文件备份和压缩命令

1、tar命令:将多个文件打包在一起,解压打包的文件。打包在将多个文件或目录变成一个总的文件,压缩是将一个大的文件通过压缩算法变成一个小文件。
2、掌握最常用的那几个选项

参数选项:
z 通过gzip压缩或解压
c 创建新的tar包
v 显示详细的tar命令执行过程
f 指定压缩文件的名字
t 不解压查看tar包的内容
j 通过bzip2命令压缩或解压
x 解开tar包
C 指定解压路径 (大写C)
--exclude=PATTERN 打包时排除不需要的文件或目录
-h 打包软链接文件指向的真实源文件 
示例:
[root@centos02 tmp]# mkdir -p /dir/www/html/oldboy/test
[root@centos02 tmp]# touch /dir/www/html/{1..10}.html
[root@centos02 tmp]# ls /dir/www/html/
10.html  1.html  2.html  3.html  4.html  5.html  6.html  7.html  8.html  9.html  oldboy
[root@centos02 tmp]# cd /dir/www/
[root@centos02 www]# ls
html
[root@centos02 www]# tar zcvf www.tar.gz ./html/
./html/
./html/oldboy/
./html/oldboy/test/
./html/1.html
./html/2.html
./html/3.html
./html/4.html
./html/5.html
./html/6.html
./html/7.html
./html/8.html
./html/9.html
./html/10.html
[root@centos02 www]# ll www.tar.gz
-rw-r--r-- 1 root root 253 817 10:06 www.tar.gz
[root@centos02 www]# tar ztvf www.tar.gz    #不解压查看压缩包的内容
drwxr-xr-x root/root         0 2024-08-17 10:03 ./html/
drwxr-xr-x root/root         0 2024-08-17 10:02 ./html/oldboy/
drwxr-xr-x root/root         0 2024-08-17 10:02 ./html/oldboy/test/
-rw-r--r-- root/root         0 2024-08-17 10:03 ./html/1.html
-rw-r--r-- root/root         0 2024-08-17 10:03 ./html/2.html
-rw-r--r-- root/root         0 2024-08-17 10:03 ./html/3.html
-rw-r--r-- root/root         0 2024-08-17 10:03 ./html/4.html
-rw-r--r-- root/root         0 2024-08-17 10:03 ./html/5.html
-rw-r--r-- root/root         0 2024-08-17 10:03 ./html/6.html
-rw-r--r-- root/root         0 2024-08-17 10:03 ./html/7.html
-rw-r--r-- root/root         0 2024-08-17 10:03 ./html/8.html
-rw-r--r-- root/root         0 2024-08-17 10:03 ./html/9.html
-rw-r--r-- root/root         0 2024-08-17 10:03 ./html/10.html
[root@centos02 www]# tar tf www.tar.gz  #自动判定压缩包类型,自动调用gzip命令解压
./html/
./html/oldboy/
./html/oldboy/test/
./html/1.html
./html/2.html
./html/3.html
./html/4.html
./html/5.html
./html/6.html
./html/7.html
./html/8.html
./html/9.html
./html/10.html
[root@centos02 www]# mkdir /tmp/dir2/ | tar zxvf www.tar.gz -C /tmp/dir2/  #解压到指定目录
./html/
./html/oldboy/
./html/oldboy/test/
./html/1.html
./html/2.html
./html/3.html
./html/4.html
./html/5.html
./html/6.html
./html/7.html
./html/8.html
./html/9.html
./html/10.html
为看方便,--exclude的后接路径和打包路径应保持形式一致,要么都是相对路径,要么都是绝对路径。
[root@centos02 dir2]# tar zcvf etc.tar.gz `find etc/ -type f`
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值