Tar Command Examples

This page provides few examples of tar command which will be useful.
Lets create a folder named justgeek and some files in it.

$ mkdir justgeek
$ cd justgeek/
$ touch 1.txt 2.txt 3.txt 4.txt 5.txt

To create a new tar archive.

$ tar -czvf justgeek.tar.gz justgeek
justgeek/
justgeek/1.txt
justgeek/2.txt
justgeek/4.txt
justgeek/3.txt
justgeek/5.txt

Here is a another command you can use, this can command can be called a shortcut to create a tar file. It will add .tar.gz to the same the folder.

$ tar -czvf justgeek{.tar.gz,}
justgeek/
justgeek/1.txt
justgeek/2.txt
justgeek/4.txt
justgeek/3.txt
justgeek/5.txt

How to extract a tar archive

$ tar -xvf justgeek.tar.gz
justgeek/
justgeek/1.txt
justgeek/2.txt
justgeek/4.txt
justgeek/3.txt
justgeek/5.txt

To view the contents of tar archive without extracting it.

$ tar -tvf justgeek.tar.gz
drwxrwxr-x justgeek/justgeek 0 2019-04-03 13:15 justgeek/
-rw-rw-r-- justgeek/justgeek 0 2019-04-03 13:15 justgeek/1.txt
-rw-rw-r-- justgeek/justgeek 0 2019-04-03 13:15 justgeek/2.txt
-rw-rw-r-- justgeek/justgeek 0 2019-04-03 13:15 justgeek/4.txt
-rw-rw-r-- justgeek/justgeek 0 2019-04-03 13:15 justgeek/3.txt
-rw-rw-r-- justgeek/justgeek 0 2019-04-03 13:15 justgeek/5.txt

Leave a Reply

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