CloudsArk
Commands Linux

What Is the tar Command in Linux?

Learn what the tar command does in Linux, how its syntax works, and when to use it.

What Is the tar Command in Linux?

Introduction

The tar command creates, lists, and extracts archive files. It is useful for beginners, Linux administrators, DevOps engineers, and RHCSA students because it solves practical terminal tasks.

What the Command Does

Use tar to work with the specific Linux object it manages. Before changing anything, identify the target and run a read-only check when possible.

Basic Syntax

tar OPTIONS ARCHIVE FILES

The syntax includes the command, any options, and the target object.

Common Options

  • -c: create an archive.
  • -x: extract an archive.
  • -t: list archive contents.

Practical Examples

tar -cf logs.tar /var/log
tar -tf logs.tar
tar -xf logs.tar
tar -czf app-backup.tar.gz /srv/app

Verification command:

tar -tf app-backup.tar.gz | head

Example output:

srv/app/
srv/app/app.conf
srv/app/bin/start.sh

When to Use This Command

Use tar when you need to bundle files for backup, transfer, or deployment. Add compression when the archive must be smaller for storage or network transfer.

Common Mistakes

  • Forgetting -f before the archive filename.
  • Extracting an archive in the wrong directory and scattering files.
  • Assuming tar compression is enabled unless you specify an option such as -z.

Quick Reference

tar -cf logs.tar /var/log
tar -tf logs.tar
tar -tf app-backup.tar.gz | head

Summary

The tar command is safest when you understand the target, choose the right option, and verify the result with a separate command.