How to use of tar command in Linux – compressing and archiving files

Use of Tar command is done for archiving large number of files. Tar can create archive with extension tar or tar can use gzip or bzip2 for compression. When gzip or bzip2 is used along with tar command a single archive file (.tar) is created which is converted into .gz or bz2 files. It works recursively can compress directories inside directory.

Syntax for use of tar command

tar <option> <file>

Option available for use with tar command

-c                            creates a new archive file

-d                            Find differences between the archive and file system

-f                             Provide the archive file name to use

-p                            Preserves files and directory permissions

-t                             Lists the files in an archive(tar) file

-v                            verbose output / show progress of process

-z                            compresses tar file with gzip

-j                             compresses tar file with bzip2

-x                            extract file from archive file

Archiving multiple files

Generally tar is used to backup multiple files. Tar does not compress file but you can use gzip or bz2

Archive file using gzip

tar -czvf archive-name.tar.gz /path

syntax uses switch c (create new archive file), z (compresses tar file with gzip), v (verbose) and f ( provide archive name)

Also see How to compress file using gzip command in Linux