tar Command Examples in Linux¶
Introduction¶
These examples show practical ways to use tar on a Linux terminal. Each example is written so you can adapt it for administration or troubleshooting.
Example 1: Basic Usage¶
tar -cf logs.tar /var/log
This is the simplest form of the command and is a good starting point before adding options.
Example 2: Common Admin Task¶
tar -tf logs.tar
This example reflects a common task on RHEL, Rocky Linux, AlmaLinux, or similar systems.
Example 3: Useful Option¶
tar -xf logs.tar
This option helps narrow the result, change behavior, or handle a more realistic target.
Example 4: Real-World Scenario¶
tar -czf app-backup.tar.gz /srv/app
Use this pattern when the task moves beyond a single basic command.
Example 5: Verification¶
tar -tf app-backup.tar.gz | head
Example output:
srv/app/
srv/app/app.conf
srv/app/bin/start.sh
Common Mistakes¶
- Forgetting
-fbefore 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 -xf logs.tar
tar -czf app-backup.tar.gz /srv/app
tar -tf app-backup.tar.gz | head
Related Guides¶
- What is tar?
- tar create and extract explained
- tar Compress and Exclude Files
- tar interview questions
Summary¶
Good tar usage means choosing the right option, keeping the target clear, and verifying the result with output you can explain.