zip – TecAdmin https://tecadmin.net How to guide for System Administrator's and Developers Tue, 10 Jan 2023 11:01:02 +0000 en-US hourly 1 https://wordpress.org/?v=6.1.1 How to Exclude Files from a Zip Archive https://tecadmin.net/how-to-exclude-files-from-a-zip-archive/ https://tecadmin.net/how-to-exclude-files-from-a-zip-archive/#comments Mon, 19 Jan 2015 13:31:00 +0000 https://tecadmin.net/?p=6532 Do you want to send files but you are facing a problem due to their large size? It has the solution. For this purpose, we have a tool known as zip, which is used to compress all the files and store them in a single folder. Now you can send the compressed zip folder containing [...]

The post How to Exclude Files from a Zip Archive appeared first on TecAdmin.

]]>
Do you want to send files but you are facing a problem due to their large size? It has the solution. For this purpose, we have a tool known as zip, which is used to compress all the files and store them in a single folder. Now you can send the compressed zip folder containing all the files easily to anyone. But there is a folder having 100 files and you have to forward this folder excluding some specific files. Now instead of going to the folder and searching the specific files, we can exclude them by using some commands.

In this article, we will discuss how to make a zip folder by excluding specific files and how it works.

Syntax

Generally, all the Linux distributions have default zip packages installed. The syntax of the command excluding files and directories is as follows:

zip -r [filename].zip [directory_name_to_zip] -x [file_to_exclude]

In the above syntax, the “-r” flag is used to append files, and the “-x” flag is used to exclude files. Now to understand it more clearly let us consider an example.

Examples to Excludes Files in Zip Archive

I have created a sample directory structure including some files. Where a directory named “docs” contains one directory and some files as shown below screenshot:

List Directory Tree in Linux
List Directory Tree in Linux

Let’s consider the above structure, here are a few examples to learn about excluding files from the zip archive.

01. Exclude a directory

Create an archive file excluding the cache directory. Use the following command to create a new archive named “docs.zip” excluding the “cache” directory. So in command, it will be written as:

zip -r docs.zip docs -x "docs/cache/*" 
Zip Command to  Exclude a Directory
Zip Command to Exclude a Directory

02. Exclude a single file

Define the full file path to exclude a single file from the archive file with the zip command. For example, to exclude index.html from the docs directory, type:

zip -r docs.zip docs -x "docs/index.html" 
Zip Command to Exclude Single File
Zip Command to Exclude Single File

03. Exclude files with wildcard

You can also use wild card characters to exclude multiple files from the zip archive. For example, to exclude all files with the “.log” extension, run:

zip -r docs.zip docs -x "*.log" 
Zip Command to Exclude files With Wildcard Names
Zip Command to Exclude files With Wildcard Names

04. Exclude multiple files

You can define -x multiple times in a single zip command to exclude multiple files and directories from the zip archive.

zip -r docs.zip docs -x "/docs/README.md" -x "docs/cache/*" 
Zip Command to Exclude Multiple Files
Zip Command to Exclude Multiple Files

05. Exclude “.git” directory

The source code managed with Git contains a “.git” directory under the root directory. Sometimes you may not be required to archive this directory. Use the following command to exclude .git directory and its contents:

zip -r filename.zip my_dir -x "*.git*" 

06. Exclude “.svn” directory

Applications source code managed through SVN contains the “.svn” directory. Use the following command to exclude the .svn directory from the zip archive.

zip -r filename.zip my_dir -x "*.svn*" 

07. Excluding “node_modules” Directory from Zip Archive

All the Node.js modules are installed under the node_modules directory. While archiving the source code, you can ignore the “node_modules” directory with the below-mentioned command:

zip -r mydir.zip myDir -x "node_modules*" 

Conclusion

With the advancement of technology, it’s not a big issue to resolve anything. Now we can easily make a single zip file of many files and can email it to anyone easily. In this article, we learned how we can make zip files excluding some files that we don’t want to be a part of the zip file

The post How to Exclude Files from a Zip Archive appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-exclude-files-from-a-zip-archive/feed/ 1
3 Most Popular Tools to Archive Files and Directories in Linux https://tecadmin.net/popular-tools-to-archive-files-and-directories-in-linux/ https://tecadmin.net/popular-tools-to-archive-files-and-directories-in-linux/#respond Sat, 22 Nov 2014 09:11:05 +0000 https://tecadmin.net/?p=6326 There are multiple tools available in Linux system for creating archive files. In this article you will find uses of multiple tools for creating or extracting archive files through command line Tool 1 – Zip zip is the most popular command line archiving utility for Linux systems. Create Archive of File # zip output.zip /var/log/*.log [...]

The post 3 Most Popular Tools to Archive Files and Directories in Linux appeared first on TecAdmin.

]]>
There are multiple tools available in Linux system for creating archive files. In this article you will find uses of multiple tools for creating or extracting archive files through command line


Tool 1 – Zip

zip is the most popular command line archiving utility for Linux systems.

Create Archive of File

# zip output.zip /var/log/*.log

Create Archive of Directory

# zip -r output.zip /var/log

Extract Archive

# unzip output.zip

Tool 2 – Tar

Tar is the another most popular command line archiving utility for Linux systems.

Create Archive of File

# tar -cf output.tar /var/log/*.log

Create Archive of Directory

# zip -cf output.tar /var/log

Extract Archive

# tar -xf output.tar

Tool 3 – Gzip

Gzip is one more tool for command line users for making archive files. Gzip also supports to take input of data as standard input or through pipe and create zip file.

Create Archive of File

# gzip -k access.log

Create zip file of piped data

# cat /var/log/messages | gzip > messages.gz

Extract Archive

# gunzip access.log.gz

Combined – Tar + Gzip

Tar also combined gzip program to enable higher compression level of files. Uisng tar with gzip created files extension will be .tar.gz.

Create Archive of File

# tar -czf output.tar.gz /var/log/*.log

Create Archive of Directory

# zip -czf output.tar.gz /var/log

Extract Archive

# tar -xzf output.tar.gz

The post 3 Most Popular Tools to Archive Files and Directories in Linux appeared first on TecAdmin.

]]>
https://tecadmin.net/popular-tools-to-archive-files-and-directories-in-linux/feed/ 0
How to use zip command in Linux https://tecadmin.net/how-to-use-zip-command-line-linux/ https://tecadmin.net/how-to-use-zip-command-line-linux/#comments Thu, 11 Apr 2013 03:38:02 +0000 https://tecadmin.net/?p=848 The zip command in Linux is a utility used for packaging a group of files into a zip archive. The zip command can also be used to compress files, making them take up less space on your computer’s hard drive. In addition, the zip command can be used to encrypt files, making it more difficult [...]

The post How to use zip command in Linux appeared first on TecAdmin.

]]>
The zip command in Linux is a utility used for packaging a group of files into a zip archive. The zip command can also be used to compress files, making them take up less space on your computer’s hard drive. In addition, the zip command can be used to encrypt files, making it more difficult for unauthorized users to access the contents of the zip archive.

To unzip a zip archive, you can use the unzip command. This command will decompress the files in the zip archive, allowing you to access them.

Zip Command Examples

Go through the below real examples of Linux zip command.

  1. Zip all files in a directory
  2. This command will create a zip of all files in the /backup directory. I will not archive files under the sub directories recursively.

    zip backup.zip /backup/* 
    
    Output:
    adding: backup/anaconda.ifcfg.log (deflated 47%) adding: backup/anaconda.log (deflated 78%) adding: backup/anaconda.program.log (deflated 84%) adding: backup/anaconda.storage.log (deflated 90%) adding: backup/boot.log (deflated 72%) adding: backup/dracut.log (deflated 92%) adding: backup/httpd/ (stored 0%) adding: backup/kadmind.log (deflated 74%) adding: backup/krb5kdc.log (deflated 71%) adding: backup/mysqld.log (deflated 82%)

  3. Zip files with wildcard
  4. Use Linux wildcards to archive files of specific extensions only. Like backup only .log extension files in a directory.

    zip backup.zip /backup/*.log 
    
    Output:
    adding: backup/anaconda.ifcfg.log (deflated 47%) adding: backup/anaconda.log (deflated 78%) adding: backup/anaconda.program.log (deflated 84%) adding: backup/anaconda.storage.log (deflated 90%) adding: backup/boot.log (deflated 72%) adding: backup/dracut.log (deflated 92%) adding: backup/kadmind.log (deflated 74%) adding: backup/krb5kdc.log (deflated 71%) adding: backup/mysqld.log (deflated 82%) adding: backup/pm-powersave.log (deflated 15%) adding: backup/wpa_supplicant.log (stored 0%) adding: backup/Xorg.0.log (deflated 83%) adding: backup/Xorg.9.log (deflated 83%) adding: backup/yum.log (deflated 77%)

  5. Zip files recursively
  6. The below command will create an archive recursively with files, directories, and its sub-directories as well. It also maintains the directory structure as source files.

    zip -r backup.zip /backup 
    

  7. Create password protected zip
  8. Sometimes we need to create a password-protected archive. The zip command provides you an option to make password-protected archive files. To create password-protected archive use -e option. This will prompt for a password and confirm the password.

    zip -e backup.zip /backup/*.log 
    
    Output:
    Enter password: Verify password: adding: backup/anaconda.ifcfg.log (deflated 47%) adding: backup/anaconda.log (deflated 78%) adding: backup/anaconda.program.log (deflated 84%) adding: backup/anaconda.storage.log (deflated 90%)

  9. Zip with compression levels
  10. You can also define the compression level with the zip command. There are 10 levels of compression, which range from 0 to 9.

    • -6 is used as default compression level.
    • -0 is used for the lowest level compression.
    • -9 is used for the highest level compression
    zip -9 high-compressed-file.zip /backup/* 
    zip -0 lowest-compressed-file.zip /backup/* 
    

    Check differences between a compressed file

    ls -lh lowest-compressed-file.zip high-compressed-file.zip 
    
    Output:
    -rw-r--r--. 1 root root 50K Apr 11 14:14 high-compressed-file.zip -rw-r--r--. 1 root root 447K Apr 11 14:14 lowest-compressed-file.zip

    You can see the difference between both file sizes.

  11. List content of zip file
  12. Using -l switch with unzip command to list only files inside a zip archive without decompressing it.

    unzip -l backup.zip 
    
    Output:
    Archive: backup.zip Length Date Time Name --------- ---------- ----- ---- 140 04-11-2013 14:07 backup/anaconda.ifcfg.log 11153 04-11-2013 14:07 backup/anaconda.log 15446 04-11-2013 14:07 backup/anaconda.program.log 136167 04-11-2013 14:07 backup/anaconda.storage.log 2722 04-11-2013 14:07 backup/boot.log 211614 04-11-2013 14:07 backup/dracut.log 0 04-11-2013 14:08 backup/httpd/ 1382 04-11-2013 14:07 backup/kadmind.log 1248 04-11-2013 14:07 backup/krb5kdc.log 6485 04-11-2013 14:07 backup/mysqld.log 87 04-11-2013 14:07 backup/pm-powersave.log 0 04-11-2013 14:07 backup/wpa_supplicant.log 30186 04-11-2013 14:07 backup/Xorg.0.log 31094 04-11-2013 14:07 backup/Xorg.9.log 6739 04-11-2013 14:07 backup/yum.log --------- ------- 454463 15 files

  13. Extract a zip file.
  14. The unzip command is used to extract a zip file. Use the below command to simply extract a zip file.

    unzip backup.zip 
    

  15. Check an archive file
  16. Use -t to check and archive files. This option extracts each specified file in memory and compares the CRC (cyclic redundancy check, an enhanced checksum).

    unzip -t backup.zip 
    
     
    
    Output:
    Archive: backup-11Apr2013.zip testing: backup/anaconda.ifcfg.log OK testing: backup/anaconda.log OK testing: backup/anaconda.program.log OK testing: backup/anaconda.storage.log OK testing: backup/boot.log OK testing: backup/dracut.log OK testing: backup/httpd/ OK testing: backup/kadmind.log OK testing: backup/krb5kdc.log OK testing: backup/mysqld.log OK testing: backup/pm-powersave.log OK testing: backup/wpa_supplicant.log OK testing: backup/Xorg.0.log OK testing: backup/Xorg.9.log OK testing: backup/yum.log OK No errors detected in compressed data of backup.zip.

Wrap Up

We’ve shown you how to zip files in Linux using the terminal. This is a handy skill that can come in handy when you need to send someone a compressed file or save storage space on your computer. Also, the Unzipping an archive is easy and we’ll show you how to do it quickly and easily.

The post How to use zip command in Linux appeared first on TecAdmin.

]]>
https://tecadmin.net/how-to-use-zip-command-line-linux/feed/ 2