How to compress/uncompress files on Linux

Following are some of the common commands on how to compress and uncompress files on Linux.


*.tar.gz

Compress:

tar -czvf name-of-archive.tar.gz /path/to/directory-or-file
  • -c: Create an archive.
  • -z: Compress the archive with gzip.
  • -v: “verbose” mode. The v is always optional in these commands, but it’s helpful.
  • -f: Allows you to specify the filename of the archive.

Uncompress:

tar -xzvf archive.tar.gz -C directory-name
  • -x: Uncompress the archive with gzip.
  • -C: This is optional, it defines a specific directory in which the contents will be extracted.

*.zip

Compress

zip -r ./file.zip <dir_to_compress>

Uncompress

unzip ./file.zip


CheatSheet of the  Common Compress Format

Decompress

NameSummary
Unpack *.tartar -xvf ./file.tar
Unpack *.tar.gztar xvzf ./file.tar.gz
Unpack *.rarunrar e ./file.rar
Unpack *.zipunzip ./file.zip
Unpack *.gzgunzip ./file.gz; gzip -d file.gz
Unpack *.bz2tar -jxvf file.tar.bz2 -C /tmp/extract_here/
Unpack *.7z7z e ./file.7z install p7zip first
Unpack *.xztar -xf ./file.tar.xz
Unpack *.jarjar -xvf ./file.jar
Unpack *.warjar -xvf ./file.war
Unpack *.tgztar -xf ./file.tgz

Compress

NameCommand
Pack to *.tartar -cvf ./file.tar <dir_to_compress>
Pack to *.gztar -zcvf ./newfile.tar.gz <dir_to_compress>
Pack to *.tgztar -cvzf backup.tgz *
Pack to *.bz2tar -cvf ./file.tar.bz2 <dir_to_compress>
Pack to *.zipzip -r ./file.zip <dir_to_compress>

Advanced Usage

NameCommand
Pack with excludetar --exclude "*.git*" -cvf file.tar <dir_to_compress>
Remove a single file from tarballtar --delete -f file.tar file1.txt
Add file to tarball. -r indicates appendtar -rf file.tar file1.txt

Comments

Leave a Reply

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