压缩和解压命令 tar, gzip, gunzip, bzip2

1. tar打包命令

  • tar cf target.tar [file1] [file2] ... :  将多个文件打包成target.tar(文件名可修改), 文件也可以是目录, v参数表示列出所有文件。
➜  linux_commands tar cf hello.tar hello*
➜  linux_commands ls hello*
hello.tar        hello.txt        hello_sorted.txt
➜  linux_commands rm hello.tar
➜  linux_commands tar cvf hello.tar hello*
a hello.txt
a hello_sorted.txt
➜  linux_commands tar cvf temp.tar temp hello.txt
a temp
a temp/bb
a temp/three
a temp/input.txt
a temp/hello.tar.xz
a temp/two
a temp/aa
a temp/hello.tar.gz
a temp/cc
a temp/hello_sorted.txt
a temp/hello.txt
a temp/aa/hello_sorted.txt
a temp/aa/hello.txt
a hello.txt
  • tar czf target.tar.gz [file1] [file2] ... : 将多个文件压缩成target.tar.gz(文件名可修改,gz表示用gzip压缩)
➜  linux_commands tar czf hello.tar.gz hello.txt hello_sorted.txt
➜  linux_commands ls hello*
hello.tar.gz     hello.txt        hello_sorted.txt
  • tar tvf source.tar: 列出tar格式打包文件的内容,t参数表示列出打包文件内容
➜  linux_commands tar tvf hello.tar
-rw-r--r--  0 qiushye staff      92 May 19 12:30 hello.txt
-rw-r--r--  0 qiushye staff      60 May 15 12:16 hello_sorted.txt
  • tar xf source.tar[.gz|.bz2|.xz] -C [path]: 将打包文件提取到当前目录,中括号内的表示其他格式文件, -C 表示提取到目标路径
➜  temp ls hello*
hello.tar.gz
➜  temp tar xf hello.tar.gz
➜  temp ls hello*
hello.tar.gz     hello.txt        hello_sorted.txt
➜  temp tar xf hello.tar.gz -C aa
➜  temp ls aa
hello.txt        hello_sorted.txt

2. gzip 压缩和解压命令

  • gzip [file]: 压缩文件[file],并以压缩文件[file.gz]替代原文件
  • gzip -d [file.gz]: 解压[file.gz],并以原文件代替压缩文件
➜  linux_commands gzip hello.txt
➜  linux_commands ls hello.txt*
hello.txt.gz
➜  linux_commands gzip -d hello.txt.gz
➜  linux_commands ls hello.txt*
hello.txt
  • gzip -c [file] > [file1.gz]: 压缩文件[file],并将压缩名设置为file1.gz,解压操作也可以用-c参数
➜  linux_commands gzip -c hello.txt > hello_1.gz
➜  linux_commands ls hello_1.gz
hello_1.gz
  • gzip -3 [file]: 指定压缩等级,1表示最快(最差),9表示最慢(最好),默认等级为6
➜  linux_commands gzip -5 hello.txt
➜  linux_commands ls -l hello.txt.gz
-rw-r--r--  1 qiushye  staff  81 May 19 12:30 hello.txt.gz
  • gzip -lv [file.gz]: 展示压缩文件内的列表详情,包括压缩方法,时间,压缩率等
➜  linux_commands gzip -lv hello.txt.gz
method  crc     date  time    compressed uncompressed  ratio uncompressed_name
defla 50b8a0b9 May 19 12:30           81           92  13.0% hello.txt

3. gunzip从gz压缩包提取文件

  • gunzip [file.tar.gz]: 从压缩包[file.tar.gz]中提取文件,若有原文件则替换
➜  linux_commands tar czf combine.tar.gz hello.txt test.txt
➜  linux_commands tar tvf combine.tar.gz
-rw-r--r--  0 qiushye staff      92 May 19 12:30 hello.txt
-rw-r--r--  0 qiushye staff      28 May 27 12:36 test.txt
➜  linux_commands gunzip combine.tar.gz
➜  linux_commands ls combine*
combine.tar
  • gunzip -c [file.tar.gz] > [target.tar]: 从压缩包[file.tar.gz]中提取为文件[target.tar]
➜  linux_commands gunzip -c combine.tar.gz > ttt1.tar
➜  linux_commands ls ttt*
ttt1.tar
  • gunzip -l [file.gz]:展示压缩包的内容
➜  linux_commands gunzip -l hello.txt.gz
  compressed uncompressed  ratio uncompressed_name
          81           92  13.0% hello.txt

4. bzip2块排序压缩

  • bzip2 [file]: 压缩文件[file],以bz2为压缩后缀, 加-1...到-9分别表示块大小从100k,200k到900k
➜  linux_commands bzip2 test.txt
➜  linux_commands ls test.txt*
test.txt.bz2
➜  linux_commands bzip2 -3 regex.txt
➜  linux_commands
  • bzip2 -d [file.bz2]: 解压压缩文件, -f表示强制覆盖输出文件
➜  linux_commands bzip2 -d test.txt.bz2
➜  linux_commands ls test.txt*
test.txt

➜  linux_commands bzip2 test.txt
➜  linux_commands echo "aa" > test.txt
➜  linux_commands ls test.txt*
test.txt     test.txt.bz2
➜  linux_commands bzip2 -d test.txt.bz2
bzip2: Output file test.txt already exists.
➜  linux_commands bzip2 -df test.txt.bz2
➜  linux_commands
  • bzip2 -dc [file.bz2]: 解压压缩文件到标准输出, -c表示标准输出
➜  linux_commands bzip2 -dc test.txt.bz2
yes,no
white,black
boy,girl
➜  linux_commands ls test.txt*
test.txt.bz2

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Linux系统中,解压压缩文件有多种命令可以使用。对于压缩文件,常见的命令有: 1. ".zip"格式的压缩命令是使用"zip"命令,例如:zip 压缩文件名.zip压缩的文件或目录。 2. ".gz"格式的压缩命令是使用"gzip"命令,例如:gzip压缩的文件。 3. ".bz2"格式的压缩命令是使用"bzip2"命令,例如:bzip2 要压缩的文件。 4. ".tar"格式的压缩命令是使用"tar"命令,例如:tar -cvf 压缩文件名.tar压缩的文件或目录。 对于解压缩文件,常见的命令有: 1. ".zip"格式的解压命令是使用"unzip"命令,例如:unzip 压缩文件名.zip。 2. ".gz"格式的解压命令是使用"gunzip"命令,例如:gunzip 压缩文件。 3. ".bz2"格式的解压命令是使用"bunzip2"命令,例如:bunzip2 压缩文件。 4. ".tar"格式的解压命令是使用"tar"命令,例如:tar -xvf 压缩文件名.tar。 需要注意的是,压缩文件的扩展名对于Linux系统来说并不是必需的,但为了方便管理和操作,建议给压缩文件添加正确的扩展名。此外,Linux系统支持的压缩格式还有很多,如".7z"、".rar"等,具体的解压命令可以根据不同的格式进行调整。 #### 引用[.reference_title] - *1* *2* *3* [Linux常用命令——压缩解压命令](https://blog.csdn.net/m0_59868866/article/details/126237042)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值