如何在使用 zip 命令时不包括父目录?

如何在使用 zip 命令时不包括父目录?

我需要:

  1. 遍历目录并查找所有超过 X 天前创建的子文件夹
  2. 将这些文件夹的内容压缩到子文件夹中,并删除之前的内容

从一开始的文件结构示例:

root_folder:
    sub-dir (many of these):
        sub-sub-dir (many of these):
            content1 (can be file or folder)
            content2 (can be file or folder)
            content3 (can be file or folder)

命令完成后的文件结构示例:

root_folder:
    sub-dir:
        sub-sub-dir:
            zipfile.zip

但! 我不想包含整个文件夹结构(sub-dir/sub-sub-dir) 在 zip 文件中。我只希望 zip 文件看起来像这样:

zip_file:
    content1 (no matter if it is a file or a folder with content in it)
    content2 (no matter if it is a file or a folder with content in it)
    content3 (no matter if it is a file or a folder with content in it)

代替:

zip_file:
    sub-dir:
        sub-sub-dir:
            content1
            content2
            content3

到目前为止我使用的命令解决了所有问题,除了文件夹结构部分...它看起来像这样(我现在面前没有确切的命令。我可能会在明天更新它。

find * -mindepth X -maxdepth X -mtime +10 -exec zip -r -m \{}/zipfilename {} \;

答案1

我认为您想使用-execdir而不是-exec

来自手册页:

   -execdir command ;

   -execdir command {} +
          Like -exec, but the specified command is run from the  subdirec‐
          tory  containing  the  matched  file,  which is not normally the
          directory in which you started find.  This a  much  more  secure
          method  for invoking commands, as it avoids race conditions dur‐
          ing resolution of the paths to the matched files.  As  with  the
          -exec action, the `+' form of -execdir will build a command line
          to process more than one matched file, but any given  invocation
          of command will only list files that exist in the same subdirec‐
          tory.  If you use this option, you must ensure that  your  $PATH
          environment  variable  does  not  reference  `.';  otherwise, an
          attacker can run any commands they like by leaving an  appropri‐
          ately-named  file in a directory in which you will run -execdir.
          The same applies to having entries in $PATH which are  empty  or
          which  are  not absolute directory names.  If find encounters an
          error, this can sometimes cause an immediate exit, so some pend‐
          ing  commands  may  not  be run at all. The result of the action
          depends on whether the  +  or  the  ;  variant  is  being  used;
          -execdir  command  {} + always returns true, while -execdir com‐
          mand {} ; returns true only if command returns 0.

例如:

$ find sub-dir 
sub-dir
sub-dir/sub-sub-dir
sub-dir/sub-sub-dir/content3
sub-dir/sub-sub-dir/content1
sub-dir/sub-sub-dir/content1/file
sub-dir/sub-sub-dir/content2

$ find sub-dir/ -mindepth 2 -maxdepth 2 -type d -execdir zip -r -m {}/zipped {} \;  

...

$ unzip -l sub-dir/sub-sub-dir/content1/zipped.zip                                
Archive:  sub-dir/sub-sub-dir/content1/zipped.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2017-05-17 17:23   content1/
        4  2017-05-17 17:23   content1/file
---------                     -------
        4                     2 files

当然,如果你不想任何在您的 zip 文件中的路径,您只需传递-j(或--junk-paths)即可存储文件。

相关内容