tar command
$ tar [-cxtzjvfpPN] files or directories
Arguments:
-
-c
: Create a new archive containing the specified items. -
-x
: Extract to disk from the archive. -
-t
: List archive contents.
Notice:
-c/x/t
Only one can be used.
-
-z
: Compress the resulting archive with gzip. In extract or list modes, this option is ignored. -
-j
: Compress the resulting archive with bzip2. In extract or list modes, this option is ignored. -
-v
: In create and extract modes, tar will list each file name as it is read from or written to the archive. In list mode, tar will produce output similar to that of ls. -
-f
: Read the archive from or write the archive to the specified file.$ tar -zcvfP tfile sfile # Incorrect $ tar -zcvPf tfile sfile # Correct
-
-p
: (x mode only) Preserve file permissions.
Examples:
-
Tarring all files in
/etc
to/tmp/etc.tar
$ tar -cvf /tmp/etc.tar /etc # No compressing $ tar -zcvf /tmp/etc.tar.gz /etc # Compressing with gzip $ tar -jcvf /tmp/etc.tar.bz2 /etc # Compressing with bzip2
-
Listing files in
/tmp/etc.tar.gz
$ tar -ztvf /tmp/etc.tar.gz # Cause we compressed with gzip, the -z is necessary
-
Decompress
/tmp/etc.tar.gz
to/usr/local/src
$ cd /usr/local/src $ tar -zxvf /tmp/etc.tar.gz
-
Decompressing the
etc/passwd
in/tmp/etc.tar.gz
to/tmp
$ cd /tmp $ tar -zxvf /tmp/etc.tar.gz etc/passwd
-
Tarring all files in
/etc/
, and keeping their permission$ tar -zxvpf /tmp/etc.tar.gz /etc
-
Only the files later than 2005/06/01 will be tarred into
/home
$ tar -N '2005/06/01' -zcvf home.tar.gz /home
-
To tar
/home
,/etc
excluding/home/dmtsai
$ tar --exclude /home/dmtsai -zcvf myfile.tar.gz /home/* /etc
网友评论