rss logo

tar command with simples examples

Intro

According to wikipedia, tar is a computer software utility for collecting many files into one archive file, often referred to as a tarball, for distribution or backup purposes.

Extract

  • List and search contents of a tar archive:
user@host $ tar -tz -f archive.tar.gz
  • Extract to a specific path:
user@host $ tar -xzvf archive.tar.gz -C /a/folder
  • Extract one or more files:
user@host $ tar -xzvf archive.tar.gz "./arch/file01" "./arch/file02"

Create

  • Create and compress with lzip a tar archive:
root@host:~# apt-get install lzip
user@host $ tar --lzip -cvf archive.tar.lz /a/folder/*
  • Create a tar archive:
user@host $ tar -cvf archive.tar /a/folder/*
  • Create and compress with gzip a tar archive:
user@host $ tar -czvf archive.tar.gz /a/folder/*
  • Create and compress multiple folders to bzip2 archive :
root@host~# tar cpjf backup.tgz /etc /var/log
  • tar archive through ssh:
root@host~# tar -jcvf - /my/fodler/ | ssh root@10.0.0.124 "cat > /backup/$(date +%Y.%m.%d)_backup.tgz"
  • Full system backup from root (/) directory using tar (example filename: /2025.09.10_backup.tar.gz):
root@host~# tar cvzf /$(date +%Y.%m.%d)_backup.tar.gz \
--exclude=/$(date +%Y.%m.%d)_backup.tar.gz \
--exclude=/dev \
--exclude=/mnt \
--exclude=/proc \
--exclude=/run \
--exclude=/sys \
--exclude=/tmp \
--exclude=/media \
--exclude=/lost+found \
/