Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

316
Views
how to get a sub-directory with for-loop & find & if-statement in linux terminal

given the following structure:

.
├── a1
│   ├── q
│   ├── w
│   └── e
├── a2
│   ├── q
│   ├── e
│   └── s

I want a nested for-loop & find $ if-statement that has to work in the linux.

The expected output is the structures of sub-directory after checking if it contains a specific string in declared array.

I want something like this code:

declare -a folderlist=("a1", "a2")
declare -a checklist=("w", "s")

for folder in "${folderlist[@]}";
do
  subfolders=$(ls ./$folder);
  for subfolder in "${subfolders[@]}";

    do 
      if [ $d == "w" -o $d == "s" ]; 
         then echo ./$folder/$subfolder;
      fi;
    done;

done;

I wonder this part above:

do 
  if [ $d == "w" -o $d == "s" ]; 
     then echo ./$folder/$subfolder;
  fi;
done;
  • How can I change [ $d == "w" -o $d == "s" ] part to something like if [ $d is in checklist ] ?

Expected Ouput:

./a1/w/
./a2/s/
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Why not use find for the whole thing?

# Test directory
$ find .
.
./a1
./a1/e
./a1/q
./a1/w
./a2
./a2/e
./a2/q
./a2/s

# Find sub-directories called `s` and `w`
$ find . -mindepth 2 -type d -name w -or -name s
./a1/w
./a2/s

Multi-level find

$ find . -mindepth 1 -maxdepth 1 -type d \( -name a1 -o -name a2 \) -print0 |
  xargs -0 -I{} find {} -type d -name w -o -name s
./a1/w
./a2/s
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!