How to compress and extract files in Linux
You can compress and decompress files using the terminal or a GUI. In this tutorial I will show you several programs to do this.
Table of Contents
tar
With tar
you can compress and decompress files with the “tar”, “tar.gz” and “tar.xz” formats. The difference between “tar” and others is “tar” is not a compress format, but an “archive” (like a folder). Depending on the version of tar
, you can add the parameters without a hyphen (tar cf
instead of tar -cf
).
- Create a tar archive file
tar -cf <filename>.tar <files>
tar -cf test.tar image.jpg tar -cf folder.tar folder/
- Extract a tar file
tar -xf <filename>.tar
- Extract only one file from a tar archive.
tar -xf <filename>.tar file
- Create a tar.gz file
tar -czf <filename>.tar.gz <files>
- Extract a tar.gz file
tar -xzf <filename>.tar.gz
- Create a tar.xz file
tar -cJf <filename>.tar.xz <files>
- Extract a tar.xz file
tar -xJf <filename>.tar.xz
- List contents inside a tar file
tar -tf <filename>.tar
More parameters:
--one-top-level
: when extracting a file, create a folder for extracted files (with the name of the file).-p
: preserve files permissions (use it when creating and extracting a tar)
zip
Compress files with the “zip” format.
zip <filename>.zip <files>
# or
zip <filename> <files>
# Compress more
zip -9 <filename>.zip <files>
More parameters:
-r
: recursive, go into directories.
unzip
Decompress “zip” files.
unzip <filename>.zip
Options for unzip
:
-l
: list files in compressed file.-d <folder>
: extract files in the specified folder (it will create the folder if it doesn’t exist).
File Roller (GUI)
Default Archive Manager for GNOME (package name is file-roller
).
Ark (GUI)
Default KDE app for archiving.
Test with this online terminal:
Featured content: