菜鳥學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
-k
unxz
xzdec
xzcat
zip(壓縮比不大,但可以支援壓縮目錄)
注意:這個壓縮是不會删除原檔案的!!!
zip, zipcloak, zipnote, zipsplit - package and compress
(archive) files
unzip 解壓
e.g.
zip name.zip test/*
隻歸檔不壓縮
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 指令
可以從使用者接下來所輸入的字元儲存到變量裡
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!!"
本文轉自Winthcloud部落格51CTO部落格,原文連結http://blog.51cto.com/winthcloud/1870958如需轉載請自行聯系原作者
Winthcloud