是否可以从 find 命令输出中删除根目录?
Is it possible remove the root directory from find command output?
我想四处移动一些文件,并认为 find 是 select 正确文件的一个很好的选择。所以我寻找文件:
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
会是什么?
显然,简单的打印输出不是我想要的。最后的命令也将有一个“-exec”。类似于:
find somedir -iname "somefile" -magic-option -exec some_command 'somedir/{}' 'someotherdir/{}' ';'
我很惊讶我找不到任何东西,因为从结果中删除根目录似乎是一个非常明显的功能。
如果问题的答案是'NO'那没关系。我有一个使用 pushd
和 for
循环的粗略计划 B。但是 find
会更优雅。
看来您需要 mindepth 标志。
find somedir -mindepth 2 -iname "somefile"
这将忽略您所在的目录并递归地从一级向下搜索。
它是非标准的,但是使用 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
我想四处移动一些文件,并认为 find 是 select 正确文件的一个很好的选择。所以我寻找文件:
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
会是什么?
显然,简单的打印输出不是我想要的。最后的命令也将有一个“-exec”。类似于:
find somedir -iname "somefile" -magic-option -exec some_command 'somedir/{}' 'someotherdir/{}' ';'
我很惊讶我找不到任何东西,因为从结果中删除根目录似乎是一个非常明显的功能。
如果问题的答案是'NO'那没关系。我有一个使用 pushd
和 for
循环的粗略计划 B。但是 find
会更优雅。
看来您需要 mindepth 标志。
find somedir -mindepth 2 -iname "somefile"
这将忽略您所在的目录并递归地从一级向下搜索。
它是非标准的,但是使用 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