First, demand: Compression /data to/ tmp
Operation:
1 [[email protected]/]# tar zcf/tmp/data.tar.gz data/ #压缩/data to/TMP and name the compressed package data.tar.gz 2 [[ Email protected]/]# ls-lrt/tmp/ #查看/tmp file 3 total 0 4-rw-r--r-- 1 root Root 490:wuyi data.tar.gz #成功
Second, demand: View the contents of the compressed package data.tar.gz
Operation:
1[Email protected]/]# tar tf/tmp/data.tar.gz #查看压缩包data. tar.gz content, below is the content (contains directories, files, and files under subdirectories)2data/3data/logs/4data/logs/access.log-20170426 5data/logs/access.log-20170430 6data/logs/access.log-20170427 7data/logs/access.log-20170505 8data/logs/access.log-20170429 9data/logs/access.log-20170425Tendata/logs/access.log-20170424 Onedata/logs/access.log-20170503 Adata/logs/Access.log -data/logs/access.log-20170101 -data/logs/access.log-20170508 thedata/logs/access.log-20170428 -data/logs/access.log-20170507 -data/logs/access.log-20170504 -data/logs/access.log-20170422 +data/logs/access.log-20170501 -data/logs/access.log-20170423 +data/logs/access.log-20170502 Adata/logs/access.log-20170506 atdata/Hello.txt -Data/nginx.conf
Third, the need: Unzip the package data.tar.gz to its directory
Operation:
1 [[email protected]/]# tar xf/tmp/data.tar.gz
Iv. needs: Decompression package data.tar.gz to/root
Operation:
1 [[email protected]/]# tar xf/tmp/data.tar.gz-c/root
V. Requirements: Compress /data to/ tmp , but do not include (exclude) hello.txt This file
Operation:
1 [[email protected]/]# tar zcf/tmp/data_pc.tar.gz data/--exclude=hello.txt #使用--exclude parameter exclusions hello.txt
VI. requirements: Compress /data to/ tmp , but do not include (exclude) the hello.txt;nginx.conf of these two files (excluding multiple files)
Operation:
1 [[email protected]/]# cat>>paichu.txt<<EOF #将hello. txt;nginx.conf Two File name written to Paichu.txt 2 > hello.txt3 > nginx.conf4 > EOF 5 [[email protected]/]# tar zcf/tmp/data_pc2.tar.gz data/--exclude-from =paichu.txt # Use--ecclude-from to exclude files in Paichu.txt
Summarize:
Use the tar command to compress and decompress as follows:
- Zip file: tar zcf [Compressed package path] [directory/file relative path to compress]
- View compressed files: Tar tf [compressed package path]
- Unzip the zip file: Tar XF [zipped package Path]
- Unzip to the specified path: Tar XF [Compressed package path]-c [Unzip Path]
- Compress a directory but exclude one of the files: tar zcf [Compressed package path] [to compress directory relative path]--exclude=[the relative path of the file name of the excluded files]
- Compress a directory but exclude multiple files from it:
- Create a text file Paichu.txt and write the name of the file you want to exclude.
- Then execute: Tar ZCF [Compressed package path] [to compress directory relative path]--exclude-from=[paichu.txt]
----------------------------------------------------Split Line------------------------------------------------------
Reference:
- Li Ling Teacher Classes
- Http://man.linuxde.net/tar
Simple compression-related operations under Linux