1.\" $OpenBSD: tar.1,v 1.67 2024/04/16 23:09:35 jca Exp $ 2.\" 3.\" Copyright (c) 1996 SigmaSoft, Th. Lockert 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25.\" 26.Dd $Mdocdate: April 16 2024 $ 27.Dt TAR 1 28.Os 29.Sh NAME 30.Nm tar 31.Nd tape archiver 32.Sh SYNOPSIS 33.Nm tar 34.Sm off 35.No { Cm crtux No } Op Cm 014578beFfHhjLmNOoPpqsvwXZz 36.Sm on 37.Bk -words 38.Op Ar blocking-factor | format | archive | replstr 39.Op Fl C Ar directory 40.Op Fl I Ar file 41.Op Ar 42.Ek 43.Nm tar 44.No { Ns Fl crtux Ns } 45.Bk -words 46.Op Fl 014578eHhjLmNOoPpqvwXZz 47.Op Fl b Ar blocking-factor 48.Op Fl C Ar directory 49.Op Fl F Ar format 50.Op Fl f Ar archive 51.Op Fl I Ar file 52.Op Fl s Ar replstr 53.Op Ar 54.Ek 55.Sh DESCRIPTION 56The 57.Nm 58command creates, adds files to, or extracts files from an 59archive file in 60.Dq tar 61format. 62A tar archive is often stored on a magnetic tape, but can be 63stored equally well on a floppy, CD-ROM, or in a regular disk file. 64.Pp 65In the first (legacy) form, all option flags except for 66.Fl C 67and 68.Fl I 69must be contained within the first argument to 70.Nm 71and must not be prefixed by a hyphen 72.Pq Sq - . 73Option arguments, if any, are processed as subsequent arguments to 74.Nm 75and are processed in the order in which their corresponding option 76flags have been presented on the command line. 77.Pp 78In the second and preferred form, option flags may be given in any order 79and are immediately followed by their corresponding option argument 80values. 81.Pp 82One of the following flags must be present: 83.Bl -tag -width Ds 84.It Fl c 85Create new archive, or overwrite an existing archive, 86adding the specified files to it. 87.It Fl r 88Append the named new files to existing archive. 89Note that this will only work on media on which an end-of-file mark 90can be overwritten. 91.It Fl t 92List contents of archive. 93If any files are named on the 94command line, only those files will be listed. 95The 96.Ar file 97arguments may be specified as glob patterns (see 98.Xr glob 7 99for more information), in which case 100.Nm 101will list all archive members that match each pattern. 102.It Fl u 103Alias for 104.Fl r . 105.It Fl x 106Extract files from archive. 107If any files are named on the 108command line, only those files will be extracted from the 109archive. 110The 111.Ar file 112arguments may be specified as glob patterns (see 113.Xr glob 7 114for more information), in which case 115.Nm 116will extract all archive members that match each pattern. 117.Pp 118If more than one copy of a file exists in the 119archive, later copies will overwrite earlier copies during 120extraction. 121The file mode and modification time are preserved 122if possible. 123The file mode is subject to modification by the 124.Xr umask 2 . 125.El 126.Pp 127In addition to the flags mentioned above, any of the following 128flags may be used: 129.Bl -tag -width Ds 130.It Fl b Ar blocking-factor 131Set blocking factor to use for the archive. 132.Nm 133uses 512-byte blocks. 134The default is 20, the maximum is 126. 135Archives with a blocking factor larger than 63 136violate the POSIX standard and will not be portable to all systems. 137.It Fl C Ar directory 138This is a positional argument which sets the working directory for the 139following files. 140When extracting, files will be extracted into 141the specified directory; when creating, the specified files will be matched 142from the directory. 143.It Fl e 144Stop after the first error. 145.It Fl F Ar format 146Specify the output archive format, with the default format being 147.Cm pax . 148.Nm 149currently supports the following formats: 150.Bl -tag -width "sv4cpio" 151.It Cm bcpio 152The old binary cpio format. 153The default blocksize for this format is 5120 bytes. 154This format is not very portable and should not be used when other formats 155are available. 156Inode and device information about a file (used for detecting file hard links 157by this format), which may be truncated by this format, is detected by 158.Nm 159and is repaired. 160.It Cm cpio 161The extended cpio interchange format specified in the 162.St -p1003.2 163standard. 164The default blocksize for this format is 5120 bytes. 165Inode and device information about a file (used for detecting file hard links 166by this format), which may be truncated by this format, is detected by 167.Nm 168and is repaired. 169.It Cm sv4cpio 170The System V release 4 cpio. 171The default blocksize for this format is 5120 bytes. 172Inode and device information about a file (used for detecting file hard links 173by this format), which may be truncated by this format, is detected by 174.Nm 175and is repaired. 176.It Cm sv4crc 177The System V release 4 cpio with file CRC checksums. 178The default blocksize for this format is 5120 bytes. 179Inode and device information about a file (used for detecting file hard links 180by this format), which may be truncated by this format, is detected by 181.Nm 182and is repaired. 183.It Cm tar 184The old 185.Bx 186tar format as found in 187.Bx 4.3 . 188The default blocksize for this format is 10240 bytes. 189Pathnames stored by this format must be 100 characters or less in length. 190Only regular files, hard links, soft links, and directories 191will be archived (other file system types are not supported). 192For backwards compatibility with even older tar formats, see the 193description for 194.Fl o . 195.It Cm ustar 196The extended tar interchange format specified in the 197.St -p1003.2 198standard. 199The default blocksize for this format is 10240 bytes. 200Filenames stored by this format must be 100 characters or less in length; 201the total pathname must be 256 characters or less. 202.It Cm pax 203The pax interchange format specified in the 204.St -p1003.1-2001 205standard. 206The default blocksize for this format is 5120 bytes. 207.El 208.Pp 209.Nm 210will detect and report any file that it is unable to store or extract 211as the result of any specific archive format restrictions. 212The individual archive formats may impose additional restrictions on use. 213Typical archive format restrictions include (but are not limited to): 214file pathname length, file size, link pathname length, and the type of the 215file. 216.It Fl f Ar archive 217Read from or write to 218.Ar archive . 219A hyphen 220.Pq - 221can be used to represent standard input or output. 222The default is 223.Pa /dev/rst0 . 224See also the 225.Ev TAPE 226environment variable. 227.It Fl H 228Follow symlinks given on the command line only. 229.It Fl h 230Follow symbolic links as if they were normal files 231or directories. 232In extract mode this means that a directory entry in the archive 233will not overwrite an existing symbolic link, but rather what the 234link ultimately points to. 235.It Fl I Ar file 236This is a positional argument which reads the names of files to 237archive or extract from the given file, one per line. 238.It Fl j 239Compress archive using bzip2. 240The bzip2 utility must be installed separately. 241.It Fl L 242Synonym for the 243.Fl h 244option. 245.It Fl m 246Do not preserve modification time. 247.It Fl N 248Use only the numeric UID and GID values when creating or extracting an 249archive. 250.It Fl O 251Write old-style (non-POSIX) archives. 252.It Fl o 253Don't write directory information that the older (V7) style 254.Nm 255is unable to decode. 256This implies the 257.Fl O 258flag. 259.It Fl P 260Do not strip leading slashes 261.Pq Sq / 262from pathnames. 263The default is to strip leading slashes. 264.It Fl p 265Preserve user and group ID as well as file mode regardless of 266the current 267.Xr umask 2 . 268The setuid and setgid bits are only preserved if the user and group ID 269could be preserved. 270Only meaningful in conjunction with the 271.Fl x 272flag. 273.It Fl q 274Select the first archive member that matches each 275.Ar file 276operand. 277No more than one archive member is matched for each 278.Ar file . 279When members of type directory are matched, the file hierarchy rooted at that 280directory is also matched. 281.It Fl s Ar replstr 282Modify the archive member names according to the substitution expression 283.Ar replstr , 284using the syntax of the 285.Xr ed 1 286utility regular expressions. 287.Ar file 288arguments may be given to restrict the list of archive members to those 289specified. 290.Pp 291The format of these regular expressions is 292.Pp 293.Dl /old/new/[gp] 294.Pp 295As in 296.Xr ed 1 , 297.Va old 298is a basic regular expression (see 299.Xr re_format 7 ) 300and 301.Va new 302can contain an ampersand 303.Pq Ql & , 304.Ql \e Ns Em n 305(where 306.Em n 307is a digit) back-references, 308or subexpression matching. 309The 310.Va old 311string may also contain newline characters. 312Any non-null character can be used as a delimiter 313.Po 314.Ql / 315is shown here 316.Pc . 317Multiple 318.Fl s 319expressions can be specified. 320The expressions are applied in the order they are specified on the 321command line, terminating with the first successful substitution. 322.Pp 323The optional trailing 324.Cm g 325continues to apply the substitution expression to the pathname substring, 326which starts with the first character following the end of the last successful 327substitution. 328The first unsuccessful substitution stops the operation of the 329.Cm g 330option. 331The optional trailing 332.Cm p 333will cause the final result of a successful substitution to be written to 334standard error in the following format: 335.Pp 336.D1 Em original-pathname No >> Em new-pathname 337.Pp 338File or archive member names that substitute to the empty string 339are not selected and will be skipped. 340.It Fl v 341Verbose operation mode. 342If 343.Fl v 344is specified multiple times or if the 345.Fl t 346option is also specified, 347.Nm 348will use a long format for listing files, similar to 349.Xr ls 1 350.Fl l . 351.It Fl w 352Interactively rename files. 353This option causes 354.Nm 355to prompt the user for the filename to use when storing or 356extracting files in an archive. 357.It Fl X 358Do not cross mount points in the file system. 359.It Fl Z 360Compress archive using 361.Xr compress 1 . 362.It Fl z 363Compress archive using 364.Xr gzip 1 . 365.El 366.Pp 367The options 368.Op Fl 014578 369can be used to select one of the compiled-in backup devices, 370.Pa /dev/rstN . 371.Sh ENVIRONMENT 372.Bl -tag -width Ds 373.It Ev TMPDIR 374Path in which to store temporary files. 375.It Ev TAPE 376Default tape device to use instead of 377.Pa /dev/rst0 . 378If set to hyphen 379.Pq Sq - , 380standard output is used. 381.El 382.Sh FILES 383.Bl -tag -width "/dev/rst0" 384.It Pa /dev/rst0 385default archive name 386.El 387.Sh EXIT STATUS 388The 389.Nm 390utility exits with one of the following values: 391.Pp 392.Bl -tag -width Ds -offset indent -compact 393.It 0 394All files were processed successfully. 395.It 1 396An error occurred. 397.El 398.Sh EXAMPLES 399Create an archive on the default tape drive, containing the files named 400.Pa bonvole 401and 402.Pa sekve : 403.Pp 404.Dl $ tar c bonvole sekve 405.Pp 406Create a 407.Xr gzip 1 408compressed archive, called 409.Pa foriru.tar.gz , 410containing the files 411.Pa bonvole 412and 413.Pa sekve : 414.Pp 415.Dl $ tar czf foriru.tar.gz bonvole sekve 416.Pp 417Verbosely create an archive, called 418.Pa backup.tar.gz , 419of all files matching the shell 420.Xr glob 7 421function 422.Pa *.c : 423.Pp 424.Dl $ tar cvzf backup.tar.gz *.c 425.Pp 426Verbosely list, but do not extract, all files ending in 427.Pa .jpeg 428from a compressed archive named 429.Pa backup.tar.gz . 430Note that the glob pattern has been quoted to avoid expansion by the shell: 431.Pp 432.Dl $ tar tvzf backup.tar.gz '*.jpeg' 433.Pp 434Verbosely extract an archive, called 435.Pa foo.tar.gz , 436to the directory 437.Pa /var/foo : 438.Pp 439.Dl $ tar xvzf foo.tar.gz -C /var/foo 440.Pp 441For more detailed examples, see 442.Xr pax 1 . 443.Sh DIAGNOSTICS 444Whenever 445.Nm 446cannot create a file or a link when extracting an archive or cannot 447find a file while writing an archive, or cannot preserve the user 448ID, group ID, file mode, or access and modification times when the 449.Fl p 450option is specified, a diagnostic message is written to standard 451error and a non-zero exit value will be returned, but processing 452will continue. 453In the case where 454.Nm 455cannot create a link to a file, 456.Nm 457will not create a second copy of the file. 458.Pp 459If the extraction of a file from an archive is prematurely terminated 460by a signal or error, 461.Nm 462may have only partially extracted the file the user wanted. 463Additionally, the file modes of extracted files and directories may 464have incorrect file bits, and the modification and access times may 465be wrong. 466.Pp 467If the creation of an archive is prematurely terminated by a signal 468or error, 469.Nm 470may have only partially created the archive, which may violate the 471specific archive format specification. 472.Sh SEE ALSO 473.Xr cpio 1 , 474.Xr pax 1 475.Sh HISTORY 476A 477.Nm 478command first appeared in 479.At v7 . 480.Sh AUTHORS 481.An Keith Muller 482at the University of California, San Diego. 483.Sh CAVEATS 484The 485.Fl j 486and 487.Fl L 488flags are not portable to other versions of 489.Nm 490where they may have a different meaning. 491