This is a draft cheat sheet. It is a work in progress and is not finished yet.
Zip Files
tar -cvf sample1.tar /home/sample1dir/
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 "sample1.tar" file by zipping "/home/sample1dir/" directory. |
gzip archive
To create a compressed gzip archive file we use the option as z:
tar cvzf sample2.tar.gz /home/sample2dir
or
tar cvzf sample3.tgz /home/sample3dir |
tar.bz2 archive
The bz2 feature compress and create archive file less than the size of the gzip. The bz2 compression 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 sample4.tar.bz2 /home/sample4
OR
tar cvfj sample5.tar.tbz /home/sample5
OR
tar cvfj sample6.tar.tb2 /home/sample6 |
Add Files or directories to tar Archive files
To add files or directories to existing tar archived file we use the option r (append)
tar -rvf sample1.tar xyz.txt |
untar archive file
To untar or extract a tar file, just issue following command using option x (extract).
tar -xvf sample1.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 <specified_directory>"
tar -xvf samle1.tar -C /targetDir/ |
Uncompress Files
tar -xvf sample2.tar.gz |
Uncompress tar.gz files |
tar -xvf sample3.tar.bz2 |
Uncompress tar.bz2 files |
tar -tvf sample1.tar |
Lists content of tar file (t- list content) |
tar -tvf sample2.tar.gz |
Lists content of tar.gz file (t- list content) |
tar -tvf sample3.tar.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 sample1.tar process.sh
(or)
tar --extract --file=sample1.tar process.sh
For *.tar.gz:
tar -zxvf codedir.tar.gz pom.xml
(or)
tar --extract --file=codedir.tar.gz pom.xml
For *.tar.bz2
tar -jxvf phpfiles.tar.bz2 home/php/index.php
(or)
tar --extract --file=phpfiles.tar.bz2 /home/php/index.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 sample1.tar "file 1" "file 2"
tar -zxvf sample2.tar.gz "file 1" "file 2"
tar -jxvf sample3.tar.bz2 "file 1" "file 2" |
Extract group of files using wildcard
tar -xvf sample1.tar --wildcards '*.php'
tar -zxvf sample2.tar.gz --wildcards '*.php'
tar -jxvf sample3.tar.bz2 --wildcards '*.php' |
Check size of tar file
Following command shows the size of archive file in KB:
tar -czf - sample1.tar | wc -c
tar -czf - sample2.tar.gz | wc -c
tar -czf - sample3.tar.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 directories to existing archive file.
W – Verify a archive file.
wildcards – Specify patterns in unix tar command |
|