What is zip command linux

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.

Читайте также:  Install ifconfig on 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 

Источник

Zip Command in Linux

Zip Command in Linux

ZIP is a file packaging utility compression technique in Unix. Each file is stored in a single file with the extension.zip.This zip command in Linux is usually supported by almost all operating systems like MSDOS, OS/2, Window NT, Minix, and Macintosh, etc. Compress and tar is used with this Zip command and compatible with PKZIP(Phil Katz’s ZIP for MSDOS systems).

Web development, programming languages, Software testing & others

zip [with some options] zipFIle fileDir/fileList

The zip file is a new or existing zip archive and fileDir/fileList is the path occasionally including wildcards. when the same name of zip archive is found then it will update it.

Example: If folder/file_1 and folder/file_2 is contained in folder.zip and the directory folder contains the files folder/file_1 and folder/file_3. So, if before a zip command will execute, folder.zip has:

Читайте также:  Линукс перезагрузка комбинация клавиш

Also, when the directory folder has:

and folder.zip will have now:

folder/file_1
folder/file_2
folder/file_3

Where folder/file_1 is replaced whereas folder/file_3 is new.So, folder.zip now contains folder/file_1, folder/file_2, and folder/file_3, along folder/file_2 from before that are unchanged.

Syntax to Extract Zip Files in Linux

Unzip command extracts list files from the ZIP archive on Unix systems. When no option is used to extract it into the present working directory (and sub-directories which are inside it) every file from the specified ZIP archive.

Zip Command Options in Linux

Now let us focus on the Zip command Options below:

1) -u Option

Updates the file in the zip archive. This also updates an existing entry in the zip archive, and only if it has been modified more now than the version already in the zip archive.

$zip –u file_name.zip file.txt

Suppose we have the files in the current directory are listed below:

file1.txt
file2.txt
file3.txt
file4.txt

$zip –u file_name.zip file5.txt

After updating file5.txt from file_name.zip file, the files will be restored with unzip command.

$unzip file_name.zip $ls command

file1.txt
file2.txt
file3.txt
file4.txt
file5.txt

The file5.txt file is updated to the zip file.

2) -d Option

It deletes the file from the zip archive. This option deletes the created zip file. Suppose we have the files in the current directory are listed below:

$zip –d file_name.zip file.txt
$zip –d file_name.zip file5.txt

After removing file5.txt from file_name.zip file, the files will be restored using unzip command

$unzip file_name.zip $ls command

file1.txt
file2.txt
file3.txt
file4.txt

The file5.txt file is removed from zip file

3) -m Option

Will delete original/main files after zipping. It will move the files by making zip and delete the original files/folder.

If a directory becomes empty after deleting files, the respective directory is also deleted. No deletions are completed until the zip has created archive without any error. So this is useful in maintaining disk space, but ultimately unsafe while removing all input files.

$zip –m file_name.zip file.txt

Suppose we have the files in the current directory are listed below:

After the execution of this command on the terminal here is the result:

file_name.zip
//No other files as .txt(extension) are found

4) -x Option

It will exclude files when we are going to create the zip. Let say you are going to zip all the files in the present directory and want to exclude few files which are not needed. So you can exclude these files which are not needed using the -x option.

$zip –x file_name.zip file_to_be_excluded

Suppose we have the files in the current directory are listed below:

$zip –x file_name.zip file_3.txt

This command while executing will compress every file except file_3.txt

Читайте также:  Linux 2012 all in one

file_name.zip // compressed file
file_3.txt //this file will be excluded while doing compression

5) -r Option

It will recursively zip the files inside it and then folders inside it.

$zip –r file_name.zip directory_name

Suppose we have the files in the current directory (doc) are listed below:

adding: doc/ //Compressing the directory
adding: doc/a.pdf // Compressing first file
adding: doc/b.pdf // Compressing second file
adding: doc/c.pdf //Compressing the third file

6) -v Option

Using the Verbose mode option we will print diagnostic version information. This option will display the progress indicator during compression and request verbose information about the zip structure.

$zip –v file_name.zip file.txt

Suppose we have the files in the current directory are listed below:

adding: file_1.txt (in=0) (out=0) (stored 0%)
adding: file_2.txt (in=0) (out=0) (stored 0%)
adding: file_3.txt (in=0) (out=0) (stored 0%)
adding: file_4.txt (in=0) (out=0) (stored 0%)

total bytes=0, compressed=0 -> 0% savings

Uses of Zip Command in Linux

The uses of Zip Command in Linux are as follows.

  • ZIP is a perfect technique when there is less bandwidth or internet speed and you want to transfer a bunch of files. Then now using this command you can zip and transfer the files in a very efficient manner.
  • The zip program puts one or more compressed files into a single zip archive, along with information like name, path, date, time of last modification, protection, and check information to verify file integrity. An entire directory structure can be compressed into a zip archive by a single command.
  • 2:1 to 3:1 as the compression ratio is prevalent for text files either uses a deflation method or store files without compression. This will choose automatically the best for each file to be compressed.
  • It comes very handy for archiving files; packaging a set of files for distribution and for saving disk space for a short time by compressing unused files or directories.

Conclusion

Now we have understood the depression (unzip) and compression (zip) concept and we have seen how to manipulate compressed files using different options. ZIP is a perfect technique when there is less bandwidth or internet speed and you want to transfer a bunch of files. Then now using this command you can zip and transfer the files in a very efficient manner.

This is a guide to Zip Command in Linux. Here we discuss the syntax to extract zip files along with the different commands of zip in Linux. You may also look at the following articles to learn more –

502+ Hours of HD Videos
54 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5

57+ Hours of HD Videos
15 Courses
9 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5

Источник

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