📦 Command: tar (Tape Archive)
The tar command is the standard Linux utility for collecting many files into a single archive file, often called a "tarball." While tar itself only groups files together, it is almost always used with compression (like gzip or xz) to reduce file size.
1. The "Why"
In your workflow as a developer and researcher, you deal with complex folder structures.
- Project Packaging: Bundle your source code and assets into one file to send to a colleague or upload to a server.
- System Backups: Create a compressed backup of your Arch Linux
.dotfilesor Hyprland configurations. - Software Installation: Many Linux programs are distributed as
.tar.gzor.tar.xzfiles. You need to know how to "explode" these to install the software. - Research Archiving: Store large datasets in a single, space-saving file.
2. Basic Syntax
The tar command is famous for its specific flags. A common way to remember them is the "Action-File" rule.
tar [OPTIONS] [ARCHIVE_NAME] [TARGET_FILES/FOLDERS]
3. The "Big Three" Operations
A. Creating an Archive (-c)
To bundle a folder into a compressed archive (using gzip compression, indicated by -z):
tar -cvzf backup.tar.gz ./my_project
- c: Create a new archive.
- v: Verbose (shows you which files are being added).
- z: Use gzip compression (makes the file smaller).
- f: File (tells tar the next argument is the name of the archive).
B. Extracting an Archive (-x)
To "unzip" or unpack an archive:
tar -xvzf backup.tar.gz
- x: Extract the contents.
C. Listing Contents (-t)
To see what's inside a tarball without actually extracting it:
tar -tvf backup.tar.gz
4. Choosing Your Compression
Linux supports different compression algorithms. You choose them by changing one letter in the flags:
| Flag | Extension | Algorithm | Best For... |
|---|---|---|---|
-z |
.tar.gz |
Gzip | Fast compression/extraction. Most common. |
-j |
.tar.bz2 |
Bzip2 | Better compression than Gzip, but slower. |
-J |
.tar.xz |
XZ | Best compression ratio. Slowest, but creates the smallest files. |
5. Practical Examples
1. Backing up your e-book project (High compression):
tar -cvJf ebook_draft.tar.xz ./ecommerce-blueprint
2. Extracting to a specific folder:
tar -xvzf project.tar.gz -C /home/userName/dev/
3. Excluding specific files (like node_modules or .git):
tar --exclude='./project/.git' -cvzf project.tar.gz ./project
6. Pro-Tips
- The "f" must be last: In the string of flags (like
cvzf), thefmust always be the last letter because it expects the filename to follow immediately. - Modern Tar: On modern Arch Linux systems,
taris smart enough to detect the compression type automatically. You can often just runtar -xf file.tar.xzand it will work without needing theJflag. - Arch Linux Context: When you download a package from the AUR (Arch User Repository) manually, it usually comes as a
.tar.gz. You extract it,cdinto the folder, and runmakepkg -si.
7. Summary Reference
| Goal | Command |
|---|---|
| Create Compressed Archive | tar -cvzf name.tar.gz folder/ |
| Extract Archive | tar -xvzf name.tar.gz |
| List Contents | tar -tvf name.tar.gz |
| Extract to specific path | tar -xvf name.tar.gz -C /path/ |