Linux command
tar 命令
文件
复制后可按需替换文件名、目录或参数。
常用示例
Create an archive
tar -cvf [archive.tar] [file1] [file2]
Create a gzip-compressed archive
tar -czvf [archive.tar.gz] [directory/]
Create a bzip2-compressed archive
tar -cjvf [archive.tar.bz2] [directory/]
Create an xz-compressed archive
tar -cJvf [archive.tar.xz] [directory/]
Extract an archive
tar -xvf [archive.tar]
Extract a compressed archive
tar -xzvf [archive.tar.gz]
Extract to a specific directory
tar -xvf [archive.tar] -C [directory/]
List contents
tar -tvf [archive.tar]
Extract specific files
tar -xvf [archive.tar] [file1] [file2]
说明
tar (tape archive) creates, extracts, and manages archive files. It bundles multiple files and directories into a single file while preserving permissions, ownership, and directory structure. Tar itself doesn't compress; compression is handled by external programs (gzip, bzip2, xz) invoked via options or automatically based on file extension with -a. Common extensions: .tar (uncompressed), .tar.gz or .tgz (gzip), .tar.bz2 (bzip2), .tar.xz (xz), .tar.zst (zstd). Modern tar auto-detects compression when extracting. The order of options matters with short flags. -cvf works; -fvc archive.tar may not, as -f expects its argument immediately after.
参数
- -c, --create
- Create a new archive
- -x, --extract
- Extract files from archive
- -t, --list
- List archive contents
- -v, --verbose
- Verbose output
- -f _file_, --file=_file_
- Use specified archive file
- -z, --gzip
- Filter through gzip
- -j, --bzip2
- Filter through bzip2
- -J, --xz
- Filter through xz
- -a, --auto-compress
- Determine compression from file extension
- -C _dir_, --directory=_dir_
- Change to directory before operation
- -p, --preserve-permissions
- Preserve file permissions
- --exclude=_pattern_
- Exclude files matching pattern
- -r, --append
- Append files to archive
- -u, --update
- Update archive with newer files
- -k, --keep-old-files
- Don't replace existing files when extracting
- --zstd
- Filter through zstd compression
- -T _FILE_, --files-from=_FILE_
- Read list of files to extract or create from FILE
- -X _FILE_, --exclude-from=_FILE_
- Exclude patterns listed in FILE
- --strip-components=_N_
- Strip N leading path components
- -h, --dereference
- Follow symbolic links; archive the files they point to
- -O, --to-stdout
- Extract files to standard output
FAQ
What is the tar command used for?
tar (tape archive) creates, extracts, and manages archive files. It bundles multiple files and directories into a single file while preserving permissions, ownership, and directory structure. Tar itself doesn't compress; compression is handled by external programs (gzip, bzip2, xz) invoked via options or automatically based on file extension with -a. Common extensions: .tar (uncompressed), .tar.gz or .tgz (gzip), .tar.bz2 (bzip2), .tar.xz (xz), .tar.zst (zstd). Modern tar auto-detects compression when extracting. The order of options matters with short flags. -cvf works; -fvc archive.tar may not, as -f expects its argument immediately after.
How do I run a basic tar example?
Run `tar -cvf [archive.tar] [file1] [file2]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -c, --create do in tar?
Create a new archive