Working with tar
tar
stands for Tape Archive.- Used to archive multiple files/directories into a single file.
- Commonly used for backup, compression, and packaging.
- Output is usually a
.tar
,.tar.gz
,.tgz
, or.tar.bz2
file.
Basic Syntax
tar [options] [archive-file] [file or directory to archive]
Common Options
Option | Description |
---|---|
-c | Create new archive |
-x | Extract from archive |
-t | List contents of archive |
-v | Verbose (show file names) |
-f | File name of archive |
-z | Compress with gzip (.tar.gz , .tgz ) |
-j | Compress with bzip2 (.tar.bz2 ) |
-J | Compress with xz (.tar.xz ) |
Create an Archive
tar -cvf archive.tar file1 file2 dir/
- Creates
archive.tar
with specified files/directories -c
: create,-v
: verbose,-f
: archive name
Create Compressed Archive
tar -czvf archive.tar.gz file1 dir/
- Uses gzip compression (
-z
)
tar -cjvf archive.tar.bz2 file1 dir/
- Uses bzip2 compression (
-j
)
Extract Archive
tar -xvf archive.tar
- Extracts contents into current directory
tar -xzvf archive.tar.gz
- Extracts
.tar.gz
archive
tar -xjvf archive.tar.bz2
- Extracts
.tar.bz2
archive
Extract to Specific Directory
tar -xvf archive.tar -C /path/to/dir
List Archive Contents
tar -tvf archive.tar
Add File to Existing Archive (not compressed)
tar -rvf archive.tar newfile.txt
Extract Single File
tar -xvf archive.tar file1.txt
Summary
Task | Command Example |
---|---|
Create .tar | tar -cvf archive.tar files/ |
Create .tar.gz | tar -czvf archive.tar.gz files/ |
Extract .tar | tar -xvf archive.tar |
Extract .tar.gz | tar -xzvf archive.tar.gz |
List contents | tar -tvf archive.tar |
Extract to directory | tar -xvf archive.tar -C /target/path |
Last updated on