Unix 是否可以从find命令输出中删除根目录?
我想移动一些文件,并认为“查找”将是选择正确文件的一个好选项。因此,我查找文件:Unix 是否可以从find命令输出中删除根目录?,unix,find,Unix,Find,我想移动一些文件,并认为“查找”将是选择正确文件的一个好选项。因此,我查找文件: find somedir-iname“somefile” somedir/subdir1/subdir2/somefile somedir/subdir2/somefile somedir/subdir3/somefile somedir/subdir4/somefile somedir/subdir5/somefile 这对我下一步的计划没有多大帮助。我需要的是: find somedir-iname“some
find somedir-iname“somefile”
somedir/subdir1/subdir2/somefile
somedir/subdir2/somefile
somedir/subdir3/somefile
somedir/subdir4/somefile
somedir/subdir5/somefile
这对我下一步的计划没有多大帮助。我需要的是:
find somedir-iname“somefile”-魔法选项
subdir1/subdir2/somefile
子磁盘2/somefile
子目录3/somefile
子文件4/somefile
subdr5/somefile
-magic选项是什么
显然,一个简单的打印输出不是我想要的。最后一个命令还将有一个'-exec'。比如:
find somedir -iname somefile | sed 's@^[^/]*/@@'
find somedir -iname somefile -printf %P\\0
find somedir-iname“somefile”-magic option-exec some_命令'somedir/{}''someotherdir/{}'';'
我很惊讶我找不到任何东西,因为从结果中删除根目录似乎是一个非常明显的特性
如果问题的答案是“否”,那么就可以了。我有一个粗略的计划B,使用了pushd
和for
循环。但是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 somedir -iname somefile | sed 's@^[^/]*/@@'
find somedir -iname somefile -printf %P\\0
你是说忽略目录结构的根级别吗?是的,这很有效。当然,我不能那样使用-exec
,但是|xargs
也可以。