Difference between revisions of "Tar"

From ArchWiki
Jump to: navigation, search
m
(clean-up)
Line 1: Line 1:
 
[[Category:Utilities (English)]]
 
[[Category:Utilities (English)]]
 
[[Category:HOWTOs (English)]]
 
[[Category:HOWTOs (English)]]
 +
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.[http://www.gnu.org/software/tar/]
 +
 +
==Rationale==
 
Copying a directory tree and its contents to another filesystem using
 
Copying a directory tree and its contents to another filesystem using
  
  cp -pR directory /newplace
+
  $ cp -pR directory /target
 +
 
 +
is not always sufficient.
 +
 
 +
Using {{Codeline|tar}} instead will preserve ownership, permissions, and timestamps.
  
doesn't always do the job.
+
==Usage==
  
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.
+
===As a cp alternative===
 +
This neat trick allows using {{Codeline|tar}} to perform a recursive copy without creating an intermediate {{Codeline|tar}} file and overcoming all {{Codeline|cp}} shortcomings.
  
To copy all of the files and subdirectories in the current working directory to the directory /target, use:
+
To copy all of the files and subdirectories in the current working directory to the directory {{Filename|/target}}, use:
  
  tar cf - * | ( cd /target; tar xfp -)
+
  $ tar cf - * | ( cd /target; tar xfp -)
  
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.
+
The first part of the command before the pipe instructs {{Codeline|tar}} to create an archive of everything in the current directory and write it to standard output (the {{Codeline|-}} 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 {{Codeline|cd}} and {{Codeline|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 superuser, this option is turned on by default and can be omitted.
+
The {{Codeline|-p}} option in the tar extraction command directs {{Codeline|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 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
+
Note that the {{Filename|*}} will not copy any of the files prefixed with a {{Filename|.}} in the root directory. It is tricky to wildcard these files because one does not want to include the {{Filename|.}} and {{Filename|..}} directories, so usually one adds {{Filename|.??*}} to pick up everything else except for one- and two-character filenames prefixed with the {{Filename|.}} (e.g. {{Filename|.a}}, {{Filename|.bc}}). To copy these as well, you will want to list them by doing:
  
  ls -a
+
  $ ls -a
  
 
in the root directory first and typing those explicitly.
 
in the root directory first and typing those explicitly.
Line 25: Line 33:
 
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 . (besides ..) that is not followed by more then two characters, add those as well e.g.
+
and if you see anything starting with a {{Filename|.}} (besides {{Filename|..}}) 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 -)

Revision as of 14:49, 12 January 2010

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.[1]

Rationale

Copying a directory tree and its contents to another filesystem using

$ cp -pR directory /target

is not always sufficient.

Using Template:Codeline instead will preserve ownership, permissions, and timestamps.

Usage

As a cp alternative

This neat trick allows using Template:Codeline to perform a recursive copy without creating an intermediate Template:Codeline file and overcoming all Template:Codeline shortcomings.

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

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

The first part of the command before the pipe instructs Template:Codeline to create an archive of everything in the current directory and write it to standard output (the Template:Codeline 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 Template:Codeline and Template:Codeline commands are contained within parentheses, their actions are performed together.

The Template:Codeline option in the tar extraction command directs Template:Codeline 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 Template:Filename will not copy any of the files prefixed with a Template:Filename in the root directory. It is tricky to wildcard these files because one does not want to include the Template:Filename and Template:Filename directories, so usually one adds Template:Filename to pick up everything else except for one- and two-character filenames prefixed with the Template:Filename (e.g. Template:Filename, Template:Filename). 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 Template:Filename (besides Template:Filename) that is not followed by more then two characters, add those as well; e.g.:

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