Archiving files and directories in linux

Archiving files and directories in linux

commanddescription
gzipcreate a compressed file and not directory
gunzipunzip a file and not directory
tarextract tar file or directory

prerequisites: create a file1 and directory mkdir with below commands:

touch file1
mkdir testdir

to zip a file:

gzip <filename>

this command will replace the source file with <filename>.gz

gzip file1

to unzip a file:

gunzip <filename>

gunzip file1.gz

this command will replace the source file from <filename>.gz to <filename>

to archive a folder:

tar -cvf <dest_filename>.tar <source_file/dirname>

-cvf create verbose file

tar -cvf testdir.tar testdir

this command will create a new extra file <filename>.tar and old file/folder will not be deleted

to unarchive a folder:

tar -xvf <dest_filename>.tar

tar -xvf testdir.tar

if we are executing tar command on a directory, it will create a tar file

Now this tar file can be zipped by gzip command:

gzip <filename>.tar

gzip testdir.tar

this command will replace the testdir.tar file with testdir.tar.gz file

This .gz file can be extracted by:

single command: tar -xvzf <filename>.tar.gz

tar -xvzf testdir.tar.gz #this will create testdir directory

Or by two commands:

unzip the file:

gunzip <filename>.tar.gz

gunzip testdir.tar.gz #this will replace this file with testdir.tar file

this command will create a file <filename>.tar

Unarchive this file:

tar -xvf <filename>.tar

tar -xvf testdir.tar #this will extract and create a directory testdir