Merge from vendor branch HEIMDAL:
[dragonfly.git] / contrib / bsdtar / bsdtar.1
1 .\" Copyright (c) 2003 Tim Kientzle
2 .\" All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\"
13 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 .\" SUCH DAMAGE.
24 .\"
25 .\" $FreeBSD: src/usr.bin/tar/bsdtar.1,v 1.21 2004/08/27 04:19:28 kientzle Exp $
26 .\"
27 .Dd April 13, 2004
28 .Dt BSDTAR 1
29 .Os
30 .Sh NAME
31 .Nm tar
32 .Nd manipulate tape archives
33 .Sh SYNOPSIS
34 .Nm
35 .Op Ar bundled-flags Ao args Ac
36 .Op Ao Ar file Ac | Ao Ar pattern Ac ...
37 .Nm
38 .Brq Fl c
39 .Op Ar options
40 .Op Ar files | directories
41 .Nm
42 .Brq Fl r | Fl u
43 .Fl f Ar archive-file
44 .Op Ar options
45 .Op Ar files | directories
46 .Nm
47 .Brq Fl t | Fl x
48 .Op Ar options
49 .Op Ar patterns
50 .Sh DESCRIPTION
51 .Nm
52 creates and manipulates streaming archive files.
53 .Pp
54 The first synopsis form shows a
55 .Dq bundled
56 option word.
57 This usage is provided for compatibility with historical implementations.
58 See COMPATIBILITY below for details.
59 .Pp
60 The other synopsis forms show the preferred usage.
61 The first option to
62 .Nm
63 is a mode indicator from the following list:
64 .Bl -tag -compact -width indent
65 .It Fl c
66 Create a new archive containing the specified items.
67 .It Fl r
68 Like
69 .Fl c ,
70 but new entries are appended to the archive.
71 Note that this only works on uncompressed archives stored in regular files.
72 The
73 .Fl f
74 option is required.
75 .It Fl t
76 List archive contents to stdout.
77 .It Fl u
78 Like
79 .Fl r ,
80 but new entries are added only if they have a modification date
81 newer than the corresponding entry in the archive.
82 Note that this only works on uncompressed archives stored in regular files.
83 The
84 .Fl f
85 option is required.
86 .It Fl x
87 Extract to disk from the archive.
88 If a file with the same name appears more than once in the archive,
89 each copy will be extracted, with later copies overwriting (replacing)
90 earlier copies.
91 .El
92 .Pp
93 In
94 .Fl c ,
95 .Fl r ,
96 or
97 .Fl u
98 mode, each specified file or directory is added to the
99 archive in the order specified on the command line.
100 By default, the contents of each directory are also archived.
101 .Pp
102 In extract or list mode, the entire command line
103 is read and parsed before the archive is opened.
104 The pathnames or patterns on the command line indicate
105 which items in the archive should be processed.
106 Patterns are shell-style globbing patterns as
107 documented in XXXX.
108 .Sh OPTIONS
109 Unless specifically stated otherwise, options are applicable in
110 all operating modes.
111 .Bl -tag -width indent
112 .It Cm @ Ns Pa archive
113 (c and r mode only)
114 The specified archive is opened and the entries
115 in it will be appended to the current archive.
116 As a simple example,
117 .Dl Nm Fl c Fl f Pa - Pa newfile Cm @ Ns Pa original.tar
118 writes a new archive to standard output containing a file
119 .Pa newfile
120 and all of the entries from
121 .Pa original.tar .
122 In contrast,
123 .Dl Nm Fl c Fl f Pa - Pa newfile Pa original.tar
124 creates a new archive with only two entries.
125 Similarly,
126 .Dl Nm Fl czf Pa - Fl -format Cm pax Cm @ Ns Pa -
127 reads an archive from standard input (whose format will be determined
128 automatically) and converts it into a gzip-compressed
129 pax-format archive on stdout.
130 In this way,
131 .Nm
132 can be used to convert archives from one format to another.
133 .It Fl b Ar blocksize
134 Specify the block size, in 512-byte records, for tape drive I/O.
135 As a rule, this argument is only needed when reading from or writing
136 to tape drives, and usually not even then as the default block size of
137 20 records (10240 bytes) is very common.
138 .It Fl C Ar directory
139 In c and r mode, this changes the directory before adding
140 the following files.
141 In x mode, change directories after opening the archive
142 but before extracting entries from the archive.
143 .It Fl -check-links ( Fl W Cm check-links )
144 (c and r modes only)
145 Issue a warning message unless all links to each file are archived.
146 .It Fl -exclude Ar pattern ( Fl W Cm exclude Ns = Ns Ar pattern )
147 Do not process files or directories that match the
148 specified pattern.
149 Note that exclusions take precedence over patterns or filenames
150 specified on the command line.
151 .It Fl -format Ar format ( Fl W Cm format Ns = Ns Ar format )
152 (c mode only)
153 Use the specified format for the created archive.
154 Supported formats include
155 .Dq cpio ,
156 .Dq pax ,
157 .Dq shar ,
158 and
159 .Dq ustar .
160 Other formats may also be supported; see
161 .Xr libarchive-formats 5
162 for more information about currently-supported formats.
163 .It Fl f Ar file
164 Read the archive from or write the archive to the specified file.
165 The filename can be
166 .Pa -
167 for standard input or standard output.
168 If not specified, the default tape device will be used.
169 (On FreeBSD, the default tape device is
170 .Pa /dev/sa0 . )
171 .It Fl -fast-read ( Fl W Cm fast-read )
172 (x and t mode only)
173 Extract or list only the first archive entry that matches each pattern
174 or filename operand.
175 Exit as soon as each specified pattern or filename has been matched.
176 By default, the archive is always read to the very end, since
177 there can be multiple entries with the same name and, by convention,
178 later entries overwrite earlier entries.
179 This option is provided as a performance optimization.
180 .It Fl H
181 (c and r mode only)
182 Symbolic links named on the command line will be followed; the
183 target of the link will be archived, not the link itself.
184 .It Fl h
185 (c and r mode only)
186 Synonym for
187 .Fl L .
188 .It Fl -include Ar pattern ( Fl W Cm include Ns = Ns Ar pattern )
189 Process only files or directories that match the specified pattern.
190 Note that exclusions specified with
191 .Fl -exclude
192 take precedence over inclusions.
193 If no inclusions are explicitly specified, all entries are processed by
194 default.
195 The
196 .Fl -include
197 option is especially useful when filtering archives.
198 For example, the command
199 .Dl Nm Fl c Fl f Pa new.tar Fl -include='*foo*' Cm @ Ns Pa old.tgz
200 creates a new archive
201 .Pa new.tar
202 containing only the entries from
203 .Pa old.tgz
204 containing the string
205 .Sq foo .
206 .It Fl j
207 (c mode only)
208 Compress the resulting archive with
209 .Xr bzip2 1 .
210 In extract or list modes, this option is ignored.
211 Note that, unlike other
212 .Nm tar
213 implementations, this implementation recognizes bzip2 compression
214 automatically when reading archives.
215 .It Fl k
216 (x mode only)
217 Do not overwrite existing files.
218 In particular, if a file appears more than once in an archive,
219 later copies will not overwrite earlier copies.
220 .It Fl L
221 (c and r mode only)
222 All symbolic links will be followed.
223 Normally, symbolic links are archived as such.
224 With this option, the target of the link will be archived instead.
225 .It Fl l
226 If
227 .Ev POSIXLY_CORRECT
228 is specified in the environment, this is a synonym for the
229 .Fl -check-links
230 option.
231 Otherwise, an error will be displayed.
232 Users who desire behavior compatible with GNU tar should use
233 the
234 .Fl -one-file-system
235 option instead.
236 .It Fl m
237 (x mode only)
238 Do not extract modification time.
239 By default, the modification time is set to the time stored in the archive.
240 .It Fl n
241 (c, r, u modes only)
242 Do not recursively archive the contents of directories.
243 .It Fl -nodump ( Fl W Cm nodump )
244 (c and r modes only)
245 Honor the nodump file flag by skipping this file.
246 .It Fl O
247 (x, t modes only)
248 In extract (-x) mode, files will be written to standard out rather than
249 being extracted to disk.
250 In list (-t) mode, the file listing will be written to stderr rather than
251 the usual stdout.
252 .It Fl o
253 (x mode only)
254 Use the user and group of the user running the program rather
255 than those specified in the archive.
256 Note that this has no significance unless
257 .Fl p
258 is specified, and the program is being run by the root user.
259 In this case, the file modes and flags from
260 the archive will be restored, but ACLs or owner information in
261 the archive will be discarded.
262 .It Fl P
263 Preserve pathnames.
264 By default, absolute pathnames (those that begin with a /
265 character) have the leading slash removed both when creating archives
266 and extracting from them.
267 Also,
268 .Nm
269 will refuse to extract archive entries whose pathnames contain
270 .Pa ..
271 or whose target directory would be altered by a symlink.
272 This option suppresses these behaviors.
273 .It Fl p
274 (x mode only)
275 Preserve file permissions.
276 Attempt to restore the full permissions, including owner, file modes, file
277 flags and ACLs, if available, for each item extracted from the archive.
278 By default, newly-created files are owned by the user running
279 .Nm ,
280 the file mode is restored for newly-created regular files, and
281 all other types of entries receive default permissions.
282 If
283 .Nm
284 is being run by root, the default is to restore the owner unless the
285 .Fl o
286 option is also specified.
287 .It Fl T Ar filename
288 (c mode only)
289 Read names to be archived from
290 .Pa filename .
291 Names are terminated by newlines.
292 The special name
293 .Dq -C
294 will cause the current directory to be changed to the directory
295 specified on the following line.
296 .It Fl U
297 (x mode only)
298 Unlink files before creating them.
299 Without this option,
300 .Nm
301 overwrites existing files, which preserves existing hardlinks.
302 With this option, existing hardlinks will be broken, as will any
303 symlink that would affect the location of an extracted file.
304 .It Fl v
305 Produce verbose output.
306 In create and extract modes,
307 .Nm
308 will list each file name as it is read from or written to
309 the archive.
310 In list mode,
311 .Nm
312 will produce output similar to that of
313 .Xr ls 1 .
314 Additional
315 .Fl v
316 options will provide additional detail.
317 .It Fl W Ar longopt=value
318 Long options (preceded by
319 .Fl - )
320 are only supported directly on systems that have the
321 .Xr getopt_long 3
322 function.
323 The
324 .Fl W
325 option can be used to access long options on systems that
326 do not support this function.
327 .It Fl w
328 Ask for confirmation for every action.
329 .It Fl X Ar filename
330 Read a list of exclusion patterns from the specified file.
331 See
332 .Fl -exclude
333 for more information about the handling of exclusions.
334 .It Fl y
335 (c mode only)
336 Compress the resulting archive with
337 .Xr bzip2 1 .
338 In extract or list modes, this option is ignored.
339 Note that, unlike other
340 .Nm tar
341 implementations, this implementation recognizes bzip2 compression
342 automatically when reading archives.
343 .It Fl z
344 (c mode only)
345 Compress the resulting archive with
346 .Xr gzip 1 .
347 In extract or list modes, this option is ignored.
348 Note that, unlike other
349 .Nm tar
350 implementations, this implementation recognizes gzip compression
351 automatically when reading archives.
352 .El
353 .Sh EXAMPLES
354 The following creates a new archive
355 called
356 .Ar file.tar
357 that contains two files
358 .Ar source.c
359 and
360 .Ar source.h :
361 .Dl Nm Fl czf Pa file.tar Pa source.c Pa source.h
362 .Pp
363 To view a detailed table of contents for this
364 archive:
365 .Dl Nm Fl tvf Pa file.tar
366 .Pp
367 To extract all entries from the archive on
368 the default tape drive:
369 .Dl Nm Fl x
370 .Pp
371 In create mode, the list of files and directories to be archived
372 can also include directory change instructions of the form
373 .Cm -C Ns Pa foo/baz
374 and archive inclusions of the form
375 .Cm @ Ns Pa archive-file .
376 For example, the command line
377 .Dl Nm Fl c Fl f Pa new.tar Pa foo1 Cm @ Ns Pa old.tgz Cm -C Ns Pa /tmp Pa foo2
378 will create a new archive
379 .Pa new.tar .
380 .Nm
381 will read the file
382 .Pa foo1
383 from the current directory and add it to the output archive.
384 It will then read each entry from
385 .Pa old.tgz
386 and add those entries to the output archive.
387 Finally, it will switch to the
388 .Pa /tmp
389 directory and add
390 .Pa foo2
391 to the output archive.
392 .Sh DIAGNOSTICS
393 .Ex -std
394 .Sh ENVIRONMENT
395 The following environment variables affect the execution of
396 .Nm :
397 .Bl -tag -width ".Ev BLOCKSIZE"
398 .It Ev LANG
399 The locale to use.
400 See
401 .Xr environ 7
402 for more information.
403 .It Ev POSIXLY_CORRECT
404 If this environment variable is defined, the
405 .Fl l
406 option will be interpreted in accordance with
407 .St -p1003.1-96 .
408 .It Ev TAPE
409 The default tape device.
410 The
411 .Fl f
412 option overrides this.
413 .It Ev TZ
414 The timezone to use when displaying dates.
415 See
416 .Xr environ 7
417 for more information.
418 .El
419 .Sh FILES
420 .Bl -tag -width ".Ev BLOCKSIZE"
421 .It Pa /dev/sa0
422 The default tape device, if not overridden by the
423 .Ev TAPE
424 environment variable or the
425 .Fl f
426 option.
427 .El
428 .Sh COMPATIBILITY
429 The bundled-arguments format is supported for compatibility
430 with historic implementations.
431 It consists of an initial word (with no leading - character) in which
432 each character indicates an option.
433 Arguments follow as separate words.
434 The order of the arguments must match the order
435 of the corresponding characters in the bundled command word.
436 For example,
437 .Dl Nm Cm tbf 32 Pa file.tar
438 specifies three flags
439 .Cm t ,
440 .Cm b ,
441 and
442 .Cm f .
443 The
444 .Cm b
445 and
446 .Cm f
447 flags both require arguments,
448 so there must be two additional items
449 on the command line.
450 The
451 .Ar 32
452 is the argument to the
453 .Cm b
454 flag, and
455 .Ar file.tar
456 is the argument to the
457 .Cm f
458 flag.
459 .Pp
460 The mode options c, r, t, u, and x and the options
461 b, f, l, m, o, v, and w comply with SUSv2.
462 .Pp
463 For maximum portability, scripts that invoke
464 .Nm tar
465 should use the bundled-argument format above, should limit
466 themselves to the
467 .Cm c ,
468 .Cm t ,
469 and
470 .Cm x
471 modes, and the
472 .Cm b ,
473 .Cm f ,
474 .Cm m ,
475 .Cm v ,
476 and
477 .Cm w
478 options.
479 .Pp
480 On systems that support getopt_long(), additional long options
481 are available to improve compatibility with other tar implementations.
482 .Sh SECURITY
483 Certain security issues are common to many archiving programs, including
484 .Nm .
485 In particular, carefully-crafted archives can request that
486 .Nm
487 extract files to locations outside of the target directory.
488 This can potentially be used to cause unwitting users to overwrite
489 files they did not intend to overwrite.
490 If the archive is being extracted by the superuser, any file
491 on the system can potentially be overwritten.
492 There are three ways this can happen.
493 Although
494 .Nm
495 has mechanisms to protect against each one,
496 savvy users should be aware of the implications:
497 .Bl -bullet -width indent
498 .It
499 Archive entries can have absolute pathnames.
500 By default,
501 .Nm
502 removes the leading
503 .Pa /
504 character from filenames before restoring them to guard against this problem.
505 .It
506 Archive entries can have pathnames that include
507 .Pa ..
508 components.
509 By default,
510 .Nm
511 will not extract files containing
512 .Pa ..
513 components in their pathname.
514 .It
515 Archive entries can exploit symbolic links to restore
516 files to other directories.
517 An archive can restore a symbolic link to another directory,
518 then use that link to restore a file into that directory.
519 To guard against this,
520 .Nm
521 checks each extracted path for symlinks.
522 If the final path element is a symlink, it will be removed
523 and replaced with the archive entry.
524 If
525 .Fl U
526 is specified, any intermediate symlink will also be unconditionally removed.
527 If neither
528 .Fl U
529 nor
530 .Fl P
531 is specified,
532 .Nm
533 will refuse to extract the entry.
534 .El
535 To protect yourself, you should be wary of any archives that
536 come from untrusted sources.
537 You should examine the contents of an archive with
538 .Dl Nm Fl tf Pa filename
539 before extraction.
540 You should use the
541 .Fl k
542 option to ensure that
543 .Nm
544 will not overwrite any existing files or the
545 .Fl U
546 option to remove any pre-existing files.
547 You should generally not extract archives while running with super-user
548 privileges.
549 Note that the
550 .Fl P
551 option to
552 .Nm
553 disables the security checks above and allows you to extract
554 an archive while preserving any absolute pathnames,
555 .Pa ..
556 components, or symlinks to other directories.
557 .Sh SEE ALSO
558 .Xr bzip2 1 ,
559 .Xr cpio 1 ,
560 .Xr gzip 1 ,
561 .Xr mt 1 ,
562 .Xr pax 1 ,
563 .Xr shar 1 ,
564 .Xr libarchive 3 ,
565 .Xr libarchive-formats 5 ,
566 .Xr tar 5
567 .Sh STANDARDS
568 There is no current POSIX standard for the tar command; it appeared
569 in
570 .St -p1003.1-96
571 but was dropped from
572 .St -p1003.1-2001 .
573 The options used by this implementation were developed by surveying a
574 number of existing tar implementations as well as the old POSIX specification
575 for tar and the current POSIX specification for pax.
576 .Pp
577 The ustar and pax interchange file formats are defined by
578 .St -p1003.1-2001
579 for the pax command.
580 .Sh BUGS
581 POSIX and GNU violently disagree about the meaning of the
582 .Fl l
583 option.
584 Because of the potential for disaster if someone expects
585 one behavior and gets the other, the
586 .Fl l
587 option is deliberately broken in this implementation.
588 .Pp
589 The
590 .Fl C Pa dir
591 option may differ from historic implementations.
592 .Pp
593 All archive output is written in correctly-sized blocks, even
594 if the output is being compressed.
595 Whether or not the last output block is padded to a full
596 block size varies depending on the format and the
597 output device.
598 For tar and cpio formats, the last block of output is padded
599 to a full block size if the output is being
600 written to standard output or to a character or block device such as
601 a tape drive.
602 If the output is being written to a regular file, the last block
603 will not be padded.
604 Many compressors, including
605 .Xr gzip 1
606 and
607 .Xr bzip2 1 ,
608 complain about the null padding when decompressing an archive created by
609 .Nm ,
610 although they still extract it correctly.
611 .Pp
612 The compression and decompression is implemented internally, so
613 there may be insignificant differences between the compressed output
614 generated by
615 .Dl Nm Fl czf Pa - file
616 and that generated by
617 .Dl Nm Fl cf Pa - file | Nm gzip
618 .Pp
619 The default should be to read and write archives to the standard I/O paths,
620 but tradition (and POSIX) dictates otherwise.
621 .Pp
622 The
623 .Cm r
624 and
625 .Cm u
626 modes require that the archive be uncompressed
627 and located in a regular file on disk.
628 Other archives can be modified using
629 .Cm c
630 mode with the
631 .Pa @archive-file
632 extension.
633 .Pp
634 To archive a file called
635 .Pa @foo
636 or
637 .Pa -foo
638 you must specify it as
639 .Pa ./@foo
640 or
641 .Pa ./-foo ,
642 respectively.
643 .Pp
644 In create mode, a leading
645 .Pa ./
646 is always removed.
647 A leading
648 .Pa /
649 is stripped unless the
650 .Fl P
651 option is specified.
652 .Pp
653 There needs to be better support for file selection on both create
654 and extract.
655 .Pp
656 There is not yet any support for multi-volume archives or for archiving
657 sparse files.
658 .Pp
659 Converting between dissimilar archive formats (such as tar and cpio) using the
660 .Cm @ Ns Pa -
661 convention can cause hard link information to be lost.
662 (This is a consequence of the incompatible ways that different archive
663 formats store hardlink information.)
664 .Pp
665 There are alternative long options for many of the short options that
666 are deliberately not documented.
667 .Sh HISTORY
668 A
669 .Nm tar
670 command appeared in Seventh Edition Unix.
671 There have been numerous other implementations,
672 many of which extended the file format.
673 John Gilmore's
674 .Nm pdtar
675 public-domain implementation (circa November, 1987)
676 was quite influential, and formed the basis of GNU tar.
677 GNU tar was included as the standard system tar
678 in FreeBSD beginning with FreeBSD 1.0.
679 .Pp
680 This is a complete re-implementation based on the
681 .Xr libarchive 3
682 library.