How to Compress file in Linux

Compress file in Linux

Tar command in Linux

In Linux, Tar command is used to reduce the file size

Tar related Options

$ tar –help

OptionsDescription
-c–createCreate a new archive.
-x–extractExtract files from an archive.
-t–listList the contents of an archive.
-f–file=ARCHIVEUse archive file or dir ARCHIVE.
-v–verboseVerbosely list files processed.

Compression Options with Tar

OptionsDescription
-a–auto-compressUse archive suffix to determine the compression program.
-j–bzip2Filter the archive through bzip2.
-J-xz –lzmaFilter the archive through xz.
-z–gzipFilter the archive through gzip.

Compress Directory with option ‘-cf’

$ tar -cf <folder-name.tar> <folder-name>

In below example, i compressed Drupa8-vagrant directory with Drupa8.tar name.

Verbose output shows which files and directories are added to the archive, use the -v option

Compress Directory with Option ‘-v’

$ tar -cvf <folder-name.tar> <folder-name>

Ex:-

Compress Directory with Option ‘-z’

For archiving a folder compressed ‘gzip’, you have to use the -z option

$ tar -czf <folder-name.tar.gz> <folder-name>

Compress Directory with Option ‘-j’

You can instead compress the archive with ‘bzip2’, by using the -j option

$ tar -cjf <folder-name.tar.bz2> <folder-name>

Ex:-

Compress Directory with Option ‘-J’

$ tar -cJf <folder-name.tar.xz> <folder-name>

Ex:-

Extract directory from archive

$ sudo tar -xf <folder-name.tar>

List the Compressed data

List the contents of an archive file without extracting it:

$ sudo tar -tf <folder-name.tar.bz2>

Leave a Comment

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

wpChatIcon