Wie kann ich KEINE übergeordneten Verzeichnisse einschließen, wenn Sie den zip-Befehl verwenden?

499
John Doe

Ich muss einfach:

  1. Durchsuchen Sie ein Verzeichnis und finden Sie alle Unterordner, die vor> X Tagen erstellt wurden
  2. Komprimieren Sie den Inhalt dieser Ordner im Unterordner und entfernen Sie den vorherigen Inhalt

Beispiel für die Dateistruktur von Anfang an:

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) 

Beispiel für eine Dateistruktur nach Abschluss des Befehls:

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

ABER! Ich möchte nicht die gesamte Ordnerstruktur (Unterverzeichnis / Unterverzeichnis) in die ZIP-Datei aufnehmen. Ich möchte nur, dass die zip-Datei so aussieht:

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) 

Anstatt:

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

Der Befehl, den ich bisher verwendet habe, löst alles außer den Ordnerstrukturteil ... Es sieht ungefähr so ​​aus (ich habe jetzt nicht den genauen Befehl vor mir. Ich werde ihn wahrscheinlich morgen aktualisieren.

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

1 Antwort auf die Frage

0
Possum

Ich denke du willst -execdirstatt verwenden -exec.

Aus der Manpage:

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

Zum Beispiel:

$ 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 

Wenn Sie keine Pfade in Ihrer ZIP-Datei haben möchten, können Sie die Dateien einfach -j(oder --junk-paths) speichern.

Vielen Dank, sehr, sehr, sehr! execdir war genau das, was ich wollte. Jetzt gibt es nur noch ein kleines Problem ... :) Wenn ich die komprimierte Datei etwas dynamischer benennen möchte: sub-dir_sub-sub-dir.zip, ist das irgendwie möglich? Ein Mal noch; vielen dank für die hilfe! Das hat mir definitiv Zeit gespart. John Doe vor 6 Jahren 0
Das fängt an, kompliziert zu werden :) Ich denke, der Schlüssel wäre, ein Skript mit "-execdir" anstelle eines Befehls zu übergeben. Wenn Sie eine neue Shell starten, können Sie `$ PWD` überprüfen und darauf basierend bearbeiten. Beispiel: -execdir bash -c zipname = $ ; zip -r $ zipname ... '' (obwohl IMO sauberer ist, um ein Skript zu erstellen, verwenden Sie `bash -c`). Possum vor 6 Jahren 0