Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

319
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda