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
1
  • 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 Commented Oct 6, 2021 at 16:24

1 Answer 1

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 ..
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.