Linux: How to zip and unzip

To zip a directory recursively

Linux: How to zip and unzip
Photo by Tomas Sobek on Unsplash

Linux: How to zip files and directories from the command line.

One of the most common tasks as a Linux admin is to compress files and directories, when you want to share the compressed files with someone who uses windows is better to use a compression tool like zip which ensures compatibility between Linux and Windows.

Example: zip files and directories

to zip files is very straightforward, you need to provide a name for the zip archive and the files you want to zip.zip archive_name.zip file1 file2 file3

To compress files based on a file pattern you can use something like this, this example will compress all *.txt files in the current directory.zip archive_name.zip *.txt

To include directories in the archive you need to use the -r switch which recursively zips files of a directory, note that we are not limited to only one directory and we also can include individual files to the archive.zip -r archive_name.zip ./dir1 ./dir2 file1 file2

Example: compression level and compression method

zip has 9 compression levels, 0 is the lowest one which actually doesn't do any compression and 9 is the maximum compression level; the default compression level is 6 which provides a good level of compression vs compression speed. We can change the compression level with the following syntaxzip -9 [OTHER_OPTIONS] [ARCHIVE_NAME] [FILES_TO_COMPRESS]

  • In this case we want the maximum compression level (9)

Also, we can define the compression method using -Z, using different compression methods isn’t much necessary for day to day work but it can be helpful in cases that we need a better compression method than the deflate method which is the default one.zip -Z bzip2 [OTHER_OPTIONS] [ARCHIVE_NAME] [FILES_TO_COMPRESS]

  • In this example we use the bzip2 method

Example: Password protected archives

If you need password protected archives you can use the -e which, it will ask you to provide a password, be careful don't loose the password, otherwise you will not be able to unzip the files.zip -e [OTHER_OPTIONS] [ARCHIVE_NAME] [FILES_TO_COMPRESS]

Example: splitting archives

There are cases that you will need to split the archives, the most common reason to do this is because of an upload limit to an application or the mail server rejects attachments larger than some megabytes, to split archives use the -s switch and the specified split sizezip -s 5m [OTHER_OPTIONS] [ARCHIVE_NAME] [FILES_TO_COMPRESS]

  • In this example we create spitted archives of 5 mb each

Example: unzip files

To unzip an archive is very easy. use unzip and provide the archive location, this will unzip to the current directory.unzip archive_name.zip

to unzip to a different directory use the -d switch$ unzip archive_name.zip -d /a/directory/

Example: How to suppress output from zip and unzip

To suppress output from zip and unzip use the -q parameter, is very useful especially if you zip or unzip a large number of fileszip -q [OTHER_OPTIONS] [ARCHIVE_NAME] [FILES_TO_COMPRESS]

I hope you found this article useful :)

Join Medium with my referral link - Konstantinos Patronas
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…