Tar Command Daily Work Summary

This article used to walk you through some commonly tar usages , based on a real life scenario.

################################################################
#   Date           Description
#   02/28/2019     dash or not
#   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
#
################################################################

02/19/2019

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

tar czf example.tar.gz file1 file2 file3 …

02/20/2019

When untar multiple files, you cannot do this, it will fail

tar zxf file1 file2 file3

The reason please see this link, the solution is to use xargs instead:

ls *.tar.gz | xargs -i tar xzf {}

Or you can use find with -exec

find . -maxdepth 1 -name "*.tar.gz" -exec tar zxf '{}' \;

02/21/2019

For example, if you want to tar things inside a folder but exclude some files

tar czf target.tar.gz ./* --exclude='createInstallerTar.sh' --exclude="target.tar.gz" --exclude='pushBIPayload.sh'

If you don’t exclude target.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

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 file to /etc/yum.repos.d/ folder:

tar zxf /tmp/source.tar.gz -C /etc/yum.repos.d/

02/28/2019

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

posted @ 2019-02-26 01:37  海胆阶段  阅读(167)  评论(0)    收藏  举报