Tar Command Daily Work Summary

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无关, -Coption只是在执行中暂时去指定的位置。

02/19/2019

Basic operation: tar multiple files into example.tar.gz

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
## use -C to go to target directory
## target directory: the directory which contains file1/2/3
tar czf example.tar.gz -C <target directory> file1 file2 file3

## tar a directory as whole
## target directory: <folder name>'s parent folder
## untar 结果是<folder name>这个文件夹
tar czf example.tar.gz -C <target directory> <folder name>


# 如果只想打包某一文件夹内的内容, 则用-C 进入那个文件夹
## 但这样用tar tvf 查看,会有./ 前缀, 因为最后那个`.` 会展开显示所有hidden file,包括当前文件夹那个`.`
tar czf example.tar.gz -C <target directory> .

## 用`*`就没有./ 前缀,但是不会包含hidden file, 必须自己列出来
## 但这样用tar tvf 查看就没有前缀了
tar czf example.tar.gz -C <target directory> * .hidden1 .hidden2

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
2
3
# -I: specify replace-str
# {}: placeholder
ls *.tar.gz | xargs -I{} tar xzf {}

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
2
3
4
5
6
## 注意最后的`.` 目标必须放最后
## target directory: 进入
cd folder1
tar czf folder1.tar.gz --exclude="folder1.tar.gz" --exclude='file1' --exclude='file2' *
## if you want to have hidden files
tar czf folder1.tar.gz --exclude="folder1.tar.gz" --exclude='file1' --exclude='file2' * .file3 .file4

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
2
tar tvf target.tar
tar ztvf target.tar.gz

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 - before zxpf.

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 use tar 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
2
## file1 and file2 put at end
tar xzf target.tar.gz -C /target/directory file1 file2

11/12/2020

Latest VIM support edit on tar file:

1
2
## then select file in dashboard, edit and save normally
vim source.tar.gz
0%