If you are a Linux user, chances are you’ve come across the term tar file (often ending in .tar, .tar.gz, .tar.bz2, or .tar.xz). A tarball is simply an archive that combines multiple files or directories into one, making it easier to store, transfer, or back up data. Think of it as the Linux equivalent of a ZIP file.


In this guide, we’ll explain, in simple steps, how to create, view, and extract tar files using the Linux terminal. No advanced knowledge is required.

Prerequisites: 

  • A Linux system (works on most distributions such as Ubuntu, CentOS, Debian, Fedora, etc.) 
  • Access to the terminal 
  • tar command (pre-installed on most Linux systems)


Basic Syntax of the tar Command

The general format is:
tar [options] archive-name.tar file-or-directory


Common options:

  • -c → Create a new archive
  • -x → Extract files from an archive
  • -v → Show progress in the terminal (verbose mode)
  • -f → Specify the archive file name
  • -z → Compress with gzip
  • -j → Compress with bzip2
  • --xz → Compress with xz
  • --zstd → Compress with zstd

 

How to Create a Tar File in Linux

1. Compress an Entire Directory

tar -czvf backup.tar.gz /path/to/directory/
backup.tar.gz → Name of the archive
/path/to/directory/ → Folder you want to compress

2. Compress a Single File

tar -czvf archive.tar.gz file.txt

3. Compress Multiple Files or Directories

tar -czvf archive.tar.gz dir1 dir2 file1.txt

4. Use Other Compression Methods

bzip2:

tar -cjvf archive.tar.bz2 /path/to/dir/

xz:

tar --xz -cvf archive.tar.xz /path/to/dir/

zstd:

tar --zstd -cvf archive.tar.zst /path/to/dir/


Excluding Files and Folders

You can tell tar to skip certain files or directories:

tar -czvf archive.tar.gz /home/user/ --exclude="Downloads"


View Contents of a Tar File (Without Extracting)

tar -tvf archive.tar.gz

This will list all files stored inside the archive.


How to Extract a Tar File

1. Extract in the Current Directory

tar -xzvf archive.tar.gz

2. Extract into a Specific Directory

tar -xzvf archive.tar.gz -C /path/to/destination/

3. Extract Other Formats

bzip2:

tar -xjvf archive.tar.bz2

xz:

tar --xz -xvf archive.tar.xz

zstd:

tar --zstd -xvf archive.tar.zst


Creating an Archive Without Compression

If you just want a .tar file without compression:

tar -cvf archive.tar /path/to/files/

Extract it later using:

tar -xvf archive.tar


Conclusion

The tar command is one of the most essential tools in Linux for archiving, compressing, and extracting files. Whether you’re creating backups, transferring projects, or packaging software, knowing how to use tar will save you time and effort.

If you need professional assistance with Linux server management, troubleshooting, our team is here to help. Feel free to contact us for expert support.

 

Hjalp dette svar dig? 0 Kunder som kunne bruge dette svar (0 Stem)