How to zip any file in linux

Содержание
  1. How to zip a folder in Ubuntu Linux / Debian Linux
  2. zip a folder in Ubuntu Linux using the cli
  3. How do I use zip command to compress a folder?
  4. Compress a directory in Ubuntu Linux
  5. zip a folder in Ubuntu Linux using the GUI method
  6. Password protecting zip file
  7. Conclusion
  8. How to Create and Extract Zip Files to Specific Directory in Linux
  9. Create Zip Archive File in Linux
  10. Extract Zip File to Specific or Different Directory
  11. If You Appreciate What We Do Here On TecMint, You Should Consider:
  12. How to Zip and UnZip Files in Linux
  13. Zip file (Compress)
  14. Unzip file (Extract)
  15. More Zip and Unzip Command Options
  16. 1) Compressing the current directory
  17. 2) Compressing all the file in current directory
  18. 3) Extracting all the compressed file in the current directory
  19. 4) Forcefully overwrite existing files when decompressing
  20. 5) Compressing file to another directory
  21. 6) Decompressing archive to another directory
  22. 7) Best compression
  23. 8) Compressing multiple files
  24. 9) See the content of a zip file without decompressing
  25. 10) Extracting one or more file from a compressed archive
  26. 11) Exclude certain file during the decompression
  27. 12) Decompressing an archive without creating directories
  28. 13) List detailed information about the archive
  29. 14) Update one file or more files of the compressed archive
  30. 15) Update all the files of the compressed archive
  31. 16) Do compression and extraction quietly
  32. 17) Converting text files when decompressing
  33. 18) Compressing only sub-directory of a main directory into individual archive
  34. 19) Compressing both files and directory into individual archive
  35. 20) Show detailed information about a compressed archive
  36. Conclusion

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

Читайте также:  Проверяет ли windows активацию

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

Источник

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.

Источник

How to Zip and UnZip Files in Linux

Compression utilities compress files (borg and rar) and are similar in their functions and operations. Zip/unzip is one of the commands used in Linux to create compressed files and to extract. In modern Linux distros these utilities are installed by default. If any issues, you can install both these utilities from the shell prompt but you must be a root user to do so.

On RedHat/CentOS based system

On Ubuntu/Debian based system

  • Zip is a compression and file packaging utility for Linux and Unix.
  • Unzip will list, test, or extract files from ZIP archive files.
Читайте также:  Unetbootin для iso windows

The zip and unzip programs work exactly in the same way as PKZIP and PKUNZIP in the DOS environment. Compressed files use less disk space than normal files and they transfer faster than larger uncompressed files. In Linux, you can compress files with the help of Gzip or Zip. If you exchange files with non-Linux users then you can use zip to avoid compatibility problems.

Zip file (Compress)

The compression process replaces the original file with a new file that has an additional extension which is .zip (the case of zip command), so don’t delete the .zip files that you create. They are the original files in a compressed wrapper! The syntax is in the form

  • option: there are available options of zip command
  • -b path: where to save temporally the zip file until the compression finish and then copy it in the current working directory
  • filezip: it is the name of zip file which will be created
  • file: it is the name of the original file. It can be more than one file

The command given below creates a file file.zip which contains a copy of the file named file10 located in the current directory.

To compress a folder, we used -r option of zip command. The command given below will compress the directory script-test into the file name back.zip:

See how it operates on the file’s directory too. You can see the result

You can add the .zip extension at the end of the zip file yourself.

The command will directly understand without adding the same extension again.

You can see the result.

Unzip file (Extract)

Unzip command extract the .zip file only. So we will use it to extract the zip files created. To extract a file do as below

It extracts the content in the current directory. So, if there are the originals files, it will ask if you want to replace it

When the zip file contains more than one file, it will do the operation with all the files. Let’s try to extract the zip folder

More Zip and Unzip Command Options

1) Compressing the current directory

The command given below will compress the current directory and also all sub-directories:

2) Compressing all the file in current directory

The command written below will create a file called backfile.zip which contains all files in the current directory. File names that start with a «.» are not included.

3) Extracting all the compressed file in the current directory

You can extract all the zip file as below

4) Forcefully overwrite existing files when decompressing

During the decompression, if there are originals files, unzip command will ask you if you want to replace or to rename the newly extracted files. The operation can be not easy if you have some files in the compressed archive.

So you can force the unzip command to overwrite the existing files with unzip -o command

5) Compressing file to another directory

It is possible to zip a file and save the zip file in another directory by indicating the new destination path with the name of the zip file at the end.

6) Decompressing archive to another directory

As with the compression operation, it is possible to extract files into another directory. It is with -d option of unzip command

7) Best compression

You can also try -9 option for best compression as shown below:

8) Compressing multiple files

It is possible to compress multiples files into one zip file. And we compress it to another directory

9) See the content of a zip file without decompressing

It is possible to see the content of a zip file with the unzip -l command. It is like when you list a detail the content of a folder without move on it.

10) Extracting one or more file from a compressed archive

If a compressed archive has more than one file, it is possible to extract only one or more files. This operation will not extract all the file of the compressed archive. You need to specify each filename to extract

Using the command above means that you already know exactly the name of the files to extract. So, it means for example that you have listed the content of the compressed archive with unzip -l command

Читайте также:  Laserjet p2055dn driver windows 10

11) Exclude certain file during the decompression

As we are able to filter file to extract, it is possible to exclude certain files during the decompression

You can check if a compressed archive has not error. It is possible with unzip -t command

12) Decompressing an archive without creating directories

If a compressed archive contains a folder, during the extraction operation it will recreate the exact structure. It is possible to extract the archive without creating the folder which were already presents. But their contents will be extracted directly

We will consider the zip archive of the example above. It contains folder1 and script-test folders, we will extract without creating the two

You can see the folders are not present

13) List detailed information about the archive

It is possible to have the detail information about the content of an archive like with ls -l command.

It will give some information such as the archive name, its total size, and the total number of files in it. The trailer gives the number of files listed, the total uncompressed and compressed data size of the listed files (not counting internal zipfile headers), and the compression ratio.

14) Update one file or more files of the compressed archive

Suppose we have compressed an archive then, modified a file. There is a possibility to add the update file to the compressed archive with the zip -u command.

In the example below, we have updated a file toto and add a new file papi

You can the date on the output of each command. You can see that the update date is may instead of april

15) Update all the files of the compressed archive

You can replace (freshen) an existing entry in the zip archive only if it has been modified more recently than the version already in the zip archive. Unlike the update option, this will not add files that are not already in the zip archive.

16) Do compression and extraction quietly

When we do compression of extraction, normally zip command show the output of the operation. It is possible to decide not to show the result to the output with -q option of zip command but we will need to check the result after a certain time

17) Converting text files when decompressing

Some non-linux systems store text files with two end-of-line characters CR and LF rather than the more efficient single character (LF) used on all Unix systems. You can have issue compatibility. You can use unzip -a to face it with a mnemonic for ASCII conversion. It converts files that are supposedly text, while leaving alone those that are marked binary.

18) Compressing only sub-directory of a main directory into individual archive

Suppose that we have a folder with some files and sub-directories. Now suppose that we only need to compressed theses sub-directory. I will give you a script line to do it. Make sure you enter to the main directory first.

We will first check if we are into the correct directory

Now let’s see the sub-directories we have

You can see that we have pap and pop sub-folder. Let’s try our script

Let’s check their zip files

You can see that it creates the zip folder with the respectively name. Note that our command only compress each level 1 sub-directory of the main directory not every descendant directory of the sub-directory.

If you want to see how this works, include an echo before the zip and you will see the commands printed instead of executed.

19) Compressing both files and directory into individual archive

You can compress both the files and directories into individual archive for some reason. To do it, you can use the script line below

20) Show detailed information about a compressed archive

You can have more detail information about a compressed archive with -v option of unzip command

Conclusion

In this tutorial, we have understood the compression(zip) and decompression(unzip) concept and we have seen how we can manipulate compressed files. Let us know if you know about more options.

Источник

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