1

I need to zip all files and subdirectories in a folder including those that start with a . (dot).

I'm in a folder called synthesis and I need to zip all the files and subdirectories under it (but not the synthesis folder itself). The synthesis folder includes a subdirectory called .sopc_builder that has a file in it.

I tried the following command

zip -r synthesis.zip *

I copied the resulting synthesis.zip to another folder and listed the zip contents in a file:

unzip -l synthesis.zip > filelist.txt

The filelist.txt doesn't have the .sopc_builder and its file in the list.

I saw on some post something about a Linux command shopt, but didn't really understand it.

The version of Linux I'm using is Red Hat Enterprise Linux Server 7.9 at my company.

Thanks in advance, Grady

2 Answers 2

1

That should just be:

zip -r file.zip ./

The trailing / is apparently needed to work around a bug whereby if there was a file called - in the current directory, it would be treated as stdin.

To include hidden files in a glob (but not . nor ..), you can use zsh and use the *(D) glob (the D for dotglob glob qualifier), but zip -r file.zip *(D) like zip -r file.zip * would be wrong on several grounds:

  • it would fail if file names started with -. -- would help, but not for a file called -. ./*(D) would be better.
  • zip by default treats its argument as wildcards (it's more of a MS-DOS type of program). So if any of the file names contain wildcard characters, especially [...], that could cause errors. Use -nw to disable that, which you'll want to use anytime you pass arbitrary file names.
$ ls -AR
.:
'*'   ...          bar    fifo|      .foo   -h     '[x]'
 -   'a'$'\n''b'   dir/   file.zip   foo    link@

./dir:
-
$ rm -f file.zip; zip -r file.zip ./
        zip warning: ignoring FIFO (Named Pipe) - use -FI to read: ./link
        zip warning: ignoring FIFO (Named Pipe) - use -FI to read: ./fifo
  adding: bar (stored 0%)
  adding: [x] (stored 0%)
  adding: ... (stored 0%)
  adding: .foo (stored 0%)
  adding: a^Jb (stored 0%)
  adding: * (stored 0%)
  adding: - (stored 0%)
  adding: foo (stored 0%)
  adding: dir/ (stored 0%)
  adding: dir/- (stored 0%)
  adding: -h (stored 0%)
$ rm -f file.zip; zip -nw -r file.zip ./*(D)
        zip warning: ignoring FIFO (Named Pipe) - use -FI to read: ./fifo
        zip warning: ignoring FIFO (Named Pipe) - use -FI to read: ./link
  adding: * (stored 0%)
  adding: - (stored 0%)
  adding: ... (stored 0%)
  adding: a^Jb (stored 0%)
  adding: bar (stored 0%)
  adding: dir/ (stored 0%)
  adding: dir/- (stored 0%)
  adding: .foo (stored 0%)
  adding: foo (stored 0%)
  adding: -h (stored 0%)
  adding: [x] (stored 0%)

See how symlinks and fifos are handled by default. The zip format is more one to use when you want to pack files to be used on other OSes like Microsoft Windows. To create archive of Unix files, you'd rather use tar.

2
  • Great! The manual doesn't mention the trailing slash. Sep 17, 2021 at 10:32
  • 1
    @schrodigerscatcuriosity, not, but that should not be needed. It's just to work around that bug which I just found out. They would rather fix the bug than document the work around. Sep 17, 2021 at 10:38
0

One approach is to combine zip with find:

$ cd the/dir/you/want/to/zip
$ find . -print | zip test -@
adding: file.txt (stored 0%)
adding: .hidden/ (stored 0%)
adding: .hidden/file2.txt (stored 0%)

According to the manual

-@ file lists. If a file list is specified as -@ [Not on MacOS],
zip takes the list of input files from standard input instead
of from the command line. For example,
    zip -@ foo 
will store the files listed one per line on stdin in foo.zip.

Under Unix, this option can be used to powerful effect
in conjunction with the find (1) command.
For example, to archive all the C source files in the current directory
and its subdirectories:
    find . -name "*.[ch]" -print | zip source -@ 
(note that the pattern must be quoted to keep the shell from expanding it). 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.