How to tar and untar files and folders in Linux

Here is a quick cheat sheet how to compress and extract files/folders in CentOs Linux.  If you are extracting to a specific folder you have to create the folder ahead of time with mkdir /tmp/data/ or the command will not work.

TAR.GZ FILES

Create a compressed tar.gz file of Documents folder and all sub-directories
$ tar -zcvf /tmp/Myfolder.tar.gz /home/username/Documents/

Extract a compressed tar.gz into a folder
$ tar -zxvf Myfolder.tar.gz -C /tmp/data/

View files inside a compressed tar.gz file
$ tar –tzvf Myfolder.tar.gz

Extract specific files from a tar.gz file using wildcard into a specific folder
$ tar -zxvf Myfolder.tar.gz –wildcards “*.jpg” -C /tmp/data/
$ tar -zxvf Myfolder.tar.gz etc/file1 etc/mysql/ -C /restoredfiles/

TAR FILES

Create a plain uncompressed tar file/folder
$ tar -cvf /tmp/Myfolder.tar /home/username/Documents/

Extract a plain tar file contents into a folder
$ tar -xvf Myfolder.tar -C /tmp/data/

View files inside a plain .tar file
$ tar –tvf Myfolder.tar

Extract specific files from a tar file using wildcard
$ tar -xvf Myfolder.tar –wildcards “*.jpg” -C /tmp/data

Extract a set of files from a tar file into a folder
$ tar -xvf Myfolder.tar file1 etc/file2 /etc/dir1/ -C /tmp/data

BZ Files (.bz2 .tbz .tbz2 .tb2)

Extract contents into a directory
$ tar -jxvf Myfolder.tar.bz2 -C /tmp/data

 

 

This entry was posted in Linux and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *