How to create tar.gz file in Linux using command line
02 Aug 2024 - 2 min readThe tar
command in Linux and Unix-like operating systems is used to create compressed and uncompressed archive files. It is a versatile utility that can combine multiple files and directories into a single archive file, which can then be compressed to save space or to make it easier to transfer. Below, I’ll explain how to use tar
to compress and uncompress files with examples.
Table of Contents
Basic tar
Commands
Compress (Create Archive)
The basic syntax to create a tar.gz
archive is:
Uncompress (Extract Archive)
To extract an archive, use the following syntax:
Common Use Cases
Archive and Compress Multiple Directories:
This command archives and compresses multiple directories into a single file.
List the Contents of an Archive:
This will list the contents of the specified archive without extracting them.
Add Files to an Existing Archive:
Adds a new file to an existing archive.
Exclude Specific Files/Directories:
Excludes all .log files from the archive.
Summary Table
Operation | Command Example |
---|---|
Create Uncompressed Archive | tar -cvf archive.tar /path/to/directory |
Create Gzip Compressed Archive | tar -cvzf archive.tar.gz /path/to/directory |
Create Bzip2 Compressed Archive | tar -cvjf archive.tar.bz2 /path/to/directory |
Create XZ Compressed Archive | tar -cvJf archive.tar.xz /path/to/directory |
Extract Uncompressed Archive | tar -xvf archive.tar |
Extract Gzip Compressed Archive | tar -xvzf archive.tar.gz |
Extract Bzip2 Compressed Archive | tar -xvjf archive.tar.bz2 |
Extract XZ Compressed Archive | tar -xvJf archive.tar.xz |
Extract to Specific Directory | tar -xvzf archive.tar.gz -C /path/to/destination |
List Contents of Archive | tar -tvf archive.tar |
Add Files to Existing Archive | tar -rvf archive.tar new-file |
Exclude Files/Directories | tar --exclude='*.log' -cvzf archive.tar.gz /path |
These commands provide a comprehensive guide to using tar for compressing and uncompressing files and directories. With tar, you can easily manage file archives and compress them for efficient storage and transfer.