Linux / Unix

Linux: Compress or uncompress files

How do I compress a linux or unix folder?

It’s quite simple to compress whole directory in linux/unix, most possible and useful actions would be:

  • Compress folder (archive)
  • Listing files from archive
  • updated compress

Compress a folder

Run following command to compress a folder.

tar -zcvf archive-file-name.tar.gz your-directory-name

Where -zcvf

  • -z : Compress archive using gzip program in Linux or Unix
  • -c : Create archive on Linux
  • -v : Verbose i.e display progress while creating archive
  • -f : Archive File name

Let’s suppose you need to archive a folder, you will run following command

tar -zcvf images-01jan2019.tar.gz /home/USER/images

Above command will compressed images in current folder with given name. Now if you need to extract archive run following command.

Uncompress archive

tar -zxvf images-01jan2019.tar.gz

Where -x: Extract files from the archive

But if you want to extract archive in specific folder run following command.

tar -zxvf images-01jan2019.tar.gz -C /static/restore

Compress multiple folders in same archive

tar -zcvf compressed-folders.tar.gz /path/to/folder1/ /path/to/folder2/

About the author

Danyal Ali Butt

I'm working as programmer since 2002. Born, grew up, studied and worked in Pakistan now Im located in Denmark since 2006.

View all posts