如何在使用 -exec 时在 find 命令中输出文件名?

如何在使用 -exec 时在 find 命令中输出文件名?

我使用以下行来查找 PWD 的所有子目录并svnadmin verify在每个目录上运行(我已经知道它们是 Subversion 存储库)

find ./* -maxdepth 0 -exec svnadmin verify {} \;

除了输出如下所示之外,此方法效果很好:

* Verifying repository metadata ...
* Verifying metadata at revision 1 ...
* Verifying metadata at revision 2 ...
* Verifying metadata at revision 4 ...
* Verifying metadata at revision 5 ...
* Verifying metadata at revision 6 ...
* Verifying metadata at revision 9 ...
* Verifying metadata at revision 10 ...
* Verifying metadata at revision 12 ...
* Verifying metadata at revision 14 ...
* Verifying metadata at revision 15 ...
* Verifying metadata at revision 18 ...
* Verifying metadata at revision 20 ...
* Verifying metadata at revision 22 ...
* Verified revision 0.
* Verified revision 1.
* Verified revision 2.
* Verified revision 3.
* Verified revision 4.
* Verified revision 5.
* Verified revision 6.
* Verified revision 7.
* Verified revision 8.
* Verified revision 9.
* Verified revision 10.
* Verified revision 11.
* Verified revision 12.
* Verified revision 13.
* Verified revision 14.
* Verified revision 15.
* Verified revision 16.
* Verified revision 17.
* Verified revision 18.
* Verified revision 19.
* Verified revision 20.
* Verified revision 21.
* Verified revision 22.
* Verified revision 23.
* Verified revision 0.
* Verifying repository metadata ...
* Verifying metadata at revision 4 ...
* Verifying metadata at revision 5 ...
* Verifying metadata at revision 6 ...
* Verifying metadata at revision 7 ...
* Verifying metadata at revision 9 ...
* Verified revision 0.
* Verified revision 1.
* Verified revision 2.
* Verified revision 3.
* Verified revision 4.
* Verified revision 5.
* Verified revision 6.
* Verified revision 7.
* Verified revision 8.
* Verified revision 9.

我真的很想find在执行命令之前打印文件名svnadmin verify,以使日志记录更容易。

我尝试挤一点ls进去但却搞砸了,我该怎么做呢(最好简单一点)?

答案1

只需在前面添加一个-printf选项

find -printf '%p' -exec command \;

答案2

如果你不想递归,find在这种情况下使用是没有意义的。直接在 shell 中执行此操作要简单得多:

for d in */; do echo "$d"; svnadmin verify "$d"; done

for d in */查找所有目录(确保*/仅找到目录而不找到文件);echo "$d"将打印目录的名称;将 svnadmin verify "$d"检查目录。

这可以直接从命令行运行,也可以从脚本内部运行,格式不变。

答案3

find ./* -maxdepth 0 -type d -exec bash -c 'echo "{}"; svnadmin verify "{}"' \;

-type d如果它只是目录,我已经添加了。

答案4

  • 使用. -maxdepth 1代替./*,您只需要文件夹结构中的第一级
  • 使用-type d,你只需要文件夹
  • 使用! -name ".",你不需要.
  • 用于-exec启动 shell
  • 用于sh -c在 shell 中启动命令

您的命令

find . -maxdepth 1 -type d ! -name "." -exec sh -c 'echo "{}"; svnadmin verify "{}"' \;

或更短

  • -prune如果文件是目录则使用,不要深入其中

您的命令

find . -mindepth 1 -prune -exec sh -c 'echo "{}"; svnadmin verify "{}"' \; 

相关内容