菜鸟学Linux 第024篇笔记 压缩,tar,read,script
压缩格式:
gz, bz2, xz, zip, z
压缩算法: 算法不同,压缩比也会不同;
command
compress: FILENAME.Z
uncompress
gzip: .gz
压缩会删除原文件
-d = gunzip
-# --fast --best
Regulate the speed of compression using the specified
digit #, where -1 or --fast indicates the fastest
compression method (less compression) and -9 or
--best indicates the slowest compression method (best
compression). The default compression level is -6
(that is, biased towards high compression at expense
of speed).
gunzip /path/name.gz
解压缩会删除原文件
zcat 可以用来查看压缩的文本文档.gz
bzip2: .bz2
压缩会删除原文件
比gzip拥有更大压缩比的压缩工具,使用近似gzip(压缩大文件的时候)
-d
-#
-k --keep
Keep (don’t delete) input files during compression or
decompression.
bunzip2 解压bz2文件
bzcat 查看压缩文档文档.bz2
xz (压缩比更大)有的系统没有需要安装yum install xz
压缩会删除原文件
-d
-#
-k
unxz
xzdec
xzcat
zip(压缩比不大,但可以支持压缩目录)
注意:这个压缩是不会删除原文件的!!!
zip, zipcloak, zipnote, zipsplit - package and compress
(archive) files
unzip 解压
e.g.
zip name.zip test/*
只归档不压缩
command
tar ( tar - The GNU version of the tar archiving utility)
[-]c --create
[-]x --extract --get
-f, --file F
-t, --list
list the contents of an archive
-v, --verbose
verbosely list files processed
--xattrs (保留文件的扩展属性信息)
this option causes tar to store each file’s extended
attributes in the archive. This option also enables
--acls and--selinux if they haven’t been set already,
due to the fact that the data for those are stored in
special xattrs.
-z, --gzip, --gunzip, --ungzip
filter the archive through gzip
-j, --bzip2
filter archive through bzip2, use to decompress .bz2
files
-J --xz(用最新的xz压缩或解压缩归档文件但需要tar版本)
e.g.
tar -tf name.tar.gz
查看压缩归档文件包含内容
tar -xvf foo.tar
verbosely extract foo.tar
tar -xzf foo.tar.gz
extract gzipped foo.tar.gz
tar -cjf foo.tar.bz2 bar/
create bzipped tar archive of the directory bar
called foo.tar.bz2
tar -xjf foo.tar.bz2 -C bar/
extract bzipped foo.tar.bz2 after changing directory
to bar
tar -xzf foo.tar.gz blah.txt
extract the file blah.txt from foo.tar.gz
cpio (cpio - copy files to and from archives)归档工具
read 命令
可以从用户接下来所输入的字符保存到变量里
e.g.
read A B
1 3 则会将1保存到A变量 3保存到B变量
写脚本
1.从键盘让用户输入几个文件,脚本能够将此几个文件归档压缩成一个文件,
并且可以自定义压缩方式;
2.检查某个用户,当用户登录时显示该用户已经登录;
KEY
1. #!/bin/bash
#
read -p "Three files:" FILE1 FILE2 FILE3
read -p "Save direcotry:" DEST
read -p "Compression type:{gzip|bzip2}" STY
case $STY in
gzip)
tar -zcf ${DEST}.tar.gz $FILE1 $FILE2 $FILE3
;;
bzip2)
tar -jcf ${DEST}.tar.bz2 $FILE1 $FILE2 $FILE3
;;
*)
echo "Unknown."
exit 20
;;
esac
2. #!/bin/bash
#
who | grep "tom" &> /dev/null
REC=$?
while [ $REC -ne 0 ]; do
echo "`date`, tom not in home"
sleep 5
who | grep "tom" &> /dev/null
REC=$?
done
echo "tom is at home!!"