tar
Overview
The tar command is used to create, maintain, modify, and extract files that are archived in the tar format. It’s commonly used for backing up files or creating distributions.
Syntax
tar [options] [archive-file] [file or directory to archive]Common Options
| Option | Description |
|---|---|
-c |
Create a new archive |
-x |
Extract files from an archive |
-f |
Use archive file |
-v |
Verbosely list files processed |
-z |
Filter the archive through gzip |
-j |
Filter the archive through bzip2 |
-t |
List the contents of an archive |
-r |
Append files to the end of an archive |
-u |
Only append files that are newer than copy in archive |
Key Use Cases
- Creating backups of files and directories
- Distributing collections of files and directories
- Archiving old data
- Creating software distributions
Examples with Explanations
Example 1: Create a tar archive
tar -cvf archive.tar /path/to/directoryCreates a new archive named ‘archive.tar’ containing all files in /path/to/directory
Example 2: Create a compressed tar archive (tarball)
tar -czvf archive.tar.gz /path/to/directoryCreates a gzip-compressed tar archive
Example 3: Extract files from an archive
tar -xvf archive.tarExtracts all files from archive.tar to the current directory
Understanding Output
When using the verbose option (-v): - Each line shows a file being processed - Format: permissions owner/group size date time filename
Common Usage Patterns
Creating compressed archives:
tar -czvf archive.tar.gz files/Extracting compressed archives:
tar -xzvf archive.tar.gzListing contents:
tar -tvf archive.tar
Performance Analysis
- Use
-zfor better compression but slower processing - Use multiple cores with
--use-compress-program=pigz - Avoid compressing already compressed files (like .jpg, .mp3)