查找:显示包含特定子目录的目录

查找:显示包含特定子目录的目录

我有一个目录列表

/u2/tip/coy/inter/fcs/
/u2/tip/coy/inter/fcs/devel
/u2/tip/coy/inter/fcs/ecom_flink
/u2/tip/coy/inter/fcs/totalstable
/u2/tip/coy/inter/fcs/develbi
/u2/tip/coy/inter/fcs/tgn
/u2/tip/coy/inter/fcs/tdhmdcuat
/u2/tip/coy/inter/fcs/ecom_tdhmdc
/u2/tip/coy/inter/fcs/ecom_tdh
/u2/tip/coy/inter/fcs/grow
/u2/tip/coy/inter/fcs/sgsb
/u2/tip/coy/inter/fcs/tdhmdc
/u2/tip/coy/inter/fcs/ecom_grow
/u2/tip/coy/inter/fcs/masupport
/u2/tip/coy/inter/fcs/totalslow
/u2/tip/coy/inter/fcs/ecom_sgsb
/u2/tip/coy/inter/fcs/tdh

但其中只有一部分包含 */out 目录:

/u2/tip/coy/inter/fcs/devel/out
/u2/tip/coy/inter/fcs/ecom_flink/out
/u2/tip/coy/inter/fcs/ecom_grow/out
/u2/tip/coy/inter/fcs/ecom_sgsb/out
/u2/tip/coy/inter/fcs/ecom_tdhmdc/out
/u2/tip/coy/inter/fcs/ecom_tdh/out
/u2/tip/coy/inter/fcs/tdhmdc/out
/u2/tip/coy/inter/fcs/tdh/out

我想知道是否有一种方法可以使用命令(例如find),给定/u2/tip/coy/inter/fcs,将返回此列表:

devel
ecom_flink
ecom_grow
ecom_sgsb
ecom_tdhmdc
ecom_tdh
tdhmdc
tdh

无需借助grepgrep类似的过滤器/工具(sedawk和朋友)

答案1

find /u2/tip/coy/inter/fcs -type d -name "out"

返回文件夹的子集,

/u2/tip/coy/inter/fcs/devel/out
/u2/tip/coy/inter/fcs/ecom_flink/out
/u2/tip/coy/inter/fcs/ecom_grow/out
…etc 

然后您可以在这些结果上运行 dirname:

find /u2/tip/coy/inter/fcs -type d -name "out" -exec dirname {} \;

/u2/tip/coy/inter/fcs/devel
/u2/tip/coy/inter/fcs/ecom_flink
/u2/tip/coy/inter/fcs/ecom_grow
…etc

通过使用 sh -c,您可以在该结果上执行 basename:

find /u2/tip/coy/inter/fcs -type d -name "out" -exec sh -c 'basename "$(dirname "$0")"' {} \;

感谢slhck提供上述内容。

问题是:运行一个包含一个进程的 shell,该进程对包含另一个进程的子 shell 的输出进行处理,这比将 find 的输出通过管道传输到 sed/awk 更简单、更快或更好吗?

答案2

find /u2/tip/coy/inter/fcs -name 'out' -type d |
while read line ; do
    IFS='/'
    set -- $line
    num=$(( $# - 1))
    eval  "echo \"\${$num}\""
done

但当文件名中嵌入了换行符时,这将彻底失败

相关内容