I think you want to use -execdir
instead of -exec
.
From the 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.
For example:
$ 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
Of course, if you don't want any paths in your zip file, you can just pass -j
(or --junk-paths
) to store the just the files.