bioffe
bioffe

Reputation: 6383

How to use find utility with logical operators and post processing

Is there way to find all directories that have executable file that matches a partial name of parent directory?

Situation

/distribution/software_a_v1.0.0/software_a 
/distribution/software_a_v1.0.1/software_a
/distribution/software_a_v1.0.2/config.cfg 

I need result

/distribution/software_a_v1.0.0/software_a 
/distribution/software_a_v1.0.1/software_a

I've gotten only so far

find /distribution -maxdepth 1 -type d  #and at depth 2 -type f -perm /u=x and binary name matches directory name, minus version  

Upvotes: 0

Views: 88

Answers (3)

lind
lind

Reputation: 2269

Another way using 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 }'

The awk part is based on your sample and stated condition ... name matches directory name, minus version. Modify it if needed.

Upvotes: 1

twalberg
twalberg

Reputation: 62389

I don't know if this is the most efficient, but here's one way you could do it, using just 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

Upvotes: 1

PlasmaPower
PlasmaPower

Reputation: 1878

I would use grep:

find /distribution -maxdepth 1 -type d | grep "/distribution/software_\w_v\d*?\.\d*?\.\d*?/software_\w"

Upvotes: 1

Related Questions