DEV Community

Cover image for Linux commands to compress and decompress files and directories
Abdullah Al Nahid
Abdullah Al Nahid

Posted on • Edited on

Linux commands to compress and decompress files and directories

Being an avid linux os user, I often find myself googling how to compress a single file or a directory using bash terminal. The purpose of this article is to compile all those handy commands that I have to copy-paste from internet.

Comment if you want more compression related commands that you frequently use

 

1A. Compress to tar.gz

###### For a file
tar -cvzf <output-file> <input-file>

###### For a folder
tar -cvzf <output-file> <input-folder>
Enter fullscreen mode Exit fullscreen mode

For example:

###### For a file
tar -cvzf file.tar.gz myfile.csv

###### For a folder
tar -cvzf documents.tar.gz Documents
Enter fullscreen mode Exit fullscreen mode

 

1B. Decompress tar.gz

tar -xvzf <tar-gz-file>
Enter fullscreen mode Exit fullscreen mode

For example:

tar -xvzf work.tar.gz
Enter fullscreen mode Exit fullscreen mode

 

2A. Compress to .tar

###### For a file
tar -cvf <output-file> <input-file>

###### For a folder
tar -cvf <output-file> <input-folder>
Enter fullscreen mode Exit fullscreen mode

For example:

###### For a file
tar -cvf file.tar myfile.csv

###### For a folder
tar -cvf documents.tar Documents
Enter fullscreen mode Exit fullscreen mode

 

2B. Decompress .tar

tar -xvf <tar-file>
Enter fullscreen mode Exit fullscreen mode

For example:

tar -xvf work.tar
Enter fullscreen mode Exit fullscreen mode

 

3A. Compress to .gz

gzip <filename>

###### To keep the original input file:
gzip -k <filename>
Enter fullscreen mode Exit fullscreen mode

For example:

gzip documents.txt

###### To keep the original input file
gzip -k documents.txt
Enter fullscreen mode Exit fullscreen mode

 

3B. Decompress .gz

gunzip <gz-file>

###### To keep the original compressed file:
gunzip -k <gz-file>
Enter fullscreen mode Exit fullscreen mode

For example:

gunzip filename.gz

###### To keep the original compressed file
gunzip -k filename.gz
Enter fullscreen mode Exit fullscreen mode

 

4A. Compress to .zip

###### For a single file
zip <output-zip-file> <input-filename>

###### For multiple files
zip <output-zip-file> <file-1> <file-2> ... <file-n>

###### For multiple files with same extension (.csv, .pdf) 
zip <output-zip-file> *<extension>

###### For a folder
zip -r <output-zip-file> <input-folder-name>

###### For multiple folders
zip -r <output-zip-file> <folder-1> <folder-2> ... <folder-n>
Enter fullscreen mode Exit fullscreen mode

For example:

###### For myfile.txt file
zip document.zip myfile.txt

###### For multiple files
zip files.zip report.docx document.pdf data.txt

###### For multiple files with same extension (.csv, .pdf)
zip pdfs.zip *.pdf
zip textfiles.zip *.txt

###### For Documents folder
zip -r temp.zip Documents

###### For Books and Documents folders
zip -r folders.zip Books Documents
Enter fullscreen mode Exit fullscreen mode

 

4B. Decompress .zip

unzip <zip-filename>
Enter fullscreen mode Exit fullscreen mode

For example:

unzip document.zip
Enter fullscreen mode Exit fullscreen mode

In case of error unzip command not found, run this:

sudo apt-get install unzip
Enter fullscreen mode Exit fullscreen mode

 

Top comments (0)