Zip directory in linux command

How do I zip/unzip on the unix command line?

@joachim, you should ask the question in a question and provide an answer in an answer. There’s no problem with answering your own questions but not as part of the question itself, since that breaks down the whole community-votes-on-the-best-answer advantage of SO. In addition, this is probably better suited for superuser since it’s not programming related.

@rlb.usa: I work in a lot of different places on a lot of different servers. Using SA for a note like this helps other people too, as I see it.

9 Answers 9

Typically one uses tar to create an uncompressed archive and either gzip or bzip2 to compress that archive. The corresponding gunzip and bunzip2 commands can be used to uncompress said archive, or you can just use flags on the tar command to perform the uncompression.

If you are referring specifically to the Zip file format, you can simply use the zip and unzip commands.

zip squash.zip file1 file2 file3 

this unzips it in your current working directory.

Just as a side note: zip can create many different flavors of .ZIP archives, as well as use many different compressors. On the other hand, there are a number of other compression tools named similarly: 7-Zip, gzip, bzip2, rzip, etc. which are not interchangeable.

I’m confused, I did this and get a long list of inflations and extractions but when I check the directory it is empty, save for the original zip file? Where is it all going?

For the record, use unzip -d myfolder squash.zip to extract into a separate new directory ( myfolder ) instead of putting all the files directly into the current directory, which can be annoying if the zip file contains many files and directories in its top level. This mimics how most UI unzip tools work.

There are a truly vast number of different ways to compress and uncompress under UNIX derivatives so I’m going to assume you meant «zip» in the generic sense rather than a specific file format.

You can zip files up (in compressed format) with the GNU tar program:

which will do the current directory. Replace . with other file names if you want something else.

That’s assuming of course that you have a tar capable of doing the compression as well as combining of files into one.

If not, you can just use tar cvf followed by gzip (again, if available) for compression and gunzip followed by tar xvf .

For specific handling of ZIP format files, I would recommend downloading 7zip and using that — it recognises a huge variety of file formats, including the ZIP one.

Читайте также:  Linux mc выделить несколько файлов

Or use tar jcvf file.tar.bz2. to compress in bzip2 format or tar Jcvf file.tar.xz . for xz compression.

@Starfish, that’s certainly within your rights, thanks for at least explaining why, so many people don’t take the time to do that 🙂 I took «zip» in the more generic sense (compression) rather than a specific format, so I’ll make that more clear.

Be aware that tar archives may not work correctly when moving between different platforms, mac to linux or vice versa. Zip is much safer, as it works correctly on all systems.

I suppose technically this is gzip, so that’s fine. I guess. Pretty obviously not what the question asked for though.

If you don’t have zip and unzip packages installed and you have java, you can use jar to unzip:

Well, when it comes to distributing files for a variety of operating systems, I’d recommend 7-zip.

Usually in the package p7zip , you’ll get the 7z and 7za command, with which you can create your own 7z archives.

7za can also decompress standard (pkzip) zip archives (and create them as well with the -tzip switch).

7za a archive.7z file1 file2 directory/ 

It can also create self-extracting archives with the -sfx switch:

7za a -sfx archive.exe files1 file2 dir 

I recommend this method in case Windows users can’t open 7z archives (in case you want to advice a tool for that: PeaZip).

If you want to use the same compression algorithm with your tarballs, use the -J switch with tar :

tar cJf archive.tar.xz file1 file2 dir 

xz is a UNIX tool, that uses LZMA2 for compression, but works the way gz , bz2 , etc works. It even works as a filter.

7z doesn’t create archives with full filesystem information on UNIX, so you’d need to use tar before using 7z (but since 7z stores other information about the tar file, I’d recommend using xz , as it is designed for it):

tar cf - file1 file2 dir | 7za z -si archive.tar.7z 

Источник

How to Zip and Unzip a directory and its files in Linux [duplicate]

I am a newbie in Linux.Whats is the complete process to zip and unzip a directory and its files.Please mention if any installation has to be done.

3 Answers 3

You say you are a newbie. Maybe you got Windows-experiences. If you do not have the packages installed yet, you need to

sudo apt-get install zip gzip tar 

first (or by a graphical pkg-manager).

Then, for an entry it would be the easiest way to use zip/unzip:

zip -r my_arch.zip my_folder 

Zip stores relative path names by default. There are several parameter-options available for zip. For that read: the manual (man zip). For a starting this will do.

Most often you will see .tar.gz endings in linux-world. That’s the product of two tools: TAR (the tape archiver) and GZIP (the GNU-Zip). Tar has got the call option to automatically gzip/gunzip files after «taring».

tar -cvzf may_arch.tar.gz my_folder 
  • -c means «create»
  • -v means «verbose» (sometimes bothersome and slowing down. )
  • -z means «use (GNU)zip»
  • -f XYZ declares the name of the output file. (You should chose a helping name like XYZ.tar.gz)
Читайте также:  Настройка wifi через терминал linux

There may also be .tar.bz2 endings. This is the product of the -j parameter instead of the -z parameter: you will choose compression with BZIP2 (-> man bzip2).

To extract you simply use -x (eXtract) instead of -c (Create):

You can use the zip and unzip command line utilities. These can be installed by running

sudo apt-get install zip unzip 

I know several ways, but since you’re new on linux. So I’ll tell you how to zip a file using GUI method (the easiest way).

enter image description here

  1. Create a new folder and fill it with anything you want, for example many of file (In my case, I’ll fill it with theme folder) :

enter image description here

  1. Right click the folder you want to zip and select «Compress. » option : enter image description here
  2. You can choose which file format you want by clicking combobox next to «Filename» textbox. Also you can set the folder you want to zip location. enter image description here

enter image description here

Clicking «Other options» will lead you to password section, in other word you can set password for your preferred file so someone (include you) have to enter password before unzip the file.

Источник

How to Zip Files and Directories in Linux

Zip is the most widely used archive file format that supports lossless data compression.

A Zip file is a data container containing one or more compressed files or directories. Compressed (zipped) files take up less disk space and can be transferred from one to another machine more quickly than uncompressed files. Zip files can be easily extracted in Windows, macOS, and Linux using the utilities available for all operating systems.

This tutorial will show you how to Zip (compress) files and directories in Linux using the zip command.

zip Command #

zip is a command-line utility that helps you create Zip archives.

The zip command takes the following syntax form:

zip OPTIONS ARCHIVE_NAME FILES 

To create a Zip archive in a specific directory, the user needs to have write permissions on that directory.

Zip files do not support Linux-style ownership information. The extracted files are owned by the user that runs the command. To preserve the file ownership and permissions, use the tar command.

The zip utility is not installed by default in most Linux distributions, but you can easily install it using your distribution package manager.

Install zip on Ubuntu and Debian #

Install zip on CentOS and Fedora #

How to ZIP Files and Directories #

To zip one or more files, specify the files you want to add to the archive separated by space, as shown below:

zip archivename.zip filename1 filename2 filename3
adding: filename1 (deflated 63%) adding: filename2 (stored 0%) adding: filename3 (deflated 38%) 

By default, the zip command prints the names of the files added to the archive and the compression method. We’ll explain the compression methods and levels later in this guide.

If the archive name doesn’t end with .zip , the extension is added automatically unless the archive name contains a dot. zip archivename.zip filename will create an archive with the same name as would zip archivename filename .

Читайте также:  Linux mint добавить сетевую папку

To suppress the output of the zip command, use the -q option:

zip -q archivename.zip filename1 filename2 filename3

Often, you’ll create a zip archive of a directory including the content of subdirectories. The -r option allows you to traverse the whole directory structure recursively:

zip -r archivename.zip directory_name

You can also add multiple files and directories in the same archive:

zip -r archivename.zip directory_name1 directory_name2 file1 file1

Compression Methods and Levels #

The default compression method of Zip is deflate. If the zip utility determines that a file cannot be compressed, it simply stores the file in the archive without compressing it using the store method. In most Linux distributions, the zip utility also supports the bzip2 compression method.

To specify a compression method, use the -Z option.

zip -r -Z bzip2 archivename.zip directory_name
. adding: sub_dir/ (stored 0%) adding: sub_dir/file1 (bzipped 52%) adding: sub_dir/file2 (bzipped 79%) 

The zip command allows you to specify a compression level using a number prefixed with a dash from 0 to 9. The default compression level is -6 . When using -0 , all files will be stored without compression. -9 will force the zip command to use an optimal compression for all files.

For example, to use the compression level -9 , you would type something like this:

zip -9 -r archivename.zip directory_name

The higher the compression level, the more CPU-intensive the zip process is, and it will take more time to complete.

Creating a Password Protected ZIP file #

If you have sensitive information that needs to be stored in the archive, you can encrypt it using the -e option:

zip -e archivename.zip directory_name

The command will be prompted to enter and verify the archive password:

Enter password: Verify password: 

Creating Split Zip File #

Imagine you want to store the Zip archive on a file hosting service that has a file size upload limit of 1GB, and your Zip archive is 5GB.

You can create a new split Zip file using the -s option followed by a specified size. The multiplier can be k (kilobytes), m (megabytes), g (gigabytes), or t (terabytes).

zip -s 1g -r archivename.zip directory_name

The command above will keep creating new archives in a set after it reaches the specified size limit.

archivename.zip archivename.z01 archivename.z02 archivename.z03 archivename.z04 

zip command Examples #

Create a Zip archive named archivename.zip containing all the files in the current directory.

Same as above, including the hidden files (files starting with a dot):

Create a Zip archive named archivename.zip containing all MP3 files in the current directory without compressing the files.

Conclusion #

In Linux, you can create Zip archives with the zip command.

To extract a ZIP archive on a Linux system, you can use the unzip command .

If you want to learn more about the zip command, visit the Zip Man page.

If you have any questions or feedback, feel free to leave a comment.

Источник

Оцените статью
Adblock
detector