I have a bunch of files and directories on my Linux machine that I want to zip into a single archive. How can I do this? I’d also like to know if I can specify the name of the resulting zip file or if it will use the name of one of the files or directories I’m zipping up.
You can use the zip command to zip multiple files and directories in Linux. This command allows you to create a zip archive of one or more files and directories, and you can specify the name of the resulting zip file.
Most Linux distributions include the zip
utility by default. So, likely, it is already installed on your system unless you are using a minimalistic or custom Linux setup. You can check if the zip
is installed by running the which zip
command, which will output the path to the zip
executable if it is installed. If the command does not produce any output, zip
is not installed, and you will need to install it before you can use it.
which zip
Install zip
on Ubuntu and Debian
sudo apt install zip
Install zip
on CentOS and Fedora
sudo yum install zip
To zip one or more files into an archive, specify the names of the files you want to include, separated by a space.
zip myarchive.zip file1.txt file2.txt file3.txt
Archive directory and all of its subdirectories.
You can use the -r option to create a zip archive of a directory and its subdirectories.
zip -r myarchive.zip directory_name
You can also add multiple files and directories in the same archive.
zip -r myarchive.zip directory1 directory2 directory3 file1.txt file2.txt file3.txt
Available Options
The zip command has several options that you can use to customize the behaviour of the command. Here is a list of some of the most commonly used options:
-r
: This option tells zip
to include the contents of directories and the directories themselves and to traverse the directory structure recursively. This is useful when you create a zip
archive of an entire directory tree, including its subdirectories and contents.
-9
: This option specifies the maximum compression level (the highest possible). This can result in smaller zip archives, but it may also take longer to create the archive.
-j
: This option tells zip
to store the files in the zip archive without including the path information. This is useful when you want to create a flat archive that does not preserve the directory structure of the original files.
-T
: This option tells zip
to test the integrity of the zip archive after it has been created. This can be useful to ensure that the zip archive was created correctly and that the files are not corrupted.
-m
: This option tells zip
to delete the original files after they have been added to the zip archive. This can be useful when you want to create a zip archive and free up disk space at the same time.
-u
: This option tells zip
to update an existing zip archive. If the specified file is not in the zip archive, it will be added. If the file is already in the zip archive, it will be updated with the new version.
-d
: This option tells zip
to delete the specified files from the zip archive. You can use this option to remove files from an existing zip archive.
-x
: This option tells zip
to exclude the specified files from the zip archive. You can use this option to create a zip archive that does not include certain files.
-v
: This option tells zip
to display verbose output, including the names of the files as they are added to the zip archive. This can be useful when you want to see the progress of the zip command or debug any issues that may arise.