0

I have a script that uses a find command to find some directory names I need but the output has them in the same string and with an extra level of path I don't need. I want to split them, cut out the part I don't need, and add them each to an array.

find command:

projects=$(find Un -maxdepth 1 -type d -name 'Proj_*')    

find command result (stored as $projects in the script):

Un/Proj_ABCD4 Un/Proj_EF2GH Un/Proj_PG5T3 Un/Proj_MMXCU

What I want to put in the array:

Proj_ABCD4
Proj_EF2GH
Proj_PG5T3
Proj_MMXCU
kevin41
  • 183
  • 2
  • 14
  • 2
    Are you sure you need an array? If you just want to loop over them, `for proj in Un/Proj_*/; do ... something with "${proj#Un/}"; done` – tripleee Oct 06 '21 at 16:24

1 Answers1

1

You don't need find for this case:

projects=()
cd Un && \
for d in Proj_*/; do
    projects+=( "${d%/}" )
done && \
cd ..

Or to exclude symbolic links:

projects=()
cd Un && \
for d in Proj_*; do
    [[ -d $d ]] && ! [[ -L $d ]] && projects+=("$d")
done && \
cd ..
jhnc
  • 11,310
  • 1
  • 9
  • 26