简体   繁体   中英

“zip -m” Don't Delete Directory if it Becomes Empty

I need to move the contents of a directory into an archive and I was delighted to find that the "-m" option does exactly that. (Of course, I'm using it with the "-T" option. :) )

But unfortunately, if the directory becomes after the zip operation, the directory itself is removed. I don not want this to happen, but can't find any option that behaves like this.

Do you guys have any ideas how I can get this behavior?

Here's my actual (obfuscated) command I'm using in my shell script:

zip -qrTmy $archive_name $files_dir

Unless your directory contains sub directories, it is not hard to recreate it after a zip/move:

zip -qrTmy $archive_name $files_dir
mkdir $files_dir

If the directory contains sub directories, then we need to duplicate that directory structure to a temporary name, perform a zip/move, rename the structure back. I'm still working on how to implement this idea. If I know a solution, I'll update this post.

UPDATE

If the directory contains sub directories:

find $files_dir -type d -exec mkdir temp_{} \; # Duplicate the dir structure
zip -qrTmy $archive_name $files_dir
mv temp_$files_dir $files_dir                  # Restore the dir structure

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM