Command to create zip files in linux

How to Create and Extract Zip Files to Specific Directory in Linux

In one of our several articles about the tar command, we showed you how to extract tar files to a specific or different directory in Linux. This short guide explains to you how to extract/unzip .zip archive files to a specific or different directory in Linux.

Zip is a simple, cross-platform file packaging and compression utility for Unix-like systems including Linux and Windows OS; plus many other operating systems. The “zip” format is a common archiving file format used on Windows PC’s and most importantly, it enables you to specify the compression level between 1 and 9 as an option.

Create Zip Archive File in Linux

To create a .zip (packaged and compressed) file from the command line, you can run a similar command like the one below, The -r flag enables recursive reading of files directory structure.

Create Zip File in Linux

To unzip the tecmint_files.zip archive file you have just created above, you can run the unzip command as follows.

The above command will extract the files into the current working directory. What if you want to send the unzipped files into a specific or different directory – you can learn this in the next section.

Extract Zip File to Specific or Different Directory

To extract/unzip .zip archive files to specific or different directory from the command line, include the -d unzip command flag as shown below. We will use the same example above to demonstrate this.

This will extract the .zip file content into the /tmp directory:

Extract Zip Files to Specific Directory

For more usage information, read zip and unzip command man pages.

You may also like to read the following related articles.

In this short article, we have explained how to extract/unzip .zip archive files to a specific or different directory in Linux. You can add your thoughts to this article via the feedback form below.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

[ZIP File/Zip Folder Linux] Zip Command in Linux with Examples

Are you looking for ZIP commands in Linux that can help you to zip files Linux and zip folder Linux? Then read our blog post to have a detailed understanding of it.

List of content you will read in this article:

The most popular lossless data compression archive file format is Zip. A Zip file is a data object that encapsulates one or more compressed files and folders. Uncompressed files take up more disc space and take longer to transmit from one device to another. Zip files may be readily extracted on Microsoft Windows, macOS, and Linux operating systems using the appropriate software for each platform. In this guide, you’ll learn how to use the zip command in Linux with examples [ZIP File Linux/Zip Folder Linux] to compress files and directories.

Zip command in Linux with examples

Zip is a command-line tool that aids in the creation of Zip archives.

The syntax for the zip command is as follows:

zip Option Archive-name File

A user must have write permission on a directory in order to build a Zip archive in that directory.

Читайте также:  Scx 4100 ������ �������

Zip files do not allow ownership information in the Linux way. The person who executes the command owns the extracted files. Use the tar command to keep file ownership and permissions intact. Most Linux distributions do not include the zip program by default, but you may simply install it using your distribution’s package management.

Zip may be installed on Ubuntu and Debian systems by the following command:

sudo apt install zip

Zip may be installed on CentOS and Fedora systems by following this command: CentOS and Fedora

sudo yum install zip

How to Compress Files and Directories in Linux [zip file and folder in linux]

To zip file/folder in Linux, you have to divide the files to be included in the pack by a space, as demonstrated below:

zip xyz.zip file1 file2 file3

Output:

adding: file1 (deflated 53%)

adding: file2 (stored 0%)

adding: file3 (deflated 48%)

The zip command publishes the names of the files that have been added to the archive as well as the compression technique by default. Unless the file which has to be compressed contains a .zip extension, the extension is created automatically if the archive name does not conclude with it.

zip xyz.zip file1 will produce the same archive as zip xyz file1.

1. Use the -q option to suppress the output of the zip command:

zip -q xyz.zip file1 file2 file3

Creating a zip archive of a directory that includes the content of subdirectories is common.

2. You may recursively scan the full directory tree using the -r option:

zip -r xyz.zip dir1

3. In the same zip file linux, you can also add numerous files and directories:

zip -r xyz.zip dir1 dir2 filename1 filename2

Compression Methods and Levels [Zip Compression]

Deflate is Zip’s default compression algorithm. If a file cannot be compressed, the zip utility just puts it in the file as it is, instead of compressing it with the store method. The bzip2 compression algorithm is supported by the zip program in most Linux variants.

Use the -Z option to select a compression method.

zip -r -Z bzip2 xyz.zip dir1

Output:

adding: sub_dir/ (stored 0%)

adding: sub_dir/filename1 (bzipped 27%)

adding: sub_dir/filename2 (bzipped 68%)

The zip command lets you define a compression level from 0 to 9 by providing a number preceded with a dash. The compression level is set to -6 by default. When -0 is used, all files are saved without compression. -9 forces the zip command to perform the best compression possible for all files.

For example, if you wanted to utilize compression level -9, you would type:

zip -9 -r xyz.zip dir1

The zip operation becomes more CPU-intensive when the compression level is increased, and it takes longer to complete.

How to Create a Password-Protected ZIP file?

If you have sensitive data that has to be saved in the archive, use the -e option to encrypt it:

zip -e xyz.zip dir1

The archive password must be entered and verified using the following command:

The password you will be entering will not be seen on the screen. However, the next time you log in, make sure you remember your password.

How to Create a Split Zip File?

Assume you wish to upload the Zip archive to a file hosting provider with a 1GB upload restriction, and your Zip archive is 5GB in size.

To create a new split Zip file, use the -s option followed by size. k (kilobytes), m (megabytes), g (gigabytes), or t (terabytes) can be used as the multiplier.

zip -s 500m -r xyz.zip dir

After a set hits the stated size limit, the operation above will keep producing new archives in it.

Bonus Zip Commands in Linux

1. Create a xyz.zip file that contains all of the files in the current directory.

2. You may use the following command to include all the hidden files:

3. Make a xyz.zip file that contains all MP4 files in the current directory, compressed.

Conclusion

The zip command in Linux may be used to build Zip archives [Zip files and Folders in Linux]. On a Linux operating system, the unzip command may also be used to decompress a ZIP file. We have seen how to zip various files and directories in this tutorial and various available zip command options in Linux.

Источник

ZIP command in Linux with examples

ZIP is a compression and file packaging utility for Unix. Each file is stored in single .zip <.zip-filename>file with the extension .zip.

  • zip is used to compress the files to reduce file size and also used as file package utility. zip is available in many operating systems like unix, linux, windows etc.
  • If you have a limited bandwidth between two servers and want to transfer the files faster, then zip the files and transfer.
  • The zip program puts one or more compressed files into a single zip archive, along with information about the files (name, path, date, time of last modification, protection, and check information to verify file integrity). An entire directory structure can be packed into a zip archive with a single command.
  • Compression ratios of 2:1 to 3:1 are common for text files. zip has one compression method (deflation) and can also store files without compression. zip automatically chooses the better of the two for each file to be compressed.
    The program is useful for packaging a set of files for distribution; for archiving files; and for saving disk space by temporarily compressing unused files or directories.
Читайте также:  Office 365 персональный для windows 10

Syntax for Creating a zip file:

Extracting files from zip file

Unzip will list, test, or extract files from a ZIP archive, commonly found on Unix systems. The default behavior (with no options) is to extract into the current directory (and sub-directories below it) all files from the specified ZIP archive.

Options :
1. -d Option: Removes the file from the zip archive. After creating a zip file, you can remove a file from the archive using the -d option.
Suppose we have following files in my current directory are listed below:
hello1.c
hello2.c
hello3.c
hello4.c
hello5.c
hello6.c
hello7.c
hello8.c

Syntax :

After removing hello7.c from myfile.zip file, the files can be restored with unzip command

2.-u Option: Updates the file in the zip archive. This option can be used to update the specified list of files or add new files to the existing zip file. Update an existing entry in the zip archive only if it has been modified more recently than the version already in the zip archive.
Syntax:

Suppose we have following files in my current directory are listed below:
hello1.c
hello2.c
hello3.c
hello4.c

After updating hello5.c from myfile.zip file, the files can be restored with unzip command

3. -m Option: Deletes the original files after zipping. Move the specified files into the zip archive actually, this deletes the target directories/files after making the specified zip archive. If a directory becomes empty after removal of the files, the directory is also removed. No deletions are done until zip has created the archive without error. This is useful for conserving disk space, but is potentially dangerous removing all input files.
Syntax :

Suppose we have following files in my current directory are listed below:
hello1.c
hello2.c
hello3.c
hello4.c

After this command has been executed by the terminal here is the result:

4.-r Option: To zip a directory recursively, use the -r option with the zip command and it will recursively zips the files in a directory. This option helps you to zip all the files present in the specified directory.

Syntax:

Suppose we have following files in my current directory (docs) are listed below:
unix.pdf
oracle.pdf
linux.pdf

5. -x Option: Exclude the files in creating the zip. Let say you are zipping all the files in the current directory and want to exclude some unwanted files. You can exclude these unwanted files using the -x option.

Syntax :

Suppose we have following files in my current directory are listed below:
hello1.c
hello2.c
hello3.c
hello4.c

This command on execution will compress all the files except hello3.c

6.-v Option: Verbose mode or print diagnostic version info. Normally, when applied to real operations, this option enables the display of a progress indicator during compression and requests verbose diagnostic info about zip file structure oddities.
When -v is the only command line argument, and either stdin or stdout is not redirected to a file, a diagnostic screen is printed. In addition to the help screen header with program name, version, and release date, some pointers to the Info-ZIP home and distribution sites are given. Then, it shows information about the target environment (compiler type and version, OS version, compilation date and the enabled optional features used to create the zip executable.

Syntax :

Suppose we have following files in my current directory are listed below:
hello1.c
hello2.c
hello3.c
hello4.c

Читайте также:  От чего лагает windows 10


This article is contributed by Mohak Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Источник

How to zip a folder in Ubuntu Linux / Debian Linux

zip a folder in Ubuntu Linux using the cli

First install the zip command using apt command or apt-get command. Open the terminal and type the following command:
$ sudo apt install zip unzip

How do I use zip command to compress a folder?

The syntax is
zip -r filename.zip folder
zip -r filename.zip folder1 folder2
zip -r filename.zip /path/to/folder1 /path/to/file2
To create compressed archive named data.zip of data folder in the current directory, run:
zip -r data.zip data/

Verify file with the ls command:
ls -l data.zip
You can encrypt data.zip with a password by passing the -e option:
zip -r -e data.zip data/
Sample outputs:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

zip command has many more options as follows:

Option Description
-f freshen: only changed files
-u update: only changed or new files
-d delete entries in zipfile
-m move into zipfile (delete OS files)
-r recurse into directories
-j junk (don’t record) directory names
-0 store only
-l convert LF to CR LF (-ll CR LF to LF)
-1 compress faster
-9 compress better
-q quiet operation
-v verbose operation/print version info
-c add one-line comments
-z add zipfile comment
-@ read names from stdin
-o make zipfile as old as latest entry
-x exclude the following names
-i include only the following names
-F fix zipfile (-FF try harder)
-D do not add directory entries
-A adjust self-extracting exe
-J junk zipfile prefix (unzipsfx)
-T test zipfile integrity
-X eXclude eXtra file attributes
-y store symbolic links as the link instead of the referenced file
-e encrypt
-n don’t compress these suffixes
-h2 show more help

Compress a directory in Ubuntu Linux

The zip command syntax is as follows to compress a directory in Ubuntu Linux:
zip -r compressed_data.zip /path/to/foldername
zip -r compressed_data.zip /home/vivek/Jan-2018

zip a folder in Ubuntu Linux using the GUI method

To access and organize your files you use “Files” app (file manager)”. Use the Files file manager to browse and organize the files on your computer. Open it. Select folder name such as data and right click the “Compress…“:

Gif 01: Compressing a folder in Ubuntu Linux using GUI method i.e. Files file manager

Password protecting zip file

We can encrypt and password protect our zip file as follows:
zip -r -e output.zip /path/to/folder/
We can also use and state password on the CLI to encrypt zipfile entries:
zip -r -e -P ‘YOUR_PASSWORD_HERE’ output.zip /path/to/folder/

WARNING: The -P ‘YOUR_PASSWORD_HERE’ option to the zip IS INSECURE! Avoid it. Many multi-user operating systems such as Linux provide ways for any user to see the current command line of any other user; even on stand-alone systems there is always the threat of over-the-shoulder peeking. Storing the plaintext password as part of a command line in an automated script is even worse. Whenever possible, use the non-echoing, interactive prompt to enter passwords. And where security is truly important, use strong encryption such as Pretty Good Privacy (PGP/GPG) instead of the relatively weak standard encryption provided by zipfile utilities. See “ HowTo Encrypt And Decrypt Files With A Password Using GPG on Linux and Unix-like Systems” for more info.

Conclusion

You just learned how to compress a directory in Ubuntu Linux or Debian Linux using the zip command. The same command used to zip a folder in Linux. For more info please see this page here.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

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