Linux zip with directories

Zip all files in directory?

Is there a way to zip all files in a given directory with the zip command? I’ve heard of using *.* , but I want it to work for extensionless files, too.

Have you tried navigating one-level up from your desired directory and doing zip myarch.zip mydir/* ?

*.* means any file with a dot. In cp/m and dos all files had a dot, and it made you type it (could not do * ). Therefore people came to see *.* as all files. Eventually Microsoft added long-filenames that could have zero, or more dots. To find a file that has a dot on windows you have to type *.*.* .

5 Answers 5

You can just use * ; there is no need for *.* . File extensions are not special on Unix. * matches zero or more characters—including a dot. So it matches foo.png , because that’s zero or more characters (seven, to be exact).

Note that * by default doesn’t match files beginning with a dot (neither does *.* ). This is often what you want. If not, in bash, if you shopt -s dotglob it will (but will still exclude . and .. ). Other shells have different ways (or none at all) of including dotfiles.

Alternatively, zip also has a -r (recursive) option to do entire directory trees at once (and not have to worry about the dotfile problem):

where mydir is the directory containing your files. Note that the produced zip will contain the directory structure as well as the files. As peterph points out in his comment, this is usually seen as a good thing: extracting the zip will neatly store all the extracted files in one subdirectory.

You can also tell zip to not store the paths with the -j / —junk-paths option.

The zip command comes with documentation telling you about all of its (many) options; type man zip to see that documentation. This isn’t unique to zip; you can get documentation for most commands this way.

Источник

How to Zip Files and Directories from Linux Command Line

Zipped files are compressed formats of the actual content, which makes the files or directories take less data space on the device hosting them. Zipping makes files easily transferable from one device to the other.

Читайте также:  1с сервер linux mssql

In Linux system, zipping one or more files might seem tricky with the GUI of the operating system, but with just a line of commands through the Linux terminal, zipping your desired files or folders can be faster and more straightforward.

In this tutorial, we learn how to zip files and directories in Linux using the zip command.

Zip Command Linux

zip command is used to package a set of files in the system. It will put one or more compressed files into a single zip archive file with the .zip extension. You can easily create an entire directory into zip archives using a single command.

Using the zip command can make file upload, emailing, or in fact, downloading files faster and more compact regardless of the number of files and their size.

Zip files work by encoding into fewer bites by removing redundant data. It supports lossless data compression and the cross-compatibility of devices and operating systems. Zip compression works betters for text files than MP3 or JPG files.

By default, zip uses the deflation compression method. Zip cannot guarantee is compress all files if cannot it uses the store method to keep it as it is. You may use bzip2 compression as option to replace deflate compression method.

You can use unzip command to extract zip files and by default the extracted files are stored in your current directory.

Why we use the zip command

The zip program puts multiple files in a single zip archive to achieve the following:

  • Reduce file size.
  • To transfer files faster between two servers.
  • Compress files into single zip archive files.
  • Saving disk space.
  • Temporarily compressing unused files.

Syntax:

The syntax of the Zip command:

zip OPTIONS ARCHIVE_NAME FILES

The “OPTIONS” refer to the action that the zip command will follow through such as recursive (-r), quiet (-q), removal (-d), etc.

ARCHIVE_NAME or ARCHIVE is the name you want to save the zip as, and FILES or INPATH is the name of the files or directories you are attempting to zip.

Install Zip Package on Linux Distributions

When you initiate a zip command most commonly you may end up seeing the error «zip command not found». This is because zip package is not installed on your Linux distribution.

Let’s first check how to install zip on Linux.

Install Zip on Ubuntu

To install zip utility on Ubuntu and Debian, type:

The command will prompt you to input your password in order to grant root user privilege.

Install Zip on Fedora

On the Fedora distribution of Linux, open your terminal and install zip using the following command:

Install Zip on CentOS and Redhat

On Redhat (RHEL) or CentOS Stream, installing zip will be through the following command:

Читайте также:  Установка boot repair kali linux

Zip Files and Directories

Let’s check how to zip files and directories using the zip command.

To zip a single file named filename1 to a zip file named archive_output_file.zip, type:

zip archive_output_file.zip filename1

zip a single file

To confirm the zip file created, you can use ls command:

list zip file using ls command

You can see that archive_output_file.zip the zipped file now exists in your current directory.

To zip multiple files, use the following command:

zip archive_output_file.zip filename1 filename2 filename3

zip multiple files

In the above output, deflated means the file is compressed using deflate compression method and store means stored to archive but not compressed.

To zip an entire directory structure use -r option. This will enable the zip command to zip the directory recursively i.e zipping the folder along with all the files in that same directory.

To zip a directory structure recursively, run the following command:

zip -r archive_output_file.zip directory1

You may specify multiple directories to get it all compressed in the zip file:

zip -r archive_output_file.zip directory1 directory2 directory3

zip multiple directories

To archive all the files including the hidden files in a specified directory, type:

zip archive_output_file.zip .* *

To suppress zip output which is by default showing file names that are being added to the archive and compression method, use -q option.

Bzip2 Compression Support

Zip allows support for bzip2. When bzip2 is enabled, the default compression deflate is replaced.

Use -Z option to specify the compression method:

zip -r -Z bzip2 archive_output_file.zip directory1

zip to add bzip2 compression method

Level of Compression to Files

Zip command allows to regulate the speed of compression. The levels range from 0 to 9, where level 0 ( fastest compression speed ) stores files without compression and level 9 which is the optimal compression level (slowest compression speed).

Zip automatically chooses level 6 as the default compression level when archiving files.

For example, to zip all the txt files in the current working directory without compressing them by using level 0 compression, type:

zip -0 archive_output_file.zip *.txt

Higher compression levels would give more stress on the CPU and time-consuming.

Zip Command Examples

Let’s look at some zip command examples showing functionalities for manipulating an archive.

Password Protected Zip file

You can protect your files in the archive file using the -e option. This keeps your files encrypted.

You will be prompted to enter a password to protect the archive file.

zip -e archive_output_file.zip file1 file2 file3

protect zip file with a passwod

To set the password for an existing archive file, named archived_file.zip, run the following command:

zipcloak archived_file.zip

Split Zip Files

There are certain conditions where your zip file might be very large and it’s restricting you from transferring the file or emailing the file due to size restrictions, then you can split the file into the specified sizes.

To split archives into multiple files, use -s option. For example, the following command split the compressed file named archive_output_file.zip into 10MB for each new archives.

zip -r -s 10m archive_output_file.zip directory1

You need to use -F option to combine the split zip files into a single zip archive.

zip -F archive_output_file.zip --out combine_archive.zip

You should combine all split files then you are able to unzip it.

Читайте также:  Linux не видит телевизор

An alternative command to split zip archive is zipsplit.

Remove Files From the Zip File

To delete unwanted files from the zip archive file, use -d option.

In the following example, files named file1 and file2 are removed from the myarchive.zip file.

zip -d myarchive.zip file1 file2

remove files named file1 and file2 from the myarchive.zip zip file

Adding New files to an Existing Zip File

Use -u option to add new files to an existing zip archive. For example, let’s add three new files named file1, file2, and file3 to an already existing file named myarchive.zip file.

zip -u myarchive.zip file4 file5

Add more files to existing zip file

Excluding Files, While Creating a Zip File

Use -x option to exclude specific files while creating a zip file.

In the following example, we zip everything in my current directory except file1 and file4.

zip mybackup.zip .* * -x file1 file2

exclude specified file while performing zip operation

Verbose Mode When Creating a Zip File

The verbose mode prints the diagnostic screen. This shows which file got compressed and how much space each file saved and the total % of compression.

zip verbose mode output

Zip Command Options

Some of the useful zip command options are:

Options Description
-r Zip recursively through the directory structure
-u Add new files to the existing zip file
-d Delete entries from a zip file
-x Exclude files or folders while zipping
-m Deletes the original files after making the specified zip archive
-0 Compression level — archives your file without compressing
-9 Highest compresses level
-q Quiet operation whereby output will not be displayed
-V Verbose mode to print diagnostic information
-j This option stores only file names and deletes or junks (don’t record) directory names
-e Encrypt with a password
-Z Set to a specific compression method
-D Do not add entries for directory in zip archive
[email protected] Read the list of input files from the standard input.
-f Updates existing files only
-T Test zip file integrity
-F To fix the offsets
-i To include only the listed filenames
-y Stores symbolic links as such instead of the referenced file
-n Exclude compression to files with given suffixes

Conclusion

In this tutorial, we learned how to zip files and directories in Linux. The zip command is a very handy command for Linux users that deals with files and directories. This enables Linux users to move, store, or share their files easily.

If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks

Источник

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