从“查找 - 删除”获取成功状态

从“查找 - 删除”获取成功状态

想知道在使用该命令时如何获取成功代码Find -delete。想知道它删除了哪些文件以及是否有任何错误。我想将此信息保存在日志文件中。

答案1

是的,如中所述man find-delete如果文件被成功删除,则该操作将返回 true,因此可以像任何其他测试一样使用

   -delete
          Delete files; true if removal succeeded.

-print举例来说,您只能删除那些被删除的文件,例如:

$ find . -name 'hostname*'
./otherdir/hostname3
./otherdir/hostname2
./otherdir/hostname1
./hostname7
./hostname5
./hostname6

然后

$ find . -name 'hostname*' -delete -print >deleted 2>not_deleted

结果是

$ head *deleted
==> deleted <==
./hostname7
./hostname5
./hostname6

==> not_deleted <==
find: cannot delete ‘./otherdir/hostname3’: Permission denied
find: cannot delete ‘./otherdir/hostname2’: Permission denied
find: cannot delete ‘./otherdir/hostname1’: Permission denied

相关内容