美文网首页Linux小推车
Linux Day27:Linux 压缩、解压缩命令

Linux Day27:Linux 压缩、解压缩命令

作者: 泥人吴 | 来源:发表于2018-11-17 15:57 被阅读30次

压缩格式:

  • 格式:gz,bz2,xz,zip,z
  • 只能压缩文件,压缩后默认会删除源文件

gzip

gzip: .gz
gzip /PATH/to/somefile 
  -d: 表示解压缩的意思(gunzip)
  -#:1-9,指定压缩比,默认为6

gunzip:
gunzip /PATH/to/somefile .zp

bzip2: .bz2

  • 比gzip有更大压缩比的压缩工具,使用格式相似
bzip2  /PATH/to/somefile 
  -d: 表示解压缩的意思
  -k: 压缩时保留源文件
  -#:1-9,指定压缩比,默认为6

bunzip2:

xz: .xz

  • 用法一样
NAME
       xz, unxz, xzcat, lzma, unlzma, lzcat - Compress or decompress .xz and .lzma files

SYNOPSIS
       xz [option]...  [file]...

cat/zcat

  • zcat /PATH/to/somefile.gz:不解压的情况,查看文本文件的内容:

zip:归档+压缩

  • 具有压缩目录的功能
  • zip filename,zip file1 file2,多个文件都可压缩到filename.zip中,具有归档的意义。
  • archive:归档,归档本身不意味着压缩。

tar:

 只归档,不压缩
  -c :创建归档文件
  -f file.tar:操作的归档文件
  -x:展开归档文件
  --xattrs:归当时,保留文件的扩展属性信息。
  
  -zcf:归档并调用gzip压缩
  -zxf:调用gzip解压缩并展开归档,-z此处可省略

  -jcf:调用bzip2
  -jxf: 

  -Jcf:  调用xz
  -Jxf:

  -t:不展开归档,直接查看归档的文件,和-f 一起用。
  -jtf:展开
tar -cf test.tar test*.txt
tar -xf test.tar
  • cpio:归档工具

read

ubuntu@VM-0-3-ubuntu:~$ ./sum.sh 
Input two intergers:2 3
5
ubuntu@VM-0-3-ubuntu:~$ cat sum.sh 
#!/bin/bash
#
echo -n "Input two intergers:"
read A B
echo "$[ $A + $B ]"

使用read -p
Input two intergers:2 3
5
ubuntu@VM-0-3-ubuntu:~$ cat sum.sh 
#!/bin/bash
#
#echo -n "Input two intergers:"
read -p "Input two intergers:" A B
echo "$[ $A + $B ]"
  • 写一个脚本,压缩三个文件大一个文件中:
# 首先查看文件中的内容
ubuntu@VM-0-3-ubuntu:~$ ls
a                c                    second.sh.save   sh06.sh.new.new   test.bed.new.new
adduser.sh       cap.sh               sed.sh.new.new   sh07.sh.new.new   test.new.new
awk.new.new      case.sh              sh03.sh          shift.sh.new.new  third.sh.new.new
b                d.new.new            sh04.sh.new.new  sum.sh
biosoft.new.new  filetest.sh.new.new  sh05.sh.new.new  test1.new.new

# 编写脚本
ubuntu@VM-0-3-ubuntu:~$ cat maycar.sh 
# !/bin/bash
#
read -p "Three files:" file1 filie2 file3
read -p "Destination:" DEST

tar -jcf ${DEST}.tar.bz2 $file1 $file2 $file3

# 执行脚本:
ubuntu@VM-0-3-ubuntu:~$ ./maycar.sh 
Three files:cap.sh case.sh sh03.sh
Destination:DEST
ubuntu@VM-0-3-ubuntu:~$ ls
a                c             filetest.sh.new.new  sh04.sh.new.new   sum.sh
adduser.sh       cap.sh        maycar.sh            sh05.sh.new.new   test1.new.new
awk.new.new      case.sh       second.sh.save       sh06.sh.new.new   test.bed.new.new
b                DEST.tar.bz2  sed.sh.new.new       sh07.sh.new.new   test.new.new
biosoft.new.new  d.new.new     sh03.sh              shift.sh.new.new  third.sh.new.new

# 展开压缩文件查看:
ubuntu@VM-0-3-ubuntu:~$ tar -tf DEST.tar.bz2 
cap.sh
sh03.sh
# 为什么只有两个文件呢?
  • 将上面的脚本升级,为用户提供可选择的压缩方式:
# !/bin/bash
#
read -p "Three files:" file1 filie2 file3
read -p "Destination:" DEST
resd -p "Compress[gzip|bzip2|xz]:" comp

case $comp in
gzip)
  tar -zcf ${DEST}..tar.bz2 $file1 $file2 $file3 ;;
bzip2)
  tar -jcf ${DEST}.tar.bz2 $file1 $file2 $file3 ;;
xz)
  tar -cf ${DEST}.tar.bz2 $file1 $file2 $file3
  xz ${DEST}.tar ;;
*) 
  echo "Unkown"
  exit 7
  ;;
esac

复习脚本编程

顺序结构
选择结构
  if
  case
循环结构
  for
  while
  until

生信技能树公益视频合辑:学习顺序是linux,r,软件安装,geo,小技巧,ngs组学!
请猛戳下面链接
B站链接:https://m.bilibili.com/space/338686099

YouTube链接:https://m.youtube.com/channel/UC67sImqK7V8tSWHMG8azIVA/playlists

生信工程师入门最佳指南:https://mp.weixin.qq.com/s/vaX4ttaLIa19MefD86WfUA

学徒培养:https://mp.weixin.qq.com/s/3jw3_PgZXYd7FomxEMxFmw

相关文章

网友评论

    本文标题:Linux Day27:Linux 压缩、解压缩命令

    本文链接:https://www.haomeiwen.com/subject/isbbfqtx.html