Tar: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 37: Line 37:
  tar cfv - ./mydir | gzip > mydir.tgz
  tar cfv - ./mydir | gzip > mydir.tgz


==Exclude a Specific File from Archive==
==Exclude Specific Files from Archive==


  tar cf <''file-name''.tar> <''dir''>
  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=
=Restoration=

Revision as of 21:34, 3 January 2020

Internal

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.

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