Tar: Difference between revisions
(5 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
* [[Linux#Commands|Linux]] | * [[Linux#Commands|Linux]] | ||
=Overview= | |||
<syntaxhighlight lang='bash'> | |||
tar -cvf <archive-name> <files> | |||
</syntaxhighlight> | |||
<syntaxhighlight lang='bash'> | |||
tar -xf <archive-name> | |||
</syntaxhighlight> | |||
It is important that <archive-name> follows immediately after -f. | |||
=Handling Symbolic Links= | =Handling Symbolic Links= | ||
{{External|https://www.gnu.org/software/tar/manual/html_node/dereference.html}} | {{External|https://www.gnu.org/software/tar/manual/html_node/dereference.html}} | ||
By default, tar handles archiving symbolic links by writing a block to the archive with the name of the target of the link, thus preserving the symbolic link. However, if the -h|--dereference option is used when creating the archive, tar archives the content of the file the symbolic link points to instead of the symbolic link information. | By default, tar handles archiving symbolic links by writing a block to the archive with the name of the target of the link, thus preserving the symbolic link. However, if the [[#-h.7C--dereference|-h|--dereference]] option is used when creating the archive, tar archives the content of the file the symbolic link points to instead of the symbolic link information. | ||
To create a portable archive aimed at systems that do not support symbolic links, use [[#-h.7C--dereference|-h|--dereference]]. | |||
=Handling Hard Links= | =Handling Hard Links= | ||
Line 31: | Line 45: | ||
===-C=== | ===-C=== | ||
Source directory. | Source directory when creating the archive. Target directory when extracting the archive. | ||
For | |||
<syntaxhighlight lang='text'> | |||
/ | |||
└── home | |||
└── someuser | |||
└── A | |||
├── X | |||
├── Y | |||
└── Z | |||
└── somefile.txt | |||
</syntaxhighlight> | |||
<syntaxhighlight lang='bash'> | |||
tar -cvf test.tar -C /home/someuser/A Z | |||
</syntaxhighlight> | |||
will produce a test.tar that contains: | |||
<syntaxhighlight lang='text'> | |||
Z | |||
└── somefile.txt | |||
</syntaxhighlight> | |||
===-h|--dereference=== | ===-h|--dereference=== | ||
Line 70: | Line 104: | ||
gunzip < ... | tar xfv - some/specific/file.txt | gunzip < ... | tar xfv - some/specific/file.txt | ||
=Troubleshooting= | |||
With some versions of <code>tar</code> I get this: | |||
<syntaxhighlight lang='text'> | |||
tar -xfz /.../a.tar.gz -C /usr/lib/jvm/ | |||
tar: z: Cannot open: No such file or directory | |||
</syntaxhighlight> | |||
The problem goes away if I don't use "-". |
Latest revision as of 00:04, 26 June 2021
Internal
Overview
tar -cvf <archive-name> <files>
tar -xf <archive-name>
It is important that <archive-name> follows immediately after -f.
Handling Symbolic Links
By default, tar handles archiving symbolic links by writing a block to the archive with the name of the target of the link, thus preserving the symbolic link. However, if the -h|--dereference option is used when creating the archive, tar archives the content of the file the symbolic link points to instead of the symbolic link information.
To create a portable archive aimed at systems that do not support symbolic links, use -h|--dereference.
Handling Hard Links
Options
Archival
When tar is executed as root, the file permissions and owner are preserved.
-p
Preserve permissions, default when the root executes the command.
-s
Preserve order (same order). Sort names to extract to match archive.
-z
Compress, produce a tgz.archive.
-C
Source directory when creating the archive. Target directory when extracting the archive.
For
/
└── home
└── someuser
└── A
├── X
├── Y
└── Z
└── somefile.txt
tar -cvf test.tar -C /home/someuser/A Z
will produce a test.tar that contains:
Z
└── somefile.txt
-h|--dereference
See Handling Symbolic Links above.
Archive
Archive maintaining the user IDs and file permissions:
tar -cvspf .../archive.tar *
Note that the name of the archive file to be created must immediately follow -f, without any interceding parameters.
To also gzip:
tar cfv - ./mydir | gzip > mydir.tgz
Exclude Specific Files from Archive
tar cf <file-name.tar> --exclude <pattern> <dir>
Do not process files or directories that match the specified pattern. Note that exclusions take precedence over patterns or filenames specified on the command line.
Example:
tar cfv module.tar --exclude */Jenkinsfile --exclude */jenkins module/
Restoration
Restore preserving the user ID and file permissions (if done as root, the behavior is implicit):
tar -xvspf .../archive.tar
Extract a Specified File
tar -xf archive.tar some/specific/file.txt gunzip < ... | tar xfv - some/specific/file.txt
Troubleshooting
With some versions of tar
I get this:
tar -xfz /.../a.tar.gz -C /usr/lib/jvm/
tar: z: Cannot open: No such file or directory
The problem goes away if I don't use "-".