Menu

Compress and Extract whole directory in Linux or Unix with TAR command

Compress :
#tar -zcvf savelocation/fileneme.tar.gz directory-name-you-want-to-compress

#tar -zcvf /tmp/compressedfile.tar.gz /data/folder

Above command will create an archive file named compressedfile.tar.gz under /tmp directory.

Where,

-z : Compress archive using gzip program in Linux or Unix
-c : Create archive on Linux
-v : Verbose i.e display progress while creating archive
-f : Archive File name

We can compress multiple directories too:

#tar -zcvf /tmp/compressedfile.tar.gz /data/ /apps/

Restore:

To restore your archive then you need to use the following command (it will extract all files in current directory):

#tar -zxvf compressedfile.tar.gz

Where, -x: Extract files from given archive

If you wish to extract files in particular directory, for example in /tmp then you need to use the following command:

#tar -zxvf compressedfile.tar.gz -C /tmp

To move the contents from one to another directory.

#mv /path/sourcefolder/* /path/destinationfolder/ or #mv * /data/temp

Moves all folders, including all the contents of those the current directory, to the directory called /data/temp

Loading

Categories:   Linux

Comments