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
| Name | Summary |
|---|---|
| Unpack *.tar | tar -xvf ./file.tar |
| Unpack *.tar.gz | tar xvzf ./file.tar.gz |
| Unpack *.rar | unrar e ./file.rar |
| Unpack *.zip | unzip ./file.zip |
| Unpack *.gz | gunzip ./file.gz; gzip -d file.gz |
| Unpack *.bz2 | tar -jxvf file.tar.bz2 -C /tmp/extract_here/ |
| Unpack *.7z | 7z e ./file.7z install p7zip first |
| Unpack *.xz | tar -xf ./file.tar.xz |
| Unpack *.jar | jar -xvf ./file.jar |
| Unpack *.war | jar -xvf ./file.war |
| Unpack *.tgz | tar -xf ./file.tgz |
Compress
| Name | Command |
|---|---|
| Pack to *.tar | tar -cvf ./file.tar <dir_to_compress> |
| Pack to *.gz | tar -zcvf ./newfile.tar.gz <dir_to_compress> |
| Pack to *.tgz | tar -cvzf backup.tgz * |
| Pack to *.bz2 | tar -cvf ./file.tar.bz2 <dir_to_compress> |
| Pack to *.zip | zip -r ./file.zip <dir_to_compress> |
Advanced Usage
| Name | Command |
|---|---|
| Pack with exclude | tar --exclude "*.git*" -cvf file.tar <dir_to_compress> |
| Remove a single file from tarball | tar --delete -f file.tar file1.txt |
| Add file to tarball. -r indicates append | tar -rf file.tar file1.txt |
Comments