YouTip LogoYouTip

Linux Comm Tar

[![Image 1: Linux Command Manual](#) Linux Command Manual](#) # Linux tar Command **Source:** The Linux tar (English full name: tape archive) command is used to back up files. tar is a powerful command-line tool in Linux and Unix systems for archiving files and directories. The name tar comes from "tape archive," originally used to pack files onto tape devices, but now widely used to pack and compress files within a file system. tar is commonly used to bundle multiple files and directories into a single archive file, known as a "tarball" (usually with a .tar extension). tar itself does not compress files, but it can be combined with compression tools (such as gzip or bzip2) to create compressed archive files (such as .tar.gz or .tar.bz2). ### Syntax tar -f archive.tar [files...] * `-f archive.tar`: Specifies the name of the archive file. * `[files...]`: The files and directories to be packed. ### Options **Basic Operation Options** * `-c`: Create a new archive file. * `-x`: Extract an archive file. * `-t`: List the contents of an archive file. * `-r`: Append files to an existing archive file. * `-u`: Only append files that are newer than the files already in the archive. * `-d`: Find differences between the archive file and the file system. * `-A`: Append one `.tar` file to another `.tar` file. **File Selection and Exclusion** * `-f `: Specifies the name of the archive file (must be placed at the end of the option list). * `-C `: Switch to the specified directory for the operation. * `--exclude=`: Exclude files matching the specified pattern. * `--exclude-from=`: Read patterns to exclude from the specified file. * `--exclude-caches`: Exclude cache files in directories. * `--exclude-backups`: Exclude backup files ending with `~`. * `--exclude-vcs`: Exclude files generated by version control systems (such as `.git`, `.svn`, etc.). **Compression and Decompression Options** * `-z`: Compress the archive using `gzip`. * `-j`: Compress the archive using `bzip2`. * `-J`: Compress the archive using `xz`. * `--lzip`: Compress the archive using `lzip`. * `--lzma`: Compress the archive using `lzma`. * `--lzop`: Compress the archive using `lzop`. * `--zstd`: Compress the archive using `zstd`. * `-a`: Automatically select the compression method (based on the archive file's extension, such as `.tar.gz`, `.tar.bz2`, etc.). * `-I `: Use the specified compression program for compression or decompression. **Output and Interaction Options** * `-v`: Display detailed operation process (verbose). * `--progress`: Show a progress bar (when used with `-v`). * `-w` or `--interactive`: Ask the user for confirmation before each operation. * `--checkpoint`: Display a checkpoint after processing each file. * `--checkpoint-action=`: Perform the specified action at checkpoints, such as `echo`, `dot`, etc. * `--totals`: Display the total number of bytes processed after the operation ends. * `--verbose`: Display detailed processing information. * `--quiet`: Minimize output information. **File and Permission Related Options** * `-p`: Preserve the original permissions of files (when extracting). * `--same-owner`: Try to set extracted files to the original owner (requires superuser privileges). * `--no-same-owner`: Do not set the file owner. * `--same-permissions`: Preserve the original permissions of files (same as `-p`). * `--no-same-permissions`: Do not preserve original permissions; use the current user's umask to set permissions. * `-m`: Do not restore the modification time of files when extracting; use the current time instead. **Archive Management Options** * `-k` or `--keep-old-files`: Preserve existing files when extracting; do not overwrite. * `--overwrite`: Force overwrite existing files when extracting. * `--remove-files`: Delete original files after successful archiving. * `--delete`: Delete specified files from the archive (only available in `gnu tar`). * `--keep-newer-files`: Preserve files that are newer than those in the archive when extracting. * `--listed-incremental=`: Create or restore an incremental backup. **File System and Device Options** * `-L `: Split archive files larger than `N` bytes (for tape drives). * `--tape-length=`: Specify the tape length (for tape drives). * `--multi-volume`: Create or restore multi-volume archive files. * `-M`: Used with `--multi-volume` to process multi-volume archive files. * `--use-compress-program=`: Use the specified compression program. **Other Useful Options** * `--transform=`: Rename files within the archive. * `--strip-components=`: Strip the specified number of path components when extracting. * `--ignore-failed-read`: Ignore read errors and continue the operation. * `--occurrence=`: Select the nth occurrence of a file in the archive. * `-S`: Handle sparse files (only archive actually used blocks). * `--no-recursion`: Do not recurse into directories. * `-h` or `--dereference`: Archive the files that symbolic links point to rather than the links themselves. **Help and Version Information** * `--help`: Display help information. * `--version`: Display the version information of `tar`. ### Examples **1. Create an archive file:** Pack files file1, file2, and directory into an archive file named archive.tar. tar -cvf archive.tar file1 file2 directory * `-c`: Create a new archive file * `-v`: Display verbose output, listing the files being added to the archive * `-f`: Specify the name of the archive file **2. Extract an archive file:** Extract the archive file named archive.tar, restoring the files and directories it contains. tar -xvf archive.tar * `-x`: Extract the archive file * `-v`: Display verbose output, listing the files being extracted * `-f`: Specify the name of the archive file to extract **3. Compress an archive file:** Pack the directory named "directory" into an archive file, then compress it using gzip, producing a file named archive.tar.gz. tar -czvf archive.tar.gz directory * `-c`: Create a new archive file * `-z`: Compress the archive using gzip * `-v`: Display verbose output, listing the files being added to the archive * `-f`: Specify the name of the archive file **4. List the contents of an archive file:** List all files and directories contained in the archive file named archive.tar. tar -tvf archive.tar * `-t`: List the contents of the archive file * `-v`: Display verbose output, listing all files and directories in the archive * `-f`: Specify the name of the archive file whose contents to list **5. Append files to an existing archive:** Add the file named newfile to the existing archive file named archive.tar. tar -rvf archive.tar newfile * `-r`: Append files to an existing archive * `-v`: Display verbose output, listing the files being added to the archive * `-f`: Specify the name of the existing archive file **6. Create a gzip-compressed archive:** Pack all files and subdirectories under the "directory" directory and compress them using gzip, producing an archive file named archive.tar.gz. tar -zcvf archive.tar.gz directory * `-z`: Indicates that gzip should be used for compression * `-c`: Indicates creating a new archive file * `-v`: Indicates verbose output, listing the files being added to the archive * `-f archive.tar.gz`: Specifies the archive file name as `archive.tar.gz` **7. Extract a gzip-compressed archive:** Extract the file example.tar.gz, restoring the files and directories it contains in the current directory. tar -zxvf example.tar.gz * `-z`: Indicates that gzip should be used to decompress the archive * `-x`: Indicates an extraction operation * `-v`: Indicates verbose output, listing the files being extracted * `-f example.tar.gz`: Specifies the archive file name to extract as `example.tar.gz` ### Specifying Compression Formats tar can be combined with different compression programs to create and extract compressed archive files. z: Use gzip compression. tar -czvf archive.tar.gz directory tar -xzvf archive.tar.gz j: Use bzip2 compression. tar -cjvf archive.tar.bz2 directory tar -xjvf archive.tar.bz2 J: Use xz compression. tar -cJvf archive.tar.xz directory tar -xJvf archive.tar.xz [![Image 2: Linux Command Manual](#) Linux Command Manual](#)
← Os MakedirsLinux Comm Restore β†’