在 bash 中递归压缩所有文件和文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23340530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:19:47  来源:igfitidea点击:

zip all files and folders recursively in bash

bashcygwinzip

提问by Husman

I am working on a project, where compilation of the project involves, zipping up various files and folders and subfolders (html/css/js) selectively. Working on the windows platform, and I could continue to just use the CTRL+A and then SHIFT-click to unselect, but it does get a little tedious. I am working with cygwin, so I was wondering if it is possible to issue a command to zip selected files/folders recursively whilst excluding others, in one command? I already have zip command installed, but I seem to be zipping up the current zip file too and the .svn file too.

我正在处理一个项目,其中涉及项目的编译,有选择地压缩各种文件和文件夹以及子文件夹(html/css/js)。在 windows 平台上工作,我可以继续只使用 CTRL+A,然后按住 SHIFT 键单击取消选择,但这确实有点乏味。我正在使用 cygwin,所以我想知道是否可以在一个命令中发出一个命令来递归压缩选定的文件/文件夹,同时排除其他文件/文件夹?我已经安装了 zip 命令,但我似乎也在压缩当前的 zip 文件和 .svn 文件。

I would like this to be incorporated into a shell script if possible, so the simpler the better.

如果可能,我希望将其合并到 shell 脚本中,这样越简单越好。

回答by Husman

After reading the man pages, I think the solution that I was looking for is as follws:

阅读手册页后,我认为我正在寻找的解决方案如下:

  • needs to recurse directories (-r),
  • needs to exclude certail files/directories (-x)
  • It works in the current directory, but the . can be replaced with the path of any directory

    zip -x directories_to_exclude -r codebase_latest.zip .

  • 需要递归目录(-r),
  • 需要排除某些文件/目录 (-x)
  • 它适用于当前目录,但 . 可以替换为任意目录的路径

    zip -x 目录_to_exclude -r codebase_latest.zip 。

I have incorporated this into a short shell script that deletes files, tidy up some code, and then zips up all of the files as needed.

我已将其合并到一个简短的 shell 脚本中,该脚本可删除文件、整理一些代码,然后根据需要压缩所有文件。

回答by MLSC

You should read man page of zipcommand:

您应该阅读zip命令的手册页:

-R
       --recurse-patterns
              Travel the directory structure recursively starting at the current directory; for example:

                     zip -R foo "*.c"

              In this case, all the files matching *.c in the tree starting at the current directory are stored into a zip archive named foo.zip.  Note that *.c will match
              file.c, a/file.c and a/b/.c.  More than one pattern can be listed as separate arguments.  Note for PKZIP users: the equivalent command is

                     pkzip -rP foo *.c

              Patterns  are relative file paths as they appear in the archive, or will after zipping, and can have optional wildcards in them.  For example, given the cur‐
              rent directory is foo and under it are directories foo1 and foo2 and in foo1 is the file bar.c,

                     zip -R foo/*

              will zip up foo, foo/foo1, foo/foo1/bar.c, and foo/foo2.

                     zip -R */bar.c

              will zip up foo/foo1/bar.c.  See the note for -r on escaping wildcards.

You can also have a look HERE

你也可以看看这里