范例一:將整個 test 目錄下的文件全部打包成為 test.tar
[python@master ~]$ tar -cvf test.tar test/ ==僅打包,不壓縮!
test/
test/a1
test/a2
test/z.txt
[python@master ~]$ tar -zcvf test.tar.gz test/ ==打包后,以 gzip 壓縮
test/
test/a1
test/a2
test/z.txt
[python@master ~]$ tar -jcvf test.tar.bz2 test/ ==打包后,以 bzip2 壓縮
test/
test/a1
test/a2
test/z.txt
# 特別注意,在參數 f 之后的文件檔名是自己取的,我們習慣上都用 .tar 來作為辨識。
# 如果加 z 參數,則以 .tar.gz 或 .tgz 來代表 gzip 壓縮過的 tar file ~
# 如果加 j 參數,則以 .tar.bz2 來作為附檔名啊~
drwxrwxr-x 2 python python 39 10月 25 11:40 test
-rw-rw-r-- 1 python python 10240 10月 25 11:41 test.tar
-rw-rw-r-- 1 python python 174 10月 25 11:44 test.tar.gz
-rw-rw-r-- 1 python python 181 10月 25 11:47 test.tar.bz2
打包壓縮節省空間很大
范例二:查閱上述 tar 文件內有哪些文件?
[python@master ~]$ tar -tvf test.tar
drwxrwxr-x python/python 0 2018-10-25 11:40 test/
-rw-rw-r-- python/python 0 2018-10-25 11:12 test/a1
-rw-rw-r-- python/python 0 2018-10-25 11:12 test/a2
-rw-rw-r-- python/python 0 2018-10-25 11:13 test/z.txt
[python@master ~]$ tar -ztvf test.tar.gz
drwxrwxr-x python/python 0 2018-10-25 11:40 test/
-rw-rw-r-- python/python 0 2018-10-25 11:12 test/a1
-rw-rw-r-- python/python 0 2018-10-25 11:12 test/a2
-rw-rw-r-- python/python 0 2018-10-25 11:13 test/z.txt
[python@master ~]$
[python@master ~]$ tar -jtvf test.tar.bz2
drwxrwxr-x python/python 0 2018-10-25 11:40 test/
-rw-rw-r-- python/python 0 2018-10-25 11:12 test/a1
-rw-rw-r-- python/python 0 2018-10-25 11:12 test/a2
-rw-rw-r-- python/python 0 2018-10-25 11:13 test/z.txt
范例三:將 tar 文件解壓縮在 /home/python/tmp 底下
方法一、
[python@master ~]$ cd tmp
[python@master tmp]$ tar -xvf /home/python/test.tar
方法二、
[python@master ~]$ tar -zxvf test.tar.gz -C /home/python/tmp
范例四:在 /tmp 底下,我只想要將 /home/python/test.tar.gz 內的 test/a1 解開而已
[python@master ~]$ cd tmp
[python@master tmp]$ tar -zxvf /home/python/test.tar.gz test/a1
# 我可以透過 tar -ztvf 來查閱 tarfile 內的文件名稱,如果單只要一個文件,
范例五:將 /etc/ 內的所有文件備份下來,並且保存其權限!
[root@linux ~]# tar -zxvpf /tmp/etc.tar.gz /etc
# 這個 -p 的屬性是很重要的,尤其是當您要保留原本文件的屬性時!
范例六:在 /home 當中,比 2005/06/01 新的文件才備份
[root@linux ~]# tar -N '2005/06/01′ -zcvf home.tar.gz /home
范例七:我要備份 /home, /etc ,但不要 /home/dmtsai
[root@linux ~]# tar –exclude /home/dmtsai -zcvf myfile.tar.gz /home/* /etc
各類打包解包類型
.tar
解包:tar xvf FileName.tar
打包:tar cvf FileName.tar DirName
(注:tar是打包,不是壓縮!)
———————————————
.gz
解壓1:gunzip FileName.gz
解壓2:gzip -d FileName.gz
壓縮:gzip FileName
.tar.gz 和 .tgz
解壓:tar zxvf FileName.tar.gz
壓縮:tar zcvf FileName.tar.gz DirName
———————————————
.bz2
解壓1:bzip2 -d FileName.bz2
解壓2:bunzip2 FileName.bz2
壓縮: bzip2 -z FileName
.tar.bz2
解壓:tar jxvf FileName.tar.bz2 或tar –bzip xvf FileName.tar.bz2
壓縮:tar jcvf FileName.tar.bz2 DirName
———————————————
.bz
解壓1:bzip2 -d FileName.bz
解壓2:bunzip2 FileName.bz
壓縮:未知
.tar.bz
解壓:tar jxvf FileName.tar.bz
壓縮:未知
———————————————
.Z
解壓:uncompress FileName.Z
壓縮:compress FileName
.tar.Z
解壓:tar Zxvf FileName.tar.Z
壓縮:tar Zcvf FileName.tar.Z DirName
———————————————
.zip
解壓:unzip FileName.zip
壓縮:zip FileName.zip DirName
壓縮一個目錄使用 -r 參數,-r 遞歸。例: $ zip -r FileName.zip DirName
———————————————
.rar
解壓:rar x FileName.rar
壓縮:rar a FileName.rar DirName