Zipping files in linux command line

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

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 use zip on Linux

Compressed files with the .zip extension are commonplace throughout Windows systems, as it’s been the native file compression method for the operating system since many years ago. On a Linux system, the nearest equivalent would have to be tar files and various methods of compression like gzip .

There are a few reasons why you may need to create or open .zip files on Linux. When sharing files with a Windows user, it’d definitely be better to send them a .zip file than a file with Linux based compression, and you may come across an occasional zip archive online or from a friend that you need to open.

In this guide, we’ll show you how to use zip on Linux to open or create compressed .zip archives on both the command line and GUI.

Читайте также:  Alpine linux default login

In this tutorial you will learn:

How to use zip on Linux

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software zip, unzip
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to use zip via command line

Using zip on the command line will involve two different utilities, zip for compressing or “zipping” files and unzip for decompressing or “unzipping” files. Your Linux distro may already come with these packages installed, but not all do. You can run the respective commands on your system to install and/or update the two programs:

$ sudo apt install zip unzip
$ sudo dnf install zip unzip

Once the packages have been installed, you can use the following command syntax to compress some files into a zip archive:

$ zip archive-name.zip file1.txt file2.txt file3.txt

Using zip on command line

DID YOU KNOW?
Zip files will not preserve file permissions like tar files will. This is one reason why you should stick with tar files on Linux unless you’re looking for cross compatibility.

You can also add directories to a zip archive by using the -r (recursive) option. You can specify as many directories as you’d like. Feel free to also include files in the same command.

$ zip -r directory1 directory2 file1.txt file2.txt

The zip tool has more options available that you can see by simply typing:

How to use unzip

The unzip command is very easy to use; all you need to do is specify the name of the zip archive that you wish to decompress.

Unzipping an archive on command line

If you want to extract the files and folders to a particular directory, you can specify the directory path with the -d option:

$ unzip archive-name.zip -d /path/to/directory

Unzipping an archive into a specified directory

Just like zip, the unzip tool has a lot more options you may find useful. Type either of these commands for a full rundown:

How to use zip via GUI

Most or every desktop environment should include a way to open and create zip files. In the examples below, we’re using GNOME on Ubuntu, but the instructions should be nearly identical across any desktop environment or Linux distro that you happen to be using.

To create a zip archive: highlight the files you want to compress, right click one of them, and click on “compress.”

Right click the files and click compress

Make sure you select the zip option in the following menu and choose a name for your archive.

Name the compressed archive and choose zip option

How to unzip via GUI

To unzip, you can simply right click the zip archive and choose “extract here.”

Right click a zip file and choose extract to decompress it

You also have options in the same menu to extract to a different location if you choose. Additionally, you can open the zip archive to browse the contents and extract individual files.

Читайте также:  Unreal commander для linux

Conclusion

In this guide, we saw how to compress and decompress zip archives via command and GUI on Linux. Zip files are the most used compression format, so it’s very useful to know how to create and open them. Tar files are better for Linux since they are more flexible and preserve file permissions, but chances are that there will be situations where you need to use zip files as well.

Comments and Discussions

Источник

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)

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.

Источник

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