Tar example usage and tricks

Tar example usage and tricks

Intro

On this article i will show some examples using the tar, the most used GNU command for compact and compress. That “guy” are called tar, because it comes from the term “Tape Archive”. He can agroup alot of files in one file and we can use some methods for compress and after it, we can call that file of an “tarball”. The tar have alot of methods for manipulation, we can update the file, removing or adding files and the syntax is very simple.

 

  • The example will be based always on my dir ‘distros/‘ that i have prepared for it, look the content of it below
root@matrix:/home/igora/debs/distros# ls -la
total 20
drwxr-xr-x 2 root root 4096 out 30 00:01 .
drwxr-xr-x 3 root root 4096 out 30 00:38 ..
-rw-r--r-- 1 root root    0 out 30 00:25 debian.txt
-rw-r--r-- 1 root root    0 out 30 00:35 fedora.txt
-rw-r--r-- 1 root root    0 out 30 00:01 olarsirs.txt
-rw-r--r-- 1 root root    0 out 30 00:01 solaris.txt
-rw-r--r-- 1 root root    0 out 30 00:55 ubuntu.txt
  • how to create a file using tar with gzip compression
 tar -czvf distros.tar.gz distros/
  • c – for create a new archive
  • z – saying that i will use gzip
  • v – verbose, will list all proccess when compacting and compressing
  • f – i should pass the file name

 

Also Can use

  • J – for xz compression
  • j – for bzip2 compression

 

  • Creating a backup archive with a file controller that gives a possibility for creates a backup Incremental later, starting with first with the full backup

 

root@matrix:/home/igora/debs# tar -g distros.snar -czvf backupDistros.tar.gz distros/*.txt
distros/debian.txt
distros/fedora.txt
distros/olarsirs.txt
distros/solaris.txt
distros/ubuntu.txt
 
root@matrix:/home/igora/debs# stat distros.snar 
    Arquivo: distros.snar
    Tamanho: 35        	Blocos: 8          bloco de E/S: 4096   arquivo comum
Dispositivo: 808h/2056d	Inode: 11796489    Links: 1
     Acesso: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
     Acesso: 2020-10-30 00:17:45.054707573 -0300
Modificação: 2020-10-30 00:17:45.058707564 -0300
  Alteração: 2020-10-30 00:17:45.058707564 -0300
    Criação: -

distros.snar are the file that will control the incremental next backups, they will just get de files that have changed

  • Now i will create an Incremental file, i will add some content on debian.txt and later the Incremental backup should add just debian.txt for that incremental backupDistrosIncremental.tar.gz file
root@matrix:/home/igora/debs# echo -e "Wheezy\nJessie\nStretch\nBuster" > distros/debian.txt 
root@matrix:/home/igora/debs# cat distros/debian.txt 
Wheezy
Jessie
Stretch
Buster
root@matrix:/home/igora/debs# tar -g distros.snar -czvf backupDistrosIncremental.tar.gz distros/*.txt
distros/debian.txt

If you see i have added the content of last four code names of debian that was released into debian.txt, after do that i runned same command, just changed the name file (ofcourse) and tar command just created the file with the debian.txt file.

  • Making sure that incremental file just have the debian.txt file inside it
root@matrix:/home/igora/debs# tar tf backupDistrosIncremental.tar.gz 
distros/debian.txt
  • -t or –list – is for list the contents of an archive
  • -f  – is for pass a file for list it
  • Now i have added a word called “Core” into distros/fedora.txt for change that file and runned Incremental again
    root@matrix:/home/igora/debs# echo "Core" > distros/fedora.txt 
    root@matrix:/home/igora/debs# tar -g distros.snar -czvf backupDistrosIncremental2.tar.gz distros/*.txt
    distros/fedora.txt
    root@matrix:/home/igora/debs#

    If you see just the fedora.txt file was changed, keep mind change the file name for a different name.

  • But if you want use a backup file for do that more organized use this command, thats a gift from debian-pb
  • I Just need add some content at one of files at distros/ for Incremental works fine before run the most important command for send the backup file
root@matrix:/home/igora/debs# echo "xenial" > distros/ubuntu.txt
root@matrix:/home/igora/debs# tar -g distros.snar -zcf - distros/*.txt | ssh [email protected] "( cd /home/backupDistros/ ; cat >; BackupIncremental3.tar.gz )"

 

  • Now i just wanna check at the “remote” server if that ubuntu.txt file is there
    root@docker:/home/backupDistros# tar tf BackupIncremental3.tar.gz 
    distros/ubuntu.txt
    root@docker:/home/backupDistros#
  • If you need something more simple, just to agroup the files without any compression
tar cf - distros/ | ssh root@192.168.15.25 'cat >; /home/backupDistros/tarexampledistros.tar'

On the remote server we take a look on the content 🙂

root@docker:/home/backupDistros# tar -tf tarexampledistros.tar 
distros/
distros/solaris.txt
distros/fedora.txt
distros/ubuntu.txt
distros/debian.txt
distros/olarsirs.txt
  • If we need extract the tar.gz we just need
root@matrix:/home/igora/debs# tar -zxvf distros.tar.gz
distros/debian.txt
distros/fedora.txt
distros/olarsirs.txt
distros/solaris.txt
distros/ubuntu.txt
  • But, if i just need debian.txt from it?
root@matrix:/home/igora/debs# tar -xzvf distros.tar.gz distros/debian.txt
distros/debian.txt
root@matrix:/home/igora/debs# 
root@matrix:/home/igora/debs/distros# stat debian.txt 
    Arquivo: debian.txt
    Tamanho: 29        	Blocos: 8          bloco de E/S: 4096   arquivo comum
Dispositivo: 808h/2056d	Inode: 11796484    Links: 1
     Acesso: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
     Acesso: 2020-10-30 09:01:56.926486554 -0300
Modificação: 2020-10-30 00:25:02.000000000 -0300
  Alteração: 2020-10-30 09:01:56.926486554 -0300
    Criação: -
root@matrix:/home/igora/debs/distros# date
sex out 30 09:02:27 -03 2020
  • -z – gzip compresssion for read the archive
  • -x – extract the file
  • -v – verbose for show all progress of extracting
  • -f – read the file that i pass at the command

 

  • If you want add some files into a tar file that are not compressed just
root@matrix:/home/igora/debs# tar -rf distros.tar distros/mint.txt 
root@matrix:/home/igora/debs# tar -tf distros.tar
distros/debian.txt
distros/fedora.txt
distros/olarsirs.txt
distros/solaris.txt
distros/ubuntu.txt
distros/mint.txt
  • But if you want remove something on a tar file without compression, just
root@matrix:/home/igora/debs# tar -f distros.tar --delete distros/fedora.txt
root@matrix:/home/igora/debs# tar -tf distros.tar
distros/debian.txt
distros/olarsirs.txt
distros/solaris.txt
distros/ubuntu.txt
distros/mint.txt
root@matrix:/home/igora/debs#

If you see above fedora.txt was gone.

  • -t – list the files
  • -r – append new files to the end of file
  • –delete – remove files from the tarball

Ya its rocks! If have any doubt just send a comment below or email me [email protected]

igorandrade

an IT & Infosec Expert, regex pro player and devops skills to made everything happens fast.

Deixe uma resposta