- Zip all files in directory?
- 5 Answers 5
- How Do I Zip All Files In A Directory In Linux?
- Install Zip
- For Debian/Ubuntu
- For REHL/CentOS
- The Zip Command
- How to Zip Files In Linux
- How to Compress Zip Directories In Linux
- How to Zip all Files in a Directory In Linux
- How to Zip All Files, Including Hidden Files
- Conclusion
- About the author
- John Otieno
- How to Zip Files and Folders in Ubuntu Command Line
- Using zip command
- Zip a single file
- Zip multiple files
- Zip a folder
- Zip files from multiple files and folders
- Remove files from existing zip file
- Add additional files to an existing zip file
- Encrypt zip file with password
- ⚠️ Move the files into the zip file
- In the end .
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 Do I Zip All Files In A Directory In Linux?
Zip archives refer to container archives that contain one or more compressed files and directories. Zip files are cross-platform, allowing you to create zip archives in Windows, Linux, and macOS using various utilities. In addition, zip archive files take less space, making them easier to transfer and store.
In Linux, we use the zip archive utility to create zip archives. Throughout this tutorial, we will focus on how to go about creating zip archives in Linux using the zip utility.
Install Zip
Depending on your Linux distribution, you will need to install the zip utility. Since all Linux distributions support it, you can use the package manager to install it.
For Debian/Ubuntu
On Debian, use the command:
For REHL/CentOS
On CentOS and REHL family, use the command:
The Zip Command
The zip command is simple to use. The general syntax for the command is:
To create a zip archive of more than one file, pass them in a list (separated by space) after the zip filename. It is also good to ensure you have to write permissions in the directory you are creating the zip file.
How to Zip Files In Linux
We can zip files in a directory as:
The command above displays the name of the file added to the archive and the compression method.
Zip utility automatically adds a .zip extension to the archive filename—if not explicitly specified.
How to Compress Zip Directories In Linux
You can compress directories and the corresponding sub-directories by using the -r flag. The -r flag will tell zip to traverse the entire directory recursively.
For example, consider the /var/log directory. To create an archive of all the files and directories, we use the command:
To suppress the output from the compression process, use the -q for quiet mode. The command creates a zip archive of the specified files with no output.
How to Zip all Files in a Directory In Linux
What if you want to zip all files in a directory? In that case, we use wildcard expressions to do this.
The above command adds all the files and directories in the specified path and adds them to the zip archive.
How to Zip All Files, Including Hidden Files
To add even hidden files to a zip archive, use the wildcard (.* *). The command for that is:
Conclusion
As shown in this tutorial, Linux allows you to create zip archives. You can use any archive utility such as WinRar, 7zip, unzip; to unarchive the files.
About the author
John Otieno
My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list
How to Zip Files and Folders in Ubuntu Command Line
The above command will create a compressed output_file.zip file with file1, file2 and contents of folder1.
I recommend using the recursive option -r as it allows you to add folders and their content. Without this option, you’ll be adding the folder to the zipped file but the directory contents won’t be included.
Let me show the zip command usage in detail with some actual examples.
Using zip command
You should first ensure that you have the zip command installed on your system.
If the above command returns ‘command ‘zip’ not found’ error, you should install it like this:
Now, I don’t intend to confuse you, but having a basic idea of syntax will always be beneficial.
zip [option] [output_file_name] [input1] [input2]
- [option] will be used to accomplish different outcomes regarding zipping files, such as setting up passwords.
- [output_file_name] is the name of your zipped file. You can add .zip extension to the output file but if you don’t, it is added automatically.
- [input1] [input2] can be various directories and files you want to compress into one zip file
Let’s see some examples now.
Zip a single file
I am starting with the simplest example; zipping a single file in the current directory.
zip output_file.zip input_file
[email protected]:~# zip log_compressed auth.log adding: auth.log (deflated 89%)
The zip command output mentions the compression ratio.
Did you notice that it automatically added the .zip extension to the output filename even though I didn’t mention it?
Zip multiple files
You can provide multiple files the same way.
zip output_file.zip input_file1 input_file2 input_file3
It will show the compression levels for all files.
[email protected]:~# zip compressed_log auth.log fontconfig.log alternatives.log adding: auth.log (deflated 89%) adding: fontconfig.log (deflated 86%) adding: alternatives.log (deflated 39%)
You can use the zipinfo command to list the contents of a zipped file without extracting it on the disk.
Zip a folder
Like many other Linux commands, you need to use the recursive option -r while dealing with the directories.
To zip a folder, just add the -r in the command:
zip -r output_folder input_folder
[email protected]:~# zip -r logs.zip logs updating: logs/ (stored 0%) adding: logs/alternatives.log (deflated 39%) adding: logs/auth.log (deflated 89%) adding: logs/fontconfig.log (deflated 86%)
If you miss the -r option, you’ll get the zipped folder but it won’t copy any files.
[email protected]:~# zip logs.zip logs adding: logs/ (stored 0%) [email protected]:~# zipinfo logs.zip Archive: logs.zip Zip file size: 160 bytes, number of entries: 1 drwxr-xr-x 3.0 unx 0 bx stor 22-Aug-22 12:54 logs/ 1 file, 0 bytes uncompressed, 0 bytes compressed: 0.0% [email protected]:~#
Zip files from multiple files and folders
You’ll think that you can combine files from multiple folders and directories into a single zip file like this:
zip -r output_file file1 folder1 folder2/file2
You are not wrong but there could be a potential problem with that approach that you may not like.
I’ll show an example so that you understand it better. I zip a file, a folder and a file from another folder together.
[email protected]:~# zip -r combined new_file logs/ toto/routes.yaml adding: new_file (stored 0%) adding: logs/ (stored 0%) adding: logs/alternatives.log (deflated 39%) adding: logs/auth.log (deflated 89%) adding: logs/fontconfig.log (deflated 86%) adding: toto/routes.yaml (deflated 27%)
If you look at the contents of the output zip file, you’ll see that routes.yml from the toto directory is named toto/routes.yml. Which means
[email protected]:~# zipinfo combined.zip Archive: combined.zip Zip file size: 89379 bytes, number of entries: 6 -rw-r--r-- 3.0 unx 1 tx stor 22-Jul-21 14:03 new_file drwxr-xr-x 3.0 unx 0 bx stor 22-Aug-22 12:54 logs/ -rw-r--r-- 3.0 unx 178 tx defN 22-Aug-22 12:54 logs/alternatives.log -rw-r----- 3.0 unx 806718 tx defN 22-Aug-22 12:54 logs/auth.log -rw-r--r-- 3.0 unx 2791 tx defN 22-Aug-22 12:54 logs/fontconfig.log -rw-r--r-- 3.0 unx 143 tx defN 22-Jun-06 15:14 toto/routes.yaml 6 files, 809831 bytes uncompressed, 88425 bytes compressed: 89.1%
This means that if you extract the combined.zip folder, it will have routes.yaml file in toto subdirectory.
[email protected]:~# tree combined combined ├── logs │ ├── alternatives.log │ ├── auth.log │ └── fontconfig.log ├── new_file └── toto └── routes.yaml
While that may be desirable in a few cases, sometimes you just want the files from different folders combined into a single folder, without their directory name and structure.
And in those cases, you combine the -r with -j . The -j option leaves out the path and directory names.
zip -rj output_file file1 folder1 folder2/file2
This way, you only get the files.
[email protected]:~# zip -rj combined_out new_file logs/ toto/routes.yaml adding: new_file (stored 0%) adding: alternatives.log (deflated 39%) adding: auth.log (deflated 89%) adding: fontconfig.log (deflated 86%) adding: routes.yaml (deflated 27%) [email protected]:~# zipinfo combined_out.zip Archive: combined_out.zip Zip file size: 89201 bytes, number of entries: 5 -rw-r--r-- 3.0 unx 1 tx stor 22-Jul-21 14:03 new_file -rw-r--r-- 3.0 unx 178 tx defN 22-Aug-22 12:54 alternatives.log -rw-r----- 3.0 unx 806718 tx defN 22-Aug-22 12:54 auth.log -rw-r--r-- 3.0 unx 2791 tx defN 22-Aug-22 12:54 fontconfig.log -rw-r--r-- 3.0 unx 143 tx defN 22-Jun-06 15:14 routes.yaml 5 files, 809831 bytes uncompressed, 88425 bytes compressed: 89.1%
Remove files from existing zip file
Added something that should not have been added? You can easily remove unnecessary files from the existing zip file.
zip -d existing_zip File_to_remove_1 File_to_remove_2
Let me show with an example.
Here’s the content of the miscallaneous.zip file:
Now I want to remove files named «HelloWorld.txt» and «Ringtone.mp3»:
zip -d miscellaneous.zip HelloWorld.txt Ringtone.mp3
You can see that those files were no longer present in the zip file.
Add additional files to an existing zip file
I often find myself in situations where I forget to add files while zipping them. No worries. You can add extra files to a zipped file thanks to the update option -u .
zip -u existing_zip File_to_add_1 File_to_add_2
Let’s say I want to add two images to a zip file named «miscellaneous.zip» :
zip -u miscellaneous.zip Burj_Khalifa.jpeg Taj_Mahal.jpeg
You can see that the files have been added to the existing zip file.
Encrypt zip file with password
This is my favorite feature from the entire catalog of zip utility. It may sound complex, but trust me, it’s not!
Add -e option along with -r and it’ll ask for an encryption key phrase before proceeding to zip given files.
zip -r -e output_file input_files input_folders
Here’s an example screenshot. You can see that it asks for the password twice before initiating the zipping process.
When you use the unzip command to extract this zip file, it asks for the passcode.
⚠️ Move the files into the zip file
By default, the zip utility creates a copy of the original file to make a zip file. If you want, you can avoid this by using -m which will move files directly into the zipped file.
For example, I’ll be adding three random files to a zip file named NoCopiesLeft:
zip -m NoCopiesLeft helloworld.txt list.txt Snake_plant.jpg
As you can see, there were three files in the working directory and after making the zip file, no original copies were left.
In the end .
Zip is an excellent tool for reducing directory size. There are many more options and usage of the zip command. You can always refer to the man page for additional information.
Here. I covered the most common examples of the zip command in Ubuntu. I hope you find it helpful.