Mastering the TAR Command: 50 Practical Use Cases for Linux

As a Linux System Administrator at YouStable, tar command is essential for efficiently managing files and directories. tar, short for tape archive, is a powerful utility used to create, maintain, manipulate, and extract files stored in a tape or disk archive.

tar commands for linux

Basic Operations

  • Creating a New Archive: Create an archive file from files or directories.
tar -cvf archive.tar file1 directory1
  • Viewing an Archive Content: List the contents of an archive without extracting it.
tar -tvf archive.tar
  • Extracting an Archive: Extract the contents of an archive to the current directory.
tar -xvf archive.tar
  • Appending Files to Archive: Add new files to an existing archive.
tar -rvf archive.tar newfile
  • Deleting Files from Archive: Remove files from an archive (GNU tar).arduino
tar --delete -f archive.tar file_to_delete

Advanced Archiving

  • Creating a Compressed Archive: Use compression algorithms like gzip or bzip2.
tar -czvf archive.tar.gz directory1 # gzip 
tar -cjvf archive.tar.bz2 directory1 # bzip2
  • Extracting a Compressed Archive: Extract files from a compressed archive.
tar -xzvf archive.tar.gz # gzip 
tar -xjvf archive.tar.bz2 # bzip2
  • Preserving Permissions: Maintain file permissions while archiving.
tar -cpvf archive.tar directory1
  • Incremental Backups: Create backups only of changed files since the last backup.
tar -cvf backup.tar --listed-incremental=/path/to/snapshot.file directory1
  • Creating Split Archives: Split an archive into multiple parts.
tar -cvf - directory1 | split -b 100M - archive.part

File Selection

  • Excluding Files: Create an archive excluding specific files.
tar -cvf archive.tar --exclude='*.txt' directory1
  • Including Files: Archive only files matching a pattern.
tar -cvf archive.tar --include='*.php' directory1
  • Archiving Using a File List: Create an archive based on a list of files.
tar -cvf archive.tar -T filelist.txt
  • Wildcards in File Selection: Use wildcards to specify files or directories.
tar -cvf archive.tar directory1/*.txt

Security and Integrity

tar -cvf - directory1 | gpg -c > archive.tar.gpg
  • Verifying Archive Integrity: Check the integrity of an archive.
tar -cvf archive.tar directory1 --verify

Networking and Remote Operations

  • Creating Archives from Remote Directories: Use SSH to create an archive of a remote directory.sql
ssh user@remote 'tar -cvf - /path/to/directory' > local_archive.tar
  • Extracting Archives to Remote Directories: Extract an archive directly to a remote directory.sql
tar -xvf archive.tar | ssh user@remote 'cd /destination && tar -xvf -'

Archiving Strategies

  • Full System Backup: Create a complete backup of the filesystem.
tar -cvpzf full_backup.tar.gz /
  • Home Directory Backup: Backup the home directory.
tar -cvzf home_backup.tar.gz /home/user
  • Database Backup: Archive a database dump file.
mysqldump -u user -p database_name | gzip > database.sql.gz tar -cvzf database_backup.tar.gz database.sql.gz


  • Archiving Special Files: Archive device files and special files.
tar -cvf archive.tar /dev/sdx
  • Comparing Archive Content with the Filesystem: Check differences between the archive and filesystem.
tar -dvf archive.tar
  • Preserving SELinux Contexts: Archive files with their SELinux context.
tar --selinux -cvf archive.tar

These basic TAR commands are essentials we frequently use. If you have a particular scenario in mind, feel free to comment below. We’ll strive to provide a tailored command response as swiftly as possible.

Power up your business website with YouStable India’s affordable unlimited web hosting services and meet your online success! YouStable is not just a brand that provides hosting services to clients. The quality of services a customer gets from us is unparalleled and our clients know it.

Leave A Comment