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>
For example:
###### For a file
tar -cvzf file.tar.gz myfile.csv
###### For a folder
tar -cvzf documents.tar.gz Documents
1B. Decompress tar.gz
tar -xvzf <tar-gz-file>
For example:
tar -xvzf work.tar.gz
2A. Compress to .tar
###### For a file
tar -cvf <output-file> <input-file>
###### For a folder
tar -cvf <output-file> <input-folder>
For example:
###### For a file
tar -cvf file.tar myfile.csv
###### For a folder
tar -cvf documents.tar Documents
2B. Decompress .tar
tar -xvf <tar-file>
For example:
tar -xvf work.tar
3A. Compress to .gz
gzip <filename>
###### To keep the original input file:
gzip -k <filename>
For example:
gzip documents.txt
###### To keep the original input file
gzip -k documents.txt
3B. Decompress .gz
gunzip <gz-file>
###### To keep the original compressed file:
gunzip -k <gz-file>
For example:
gunzip filename.gz
###### To keep the original compressed file
gunzip -k filename.gz
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>
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
4B. Decompress .zip
unzip <zip-filename>
For example:
unzip document.zip
In case of error
unzip command not found
, run this:
sudo apt-get install unzip
Top comments (0)