Show Menu
Cheatography

Developer Cheatsheet Cheat Sheet (DRAFT) by

For Developers

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Zip Files

tar -cvf sample­1.tar /home/­sam­ple­1dir/
where,
c – Creates a new .tar archive file.
v – Verbosely show the .tar file progress.
f – File name type of the archive file.

Above command creates a "­sam­ple­1.t­ar" file by zipping "­/ho­me/­sam­ple­1di­r/" directory.

gzip archive

To create a compressed gzip archive file we use the option as z:
tar cvzf sample­2.t­ar.gz /home/­sam­ple2dir
or
tar cvzf sample­3.tgz /home/­sam­ple3dir

tar.bz2 archive

The bz2 feature compress and create archive file less than the size of the gzip. The bz2 compre­ssion takes more time to compress and decompress files as compared to gzip which takes less time. To create highly compressed tar file we use option as j.

tar cvfj sample­4.t­ar.bz2 /home/­sample4
OR
tar cvfj sample­5.t­ar.tbz /home/­sample5
OR
tar cvfj sample­6.t­ar.tb2 /home/­sample6

Add Files or direct­ories to tar Archive files

To add files or direct­ories to existing tar archived file we use the option r (append)
tar -rvf sample­1.tar xyz.txt

untar archive file

To untar or extract a tar file, just issue following command using option x (extract).

tar -xvf sample­1.tar
This command extracts the file in current directory

untar file in different directory

To untar in a different directory then use option as "-C <sp­eci­fie­d_d­ire­cto­ry>­"
tar -xvf samle1.tar -C /targe­tDir/

Uncompress Files

tar -xvf sample­2.t­ar.gz
Uncompress tar.gz files
tar -xvf sample­3.t­ar.bz2
Uncompress tar.bz2 files
tar -tvf sample­1.tar
Lists content of tar file (t- list content)
tar -tvf sample­2.t­ar.gz
Lists content of tar.gz file (t- list content)
tar -tvf sample­3.t­ar.bz2
Lists content of tar.bz2 file
 

Untar single file from tar archive file

To extract only specific file from archive file:
For *.tar:
tar -xvf sample­1.tar process.sh
(or)
tar --extract --file­=sa­mpl­e1.tar process.sh

For *.tar.gz:
tar -zxvf codedi­r.t­ar.gz pom.xml
(or)
tar --extract --file­=co­ded­ir.t­ar.gz pom.xml

For *.tar.bz2
tar -jxvf phpfil­es.t­ar.bz2 home/p­hp/­ind­ex.php
(or)
tar --extract --file­=ph­pfi­les.ta­r.bz2 /home/­php­/in­dex.php

Untar multiple files

To extract or untar multiple files from the tar, tar.gz and tar.bz2 archive file. For example the below command will extract “file 1” “file 2” from the archive files.

tar -xvf sample­1.tar "file 1" "file 2"
tar -zxvf sample­2.t­ar.gz "file 1" "file 2"
tar -jxvf sample­3.t­ar.bz2 "file 1" "file 2"

Extract group of files using wildcard

tar -xvf sample­1.tar --wild­cards '*.php'
tar -zxvf sample­2.t­ar.gz --wild­cards '*.php'
tar -jxvf sample­3.t­ar.bz2 --wild­cards '*.php'

Check size of tar file

Following command shows the size of archive file in KB:
tar -czf - sample­1.tar | wc -c
tar -czf - sample­2.t­ar.gz | wc -c
tar -czf - sample­3.t­ar.bz2 | wc -c

Tar command attributes

c – create a archive file.
x – extract a archive file.
v – show the progress of archive file.
f – filename of archive file.
t – viewing content of archive file.
j – filter archive through bzip2.
z – filter archive through gzip.
r – append or update files or direct­ories to existing archive file.
W – Verify a archive file.
wildcards – Specify patterns in unix tar command