Como NÃO incluir diretórios pai ao usar o comando zip?

1

Eu preciso:

  1. Percorra um diretório e encontre todas as sub-subpastas criadas para > X dias atrás
  2. Compacte o conteúdo dessas pastas dentro da sub-subpasta e remova a conteúdo anterior

Exemplo de estrutura de arquivo desde o começo:

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)

Exemplo de estrutura de arquivo após o término do comando:

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

MAS! Eu não quero incluir toda a estrutura de pastas (sub-dir / sub-sub-dir) no arquivo zip. Eu quero apenas que o arquivo zip tenha esta aparência:

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)

Em vez de:

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

O comando que usei até agora resolve tudo menos a parte da estrutura de pastas ... Parece algo assim (eu não tenho o comando exato na minha frente agora. Provavelmente atualizarei amanhã.

find * -mindepth X -maxdepth X -mtime +10 -exec zip -r -m \{}/zipfilename {} \;
    
por John Doe 17.05.2017 / 22:51

1 resposta

0

Acho que você quer usar -execdir em vez de -exec .

Na página de manual:

   -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.

Por exemplo:

$ 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

Claro, se você não quiser qualquer caminho no seu arquivo zip, você pode apenas passar -j (ou --junk-paths ) para armazenar apenas os arquivos.

    
por 17.05.2017 / 23:29