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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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):
zip -r myfiles.zip mydir
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.
Method 2
In my case I wanted to zip each file into its own archive, so I did the following (in zsh):
$ for file in *; do zip ${file%.*}.zip $file; done
Method 3
Another way would be to use find and xargs: (this might include a “.” directory in the zip, but it should still extract correctly. With my test, zip stripped the dot before compression)
find . -type f -exec zip zipfile.zip {} +
(The + can be replaced with ; if your version of find does not support the + end for exec. It will be slower though…)
This will by default include all sub-directories. On GNU find -maxdepth can prevent that.
Method 4
Another (slow) method to do this (which adds one file to the zip at a time):
for f in * .[^.]*; do
[ -r "$f" ] || continue # Skip directories or non-existant files (Probably ".[^.]*" if directory has no dotfiles). Using -e will allow directories to be used as well
zip zipfile.zip "$f" # If directories are included, you probably want to add -r
done
This has the dotfile issues of * (workaround added) and would be start zip once for each file, adding it to the archive. In bash, it would deal with a large amount of files.
It would be slower than most of the other methods, but is relatively simple.
Method 5
Not .zip files, but gzip * is a brief command that will compress each file in a dir into its own .gz and delete the original. Really handy in many cases, as many other tools can work with the .gz files directly.
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8df8fee8ffcdeee2e0fdf8f9e8ff">[email protected]</a>:~/test$ touch test1.txt <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="95e0e6f0e7d5f6faf8e5e0e1f0e7">[email protected]</a>:~/test$ touch test2.txt <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="681d1b0d1a280b0705181d1c0d1a">[email protected]</a>:~/test$ touch test3.txt <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2d585e485f6d4e42405d5859485f">[email protected]</a>:~/test$ ls test1.txt test2.txt test3.txt <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="eb9e988e99ab8884869b9e9f8e99">[email protected]</a>:~/test$ gzip * <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="186d6b7d6a587b7775686d6c7d6a">[email protected]</a>:~/test$ ls test1.txt.gz test2.txt.gz test3.txt.gz
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0