有没有办法找到所有可执行文件与父目录的部分名称匹配的目录?
情况
/distribution/software_a_v1.0.0/software_a
/distribution/software_a_v1.0.1/software_a
/distribution/software_a_v1.0.2/config.cfg
我需要结果
/distribution/software_a_v1.0.0/software_a
/distribution/software_a_v1.0.1/software_a
我到目前为止
find /distribution -maxdepth 1 -type d #and at depth 2 -type f -perm /u=x and binary name matches directory name, minus version
答案 0 :(得分:1)
我会用grep:
find /distribution -maxdepth 1 -type d | grep "/distribution/software_\w_v\d*?\.\d*?\.\d*?/software_\w"
答案 1 :(得分:1)
我不知道这是否效率最高,但只有bash
只能使用for f in /distribution/*/*
do
if [[ -f "${f}" && -x "${f}" ]] # it's a file and executable
then
b="${f##*/} # get just the filename
[[ "${f}" =~ "/distribution/${b}*/${b}" ]] && echo "${f}"
fi
done
{{1}}
答案 2 :(得分:1)
使用awk的另一种方式:
find /path -type f -perm -u=x -print | awk -F/ '{ rec=$0; sub(/_v[0-9].*$/,"",$(NF-1)); if( $NF == $(NF-1) ) print rec }'
awk部分基于您的样本和陈述条件... name matches directory name, minus version
。如果需要,可以修改它。