Introduction to the tar command
1. Only one of the following 5 parameters can be selected
-c: create compressed archive -x: decompress -t: view content -r: append files to the end of the compressed archive -u: update the files in the original archive
2. The following parameters are optional and can be used together with the above parameters
-z: with gzip attribute -j: with bz2 attribute -Z: with the compress attribute -v: show all processes -O: extract file to standard output
3. The following parameter -f is required
-f: Use the file name, remember, this parameter is the last parameter, only the file name can be followed. #Packaging file: -c generates a new file, -f specifies the file name $ tar -cf txt.tar *.txt #Add to package: -r add file $ tar -rf txt.tar *.txt #Update the files in the package: -u update file $ tar -uf txt.tar a.txt #List the contents of the package: -t list files $ tar -tf txt.tar #Unzip all files (replace if there are files with the same name): -x unzip $ tar -xf txt.tar
4. Compressed file: tar -cf extension
#Pack all txt files $ tar -cvf txt.tar *.txt # compress with tar and then compress with gzip $ tar -czf txt.tar.gz *.jpg # Compress with tar and then compress with bzip2 $ tar -cjf txt.tar.bz2 *.jpg #Compress with tar and then compress with compress $ tar -cZf txt.tar.Z *.jpg
5. Compress files rar and zip
$ rar a txt.rar *.txt $ zip txt.zip *.jpg
6. Unzip the file
$ tar –xvf file.tar #Unzip the tar package $ tar -xzvf file.tar.gz # 解压 tar.gz $ tar -xjvf file.tar.bz2 # 解压 tar.bz2 $ tar –xZvf file.tar.Z #decompress tar.Z $ unrar e file.rar # depressurar $ unzip file.zip #unzip zip
7. Summary
*.tar decompress with tar –xvf *.gz decompress with gzip -d or gunzip *.tar.gz and *.tgz decompress with tar –xzf *.bz2 decompress with bzip2 -d or with bunzip2 *.tar.bz2 decompress with tar –xjf *.Z decompress with uncompress *.tar.Z decompress with tar –xZf *.rar decompress with unrar e *.zip unzip with unzip
8. Extension tar
解包:tar xvf FileName.tar Packaging: tar cvf FileName.tar DirName
9. Extension gz
Unzip 1: gunzip FileName.gz Unzip 2: gzip -d FileName.gz Compression: gzip FileName
10. Extension .tar.gz and extension .tgz
Unzip: tar zxvf FileName.tar.gz 压缩:tar zcvf FileName.tar.gz DirName
11. Extension .bz2
Unzip 1: bzip2 -d FileName.bz2 Unzip 2: bunzip2 FileName.bz2 Compression: bzip2 -z FileName
12. Extension .tar.bz2
Unzip: tar jxvf FileName.tar.bz2 压缩:tar jcvf FileName.tar.bz2 DirName
13. Extension .bz
Unzip 1: bzip2 -d FileName.bz Unzip 2: bunzip2 FileName.bz Compression: unknown
14. Extension .tar.bz
Unzip: tar jxvf FileName.tar.bz Compression: unknown
15. Extension .Z
Unzip: uncompress FileName.Z Compression: compress FileName
16. Extension .tar.Z
Unzip: tar Zxvf FileName.tar.Z 压缩:tar Zcvf FileName.tar.Z DirName
17. Extension .zip
Unzip: unzip FileName.zip Compression: zip FileName.zip DirName
18. Extension .rar
Unzip: rar x FileName.rar 压缩:rar a FileName.rar DirName
The tar --help under the Linux terminal is as follows:
$ tar --help Usage: tar [options...] [FILE]... GNU 'tar' Save many files together to a single tape or disk archive and restore desired files individually from the archive. Example tar -cf archive.tar foo bar # create archive from files foo and bar archive.tar tar -tvf archive.tar # List archive files archive.tar in detail all files in . tar -xf archive.tar # expand the archive file archive.tar all files in . Main operating mode: -A, --catenate, --concatenate append tar files to archive -c, --create create a new archive -d, --diff, --compare find differences between archives and filesystems --delete delete from archive (not tape!) -r, --append append files to end of archive -t, --list list archive contents --test-label test archive label and exit -u, --update append only files newer than the copy in the archive -x, --extract, --get extract files from archive Operation modifier: --check-device Check device number when creating incremental archives (default) -g, --listed-incremental=FILE handle newer GNU format incremental backups -G, --incremental handle old GNU format incremental backups --ignore-failed-read Do not exit with non-zero value when encountering unreadable file --level=NUMBER output level for incremental list archives created -n, --seek archive searchable --no-check-device Do not check device number when creating incremental archives --no-seek archive not searchable --occurrence[=NUMBER] process only NUMBER of each file in the archive events; only when combined with the following subcommand --delete, --diff, --extract or --list This option is only valid when used in combination with one of the . and whether the file list is given on the command line or via Specified by the -T option; the NUMBER value defaults to 1 --sparse-version=MAJOR[.MINOR] Set the discrete format version used (implicit --sparse) -S, --sparse Efficiently handle discrete files Override control: -k, --keep-old-files don't replace existing files when extracting, treat them as errors --keep-directory-symlink preserve existing symlinks to directories when extracting --keep-newer-files Do not replace existing files that are newer than the copy in the archive --no-overwrite-dir keep metadata of existing directories --overwrite overwrite existing files when decompressing --overwrite-dir Overwrite metadata of existing directories when decompressing (default) --recursive-unlink clear directory hierarchy before unzipping directory --remove-files remove files after adding them to the archive --skip-old-files don't replace existing files when extracting, silently skip over them -U, --unlink-first remove files to be rewritten before unpacking them -W, --verify try to verify archive after writing Select output stream: --ignore-command-error ignore the exit code of the child process --no-ignore-command-error Treat the non-zero exit code of the child process as an error -O, --to-stdout extract files to stdout --to-command=COMMAND Pipe the decompressed file to another program Action file properties: --atime-preserve[=METHOD] Preserve access time on output files, either by reading (default Restore the time after METHOD='replace'), or don't set the time for the first time (METHOD='system') --delay-directory-restore The modification time and the permissions of the extracted directory are not set until the end of the decompression --group=name forces NAME to be as the group owner of the added file --mode=CHANGES force added files (symbols) to be changed to permissions CHANGES --mtime=DATE-OR-FILE set from DATE-OR-FILE for added files mtime -m, --touch don't extract the modification time of the file --no-delay-directory-restore Suppress the effect of the --delay-directory-restore option --no-same-owner Unzip the file as yours (default for normal users) --no-same-permissions Use user's mask bits when extracting permissions from archive (defaults to normal user service) --numeric-owner always numeric user/group names --owner=name force NAME as the owner of the added file -p, --preserve-permissions, --same-permissions Unzip file permission information (default only for superusers) --preserve same as -p and -s --same-owner Keep the owner relationship consistent when trying to decompress (default for superusers) -s, --preserve-order, --same-order member arguments are listed in the same order as the files in the archive Handling of extended file attributes: --acls Enable the POSIX ACLs support --no-acls Disable the POSIX ACLs support --no-selinux Disable the SELinux context support --no-xattrs Disable extended attributes support --selinux Enable the SELinux context support --xattrs Enable extended attributes support --xattrs-exclude=MASK specify the exclude pattern for xattr keys --xattrs-include=MASK specify the include pattern for xattr keys Device selection and switching: -f, --file=ARCHIVE use archive or ARCHIVE device --force-local Treat the archive as a local archive even if a copy exists -F, --info-script=name, --new-volume-script=name Run script at the end of each tape (implies -M) -L, --tape-length=NUMBER Change tape after writing NUMBER × 1024 bytes -M, --multi-volume create/list/extract multi-volume archives --rmt-command=COMMAND use the specified rmt COMMAND instead of rmt --rsh-command=COMMAND use remote COMMAND instead of rsh --volno-file=FILE use/update the number of volumes in FILE Device partition: -b, --blocking-factor=BLOCKS BLOCKS x 512 bytes per record -B, --read-full-records rechunk on read (only works with 4.2BSD pipelines) -i, --ignore-zeros ignore zero-byte blocks in the archive (ie end of file) --record-size=NUMBER NUMBER bytes per record, multiplied by 512 Select archive format: -H, --format=FORMAT create an archive in the specified format FORMAT is one of the following formats: gnu GNU tar 1.13.x format oldgnu GNU format as per tar <= 1.12 pax POSIX 1003.1-2001 (pax) format posix is equivalent to pax ustar POSIX 1003.1-1988 (ustar) format v7 old V7 tar format --old-archive, --portability Equivalent to --format=v7 --pax-option=keyword[[:]=value][,keyword[[:]=value]]... control pax keyword --posix is equivalent to --format=posix -V, --label=TEXT create TEXT with volume name archives; use TEXT when listing/extracting pattern string as volume name Compression options: -a, --auto-compress use archive suffix to determine compressor -I, --use-compress-program=PROG Filter by PROG (must be able to accept -d option program) -j, --bzip2 filter archive by bzip2 -J, --xz filter archive by xz --lzip filter archive by lzip --lzma filter archives by lzma --lzop --no-auto-compress do not use archive suffix to determine compressor -z, --gzip, --gunzip, --ungzip Filter archives by gzip -Z, --compress, --uncompress filter archive by compress Local file selection: --add-file=FILE Add the specified FILE to the archive (if the name starts with - would be useful to start with) --backup[=CONTROL] backup before deletion, select CONTROL version -C, --directory=DIR change to directory DIR --exclude=PATTERN exclude files specified with PATTERN --exclude-backups exclude backups and lock files --exclude-caches exclude includes in addition to identifying the file itself Contents of the directory of CACHEDIR.TAG --exclude-caches-all exclude directories containing CACHEDIR.TAG --exclude-caches-under exclude everything in directories containing CACHEDIR.TAG --exclude-tag=FILE exclude include FILE in addition to FILE itself the contents of the directory --exclude-tag-all=FILE exclude directories containing FILE --exclude-tag-under=FILE exclude everything in directories containing FILE --exclude-vcs exclude version control system directories -h, --dereference Follow symbolic links; archive and output the files they point to --hard-dereference Follow hard links; archive and output the files they point to -K, --starting-file=MEMBER-NAME begin at member MEMBER-NAME when reading the archive --newer-mtime=DATE compare data and time when only data has changed --no-null disable the effect of the last --null option --no-recursion avoid automatic downgrades in directories --no-unquote Do not end the quote with the filename read by -T --null -T read null-terminated names, -C disable -N, --newer=DATE-OR-FILE, --after-date=DATE-OR-FILE Only save files newer than DATE-OR-FILE --one-file-system save in local filesystem when creating archive -P, --absolute-names do not remove leading '/' from filenames --recursion directory recursion (default) --suffix=STRING backup before deletion unless set by environment variable SIMPLE_BACKUP_SUFFIX Override, otherwise override common suffix ('') -T, --files-from=FILE 从 FILE get the filename in to extract or create the file --unquote with -T read filename as a reference end (default) -X, --exclude-from=FILE exclude patterns listed in FILE File name change: --strip-components=NUMBER strip NUMBER from filename when unzipping introductory section --transform=EXPRESSION, --xform=EXPRESSION Use sed instead of EXPRESSION to change the file name Filename matching options (affects both exclude and include patterns): --anchored pattern string matching filename header --ignore-case ignore case --no-anchored pattern matches any character after '/' (default pair exclusion is valid) --no-ignore-case match case (default) --no-wildcards match strings verbatim --no-wildcards-match-slash wildcards do not match '/' --wildcards use wildcards (default) --wildcards-match-slash Wildcard match '/' (default is valid for exclude operation) Informative output: --checkpoint[=NUMBER] every NUMBER records showing progress information (10 by default) --checkpoint-action=ACTION execute ACTION on every checkpoint --full-time print file time to its full resolution --index-file=FILE send verbose output to FILE -l, --check-links Print information whenever not all links are output --no-quote-chars=STRING disable character quoting from STRING --quote-chars=STRING Extra quote characters from STRING --quoting-style=STYLE Set name quoting style; valid STYLE See the description below for the values -R, --block-number Each message shows the number of blocks in the archive --show-defaults show tar default options --show-omitted-dirs When listing or unpacking, list each directory that does not match the search criteria --show-transformed-names, --show-stored-names Displays the transformed filename or archivename --totals[=SIGNAL] print the total bytes after processing the archive; when this SIGNAL is triggered with parameters - Print the total number of bytes; allowed signals are: SIGHUP,SIGQUIT,SIGINT,SIGUSR1 和 SIGUSR2; also accepts without SIG Prefixed signal name --utc print file modification time in UTC -v, --verbose verbosely list processed files --warning=KEYWORD warning control: -w, --interactive, --confirmation Require confirmation for every operation Compatibility options: -o when creating an archive, equivalent to --old-archive; when the archive is expanded, it is equivalent to --no-same-owner Other options: -?, --help show this help list --restrict disable some potentially dangerous options --usage display a short usage description --version print program version Long options and corresponding short options have the same mandatory or optional parameters. unless specified with --suffix or SIMPLE_BACKUP_SUFFIX Set the backup suffix, otherwise the backup suffix is "~". Version control can be set with --backup or VERSION_CONTROL, possible values are: none, off never backup t, numbered for numbered backups nil, existing If a numbered backup exists, perform a numbered backup, otherwise perform a simple backup never, simple always use simple backup Valid arguments for the --quoting-style option are: literal shell shell-always c c-maybe escape locale clocale This tar defaults to: --format=gnu -f- -b20 --quoting-style=escape --rmt-command=/etc/rmt --rsh-command=/usr/bin/ssh Send bug reports to <[email protected]>.Content source: https://www.cnblogs.com/newcaoguo/p/5896975.html