This article used to walk you through some commonly tar
usages , based on a real life scenario.
################################################################ # Date Description # 05/29/2019 vim tar files # 05/29/2019 extract single file to another directory # 05/28/2019 extract file to another directory # 05/23/2019 extract single file from archive # 04/21/2019 untar keep owner and permission # 02/27/2019 untar to specified folder # 02/22/2019 list tar content # 02/21/2019 tar exclude # 02/20/2019 untar multiple files # 02/19/2019 tar multiple files # ################################################################
Sometimes I see people use -czf
but sometimes czf
, dash or not to pass flags?
Historical and compatible reason, no dash version is probably more portable.
tar
is one of those ancient commands from the days when option syntax hadn’t been standardized. Because all useful invocations of tar
require specifying an operation before providing any file name, most tar
implementations interpret their first argument as an option even if it doesn’t begin with a -
. Most current implementations accept a -
.
注意, 这里的例子大多是用的old option style for compatibility. For example czf
this set of letters must be the first to appear on the command line, after the tar program name and some white space; old options cannot appear anywhere else.
还要注意,当前pwd是/tmp
然后运行tar
,则tar
的结果就在/tmp
, 和-C
无关, -C
option只是在执行中暂时去指定的位置。
02/19/2019
Basic operation: tar multiple files into example.tar.gz
1 | ## use -C to go to target directory |
The file path matters! see my blog.
02/20/2019
When untar multiple files, you cannot do this, it will fail
1 | tar zxf file1.tar.gz file2.tar.gz file3.tar.gz |
The reason please see this link, the solution is to use xargs
instead:
1 | # -I: specify replace-str |
Or you can use find
with -exec
1 | find . -maxdepth 1 -name "*.tar.gz" -exec tar zxf '{}' \; |
02/21/2019
For example, if you want to tar things inside a folder foler1
but excluding some files:
1 | ## 注意最后的`.` 目标必须放最后 |
If you don’t exclude folder1.tar.gz
, it will tar itself again.
02/22/2019
List tar.gz file content, flag z
is used to distinguish tar and tar.gz
1 | tar tvf target.tar |
02/27/2019
If you don’t specify target folder, untar will put things in current directory, use -C
option to specify it. For example, I want to untar source.tar.gz
to /etc/yum.repos.d/
folder:
1 | tar zxf /tmp/source.tar.gz -C /etc/yum.repos.d/ |
For -C
option, in c
and r
mode, this changes the directory before adding the following files. In x
mode, change directories after opening the archive but before extracting entries from the archive.
04/21/2019
When unpacking, consider using p
option to perserve file permissions. Use this in extract mode to override your umask
and get the exact permissions specified in the archive… The p
option is the default when working as the superuser, it will get what it has. If you are a regular user, add p
to keep permissions.
1 | tar zxpf target.tar.gz |
It seems umask ignores execute bit? When I untar the file with rwxrwxrwx
permission inside by regular user with umask 0002
, the final permission is rwxrwxr-x
.
if you want to keep owner as well:
1 | tar --same-owner -zxpf target.tar.gz |
Note that there is a
-
beforezxpf
.
05/23/2019
Extract specific files from tarball to current directory:
1 | tar xzf target.tar.gz file1 file2 |
Note that no leading
/
in the path (it uses relative path in tar file!), you can usetar xtvf target.tar.gz
to check the path.
05/28/2019
tar
by default extracts file to current directory, if you want to place the untar files to another directory, run:
1 | tar zxf target.tar.gz -C /target/directory |
Note that the target directory has to exist before running that command.
05/29/2019
If you want to extact files to another directory:
1 | ## file1 and file2 put at end |
11/12/2020
Latest VIM support edit on tar file:
1 | ## then select file in dashboard, edit and save normally |