我想移动一些文件,并认为 find 是选择正确文件的好选择。所以我寻找这些文件:
find somedir -iname "somefile"
somedir/subdir1/subdir2/somefile
somedir/subdir2/somefile
somedir/subdir3/somefile
somedir/subdir4/somefile
somedir/subdir5/somefile
这对我接下来的计划没有太大帮助。我需要的是:
find somedir -iname "somefile" -magic-option
subdir1/subdir2/somefile
subdir2/somefile
subdir3/somefile
subdir4/somefile
subdir5/somefile
什么会-magic-option
be?
显然,简单的打印输出不是我想要的。最终命令还将有一个“-exec”。就像是:
find somedir -iname "somefile" -magic-option -exec some_command 'somedir/{}' 'someotherdir/{}' ';'
我很惊讶我找不到任何东西,因为从结果中删除根目录似乎是一个非常明显的功能。
如果问题的答案是'NO'那么就可以了。我有一个粗略的 B 计划,使用pushd
and for
循环。但find
会更优雅。
它是非标准的,但是使用 gnu find (4.6.0.225-235f),你可以这样做:
find somedir -iname somefile -printf %P\\n
从文档中:
%P File's name with the name of the starting-point under which it was found removed.
如果您想要一个通用的解决方案,那么使用以下内容过滤输出似乎很简单:
find somedir -iname somefile | sed 's@^[^/]*/@@'
如果您的任何文件名包含换行符,这两种解决方案都会严重失败,因此如果您想要一个强大的解决方案,您需要执行以下操作:
find somedir -iname somefile -printf %P\\0
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)