Difference between revisions of "Tar"

From ArchWiki
Jump to: navigation, search
(15 intermediate revisions by 8 users not shown)
Line 1: Line 1:
Copying a directory tree and its contents to another filesystem using
+
[[Category:Data compression and archiving]]
 +
From [http://www.gnu.org/software/tar/ GNU's Tar Page]:
 +
:''"The {{Ic|Tar}} program provides the ability to create tar archives, as well as various other kinds of manipulation. For example, you can use Tar on previously created archives to extract files, to store additional files, or to update or list files which were already stored."''
  
 +
As an early Unix compression format, {{ic|tar}} files (known as '''tarballs''') are widely used for packaging in Unix-like operating systems. Both [[pacman]] and [[Arch User Repository|AUR]] packages are tarballs, and Arch uses [[GNU Project|GNU's]] {{Ic|Tar}} program by default.
  
cp -pR directory /newplace
+
==Usage==
 +
For {{ic|tar}} archives, {{Ic|Tar}} by default will extract the file according to its extension:
 +
$ tar xvf file.EXTENSION
  
doesn't always do the job.
+
Forcing a given format:
  
Using tar instead will preserve ownership, permissions, and timestamps. This neat trick allows using tar to perform a recursive copy without creating an intermediate tar file and overcoming all cp shortcomings.
+
{| border="1"
 +
!File Type    !! Extraction Command
 +
|-
 +
|{{ic|file.tar}}    || {{Ic|tar xvf file.tar}}
 +
|-
 +
|{{ic|file.tgz}}    || {{Ic|tar xvzf file.tgz}}
 +
|-
 +
|{{ic|file.tar.gz}}  || {{Ic|tar xvzf file.tar.gz}}
 +
|-
 +
|{{ic|file.tar.bz}}  || {{Ic|<nowiki>bzip -cd file.bz | tar xvf -</nowiki>}}
 +
|-
 +
|{{ic|file.tar.bz2}} || {{Ic|tar xvjf file.tar.bz2}}<br> {{Ic|<nowiki>bzip2 -cd file.bz2 | tar xvf -</nowiki>}}
 +
|-
 +
|{{ic|file.tar.xz}}  || {{Ic|tar xvJf file.tar.xz}}<br> {{Ic|<nowiki> xz -cd file.xz | tar xvf -</nowiki>}}
 +
|}
  
To copy all of the files and subdirectories in the current working directory to the directory /target, use:
+
The construction of some of these {{Ic|Tar}} arguments may be considered legacy, but they are still useful when performing specific operations. The '''Compatibility''' section of {{Ic|Tar}}'s [[man page]] shows how they work in detail.
  
 +
===As a cp alternative===
 +
{{accuracy}}
 +
Copying a directory tree and its contents to another filesystem using
  
tar cf - * | ( cd /target; tar xfp -)
+
$ cp -pR directory /target
  
The first part of the command before the pipe instruct tar to create an archive of everything in the current directory and write it to standard output (the - in place of a filename frequently indicates stdout). The commands within parentheses cause the shell to change directory to the target directory and untar data from standard input. Since the cd and tar commands are contained within parentheses, their actions are performed together.
+
is not always sufficient.
  
The -p option in the tar extraction command directs tar to preserve permission and ownership information, if possible given the user executing the command. If you are running the command as superuser, this option is turned on by default and can be omitted.
+
Using {{Ic|Tar}} instead will preserve ownership, permissions, and timestamps.
  
Note that the * will not copy any of the files prefixed with a . in the root directory. It is a little tricky to wild card these files because one does not want to include the . and .. directories so usually one adds .??* to pick up everything else except for 1 and 2 character filenames prefixed with the . e.g. .a, .bc. To copy these as well, you will want to list them by doing an
+
This neat trick allows using {{Ic|Tar}} to perform a recursive copy without creating an intermediate {{ic|tar}} file and overcoming all {{Ic|cp}} shortcomings.
  
 +
To copy all of the files and subdirectories in the current working directory to the directory {{ic|/target}}, use:
  
ls -a
+
$ tar cf - * | ( cd /target; tar xfp -)
 +
 
 +
The first part of the command before the pipe instructs {{Ic|Tar}} to create an archive of everything in the current directory and write it to standard output (the {{Ic|-}} in place of a filename frequently indicates stdout). The commands within parentheses cause the shell to change directory to the target directory and untar data from standard input. Since the {{Ic|cd}} and {{Ic|Tar}} commands are contained within parentheses, their actions are performed together.
 +
 
 +
The {{Ic|-p}} option in the tar extraction command directs {{Ic|Tar}} to preserve permission and ownership information, if possible given the user executing the command. If you are running the command as root, this option is turned on by default and can be omitted.
 +
 
 +
Note that the {{ic|*}} will not copy any of the files prefixed with a {{ic|.}} in the root directory. It is tricky to wildcard these files because one does not want to include the {{ic|.}} and {{ic|..}} directories, so usually one adds {{ic|.??*}} to pick up everything else except for one- and two-character filenames prefixed with the {{ic|.}} (e.g. {{ic|.a}}, {{ic|.bc}}). To copy these as well, you will want to list them by doing:
 +
 
 +
$ ls -a
  
 
in the root directory first and typing those explicitly.
 
in the root directory first and typing those explicitly.
Line 26: Line 57:
 
In summary, I would recommend this instead:
 
In summary, I would recommend this instead:
  
 
+
$ tar cf - * .??* | ( cd /target; tar xfp -)
tar cf - * .??* | ( cd /target; tar xfp -)
+
  
 
but first do:
 
but first do:
  
 +
$ ls -a
  
ls -a
+
and if you see anything starting with a {{ic|.}} (besides {{ic|..}}) that is not followed by more then two characters, add those as well; e.g.:
 
+
and if you see anything starting with a . (besides ..) that is not followed by more then two characters, add those as well e.g.
+
  
 +
$ tar cf - * .??* .a .z .bc | ( cd /target; tar xfp -)
  
tar cf - * .??* .a .z .bc | ( cd /target; tar xfp -)
+
==See Also==
 +
* [http://www.gnu.org/software/tar/manual/index.html GNU tar manual] (Also available via {{Ic|info tar}})

Revision as of 13:04, 13 June 2012

From GNU's Tar Page:

"The Tar program provides the ability to create tar archives, as well as various other kinds of manipulation. For example, you can use Tar on previously created archives to extract files, to store additional files, or to update or list files which were already stored."

As an early Unix compression format, tar files (known as tarballs) are widely used for packaging in Unix-like operating systems. Both pacman and AUR packages are tarballs, and Arch uses GNU's Tar program by default.

Usage

For tar archives, Tar by default will extract the file according to its extension:

$ tar xvf file.EXTENSION

Forcing a given format:

File Type Extraction Command
file.tar tar xvf file.tar
file.tgz tar xvzf file.tgz
file.tar.gz tar xvzf file.tar.gz
file.tar.bz bzip -cd file.bz | tar xvf -
file.tar.bz2 tar xvjf file.tar.bz2
bzip2 -cd file.bz2 | tar xvf -
file.tar.xz tar xvJf file.tar.xz
xz -cd file.xz | tar xvf -

The construction of some of these Tar arguments may be considered legacy, but they are still useful when performing specific operations. The Compatibility section of Tar's man page shows how they work in detail.

As a cp alternative

Tango-inaccurate.pngThe factual accuracy of this article or section is disputed.Tango-inaccurate.png

Reason: please use the first argument of the template to provide a brief explanation. (Discuss in Talk:Tar#)

Copying a directory tree and its contents to another filesystem using

$ cp -pR directory /target

is not always sufficient.

Using Tar instead will preserve ownership, permissions, and timestamps.

This neat trick allows using Tar to perform a recursive copy without creating an intermediate tar file and overcoming all cp shortcomings.

To copy all of the files and subdirectories in the current working directory to the directory /target, use:

$ tar cf - * | ( cd /target; tar xfp -)

The first part of the command before the pipe instructs Tar to create an archive of everything in the current directory and write it to standard output (the - in place of a filename frequently indicates stdout). The commands within parentheses cause the shell to change directory to the target directory and untar data from standard input. Since the cd and Tar commands are contained within parentheses, their actions are performed together.

The -p option in the tar extraction command directs Tar to preserve permission and ownership information, if possible given the user executing the command. If you are running the command as root, this option is turned on by default and can be omitted.

Note that the * will not copy any of the files prefixed with a . in the root directory. It is tricky to wildcard these files because one does not want to include the . and .. directories, so usually one adds .??* to pick up everything else except for one- and two-character filenames prefixed with the . (e.g. .a, .bc). To copy these as well, you will want to list them by doing:

$ ls -a

in the root directory first and typing those explicitly.

In summary, I would recommend this instead:

$ tar cf - * .??* | ( cd /target; tar xfp -)

but first do:

$ ls -a

and if you see anything starting with a . (besides ..) that is not followed by more then two characters, add those as well; e.g.:

$ tar cf - * .??* .a .z .bc | ( cd /target; tar xfp -)

See Also