Bash ejemplo de bucle recorriendo contenido de una carpeta

Bash ejemplo de loop para recorrer el contenido de un directorio

El script consiste en obtener el contenido de una carpeta, recorrer su contenido, comprobar si cumple con un nombre y si es así verificar si es un directorio. Si se dan todas estas condiciones se imprime la ruta completa de la carpeta y se obtiene todo su contenido.

El código

#!/bin/bash #loop-2.sh # $PATHPRJ variable de entorno que tiene una ruta como /my-space/projects pathdir=$PATHPRJ # array con carpetas que no se tomarán en cuenta arexclude=("temper" "lacia" "tmp") for file in $(ls $pathdir) do # si el nombre del archivo está en los strings excluidos salta al siguiente valor # linea equivalente en php: in_array($file, $arexclude) if [[ ${arexclude[*]} =~ $file ]]; then continue; fi pathfile="$pathdir/$file" # si el archivo no es un directorio salta al siguiente valor # linea equivalente en php: !is_dir($patfile) if [ ! -d $pathfile ]; then continue; fi # el fichero es un dicrectorio por lo tanto se puede ejecutar # cualquier comando con esta ruta. por ejemplo un ls # -e permite entender \n como salto de linea echo -e "\n===========\n$pathfile is a dir\n==========\n" ls -lat $pathfile done

El resultado

===========
some-path/projects/lambda-builds is a dir
==========

total 24
drwxr-xr-x  63 ioedu  staff  2016 19 nov 11:24 ..
-rw-r--r--@  1 ioedu  staff  8196 26 jul  2021 .DS_Store
drwxr-xr-x   5 ioedu  staff   160 13 mar  2021 prj-python
drwxr-xr-x   4 ioedu  staff   128 13 mar  2021 .

===========
some-path/projects/php-ddd-example is a dir
==========

total 840
drwxr-xr-x  63 ioedu  staff    2016 19 nov 11:24 ..
drwxr-xr-x   8 ioedu  staff     256 27 abr  2022 .idea
drwxr-xr-x  13 ioedu  staff     416 26 abr  2022 .git
drwxr-xr-x  20 ioedu  staff     640 26 abr  2022 .
drwxr-xr-x   5 ioedu  staff     160 26 abr  2022 tests
drwxr-xr-x   7 ioedu  staff     224 26 abr  2022 src
-rw-r--r--   1 ioedu  staff    1068 26 abr  2022 psalm.xml
-rw-r--r--   1 ioedu  staff    1393 26 abr  2022 phpunit.xml
drwxr-xr-x   6 ioedu  staff     192 26 abr  2022 etc
-rwxr-xr-x   1 ioedu  staff    3013 26 abr  2022 docker-compose.yml
-rw-r--r--   1 ioedu  staff  384631 26 abr  2022 composer.lock
-rw-r--r--   1 ioedu  staff    1907 26 abr  2022 composer.json
-rw-r--r--   1 ioedu  staff      54 26 abr  2022 behat.yml
drwxr-xr-x   5 ioedu  staff     160 26 abr  2022 apps
-rw-r--r--   1 ioedu  staff    7171 26 abr  2022 README.md
-rw-r--r--   1 ioedu  staff    3159 26 abr  2022 Makefile
-rw-r--r--   1 ioedu  staff    1052 26 abr  2022 Dockerfile
-rw-r--r--   1 ioedu  staff     137 26 abr  2022 .gitignore
drwxr-xr-x   4 ioedu  staff     128 26 abr  2022 .github
-rw-r--r--   1 ioedu  staff    1178 26 abr  2022 .env

===========
some-path/projects/prj_ads is a dir
==========

total 8
drwxr-xr-x  63 ioedu  staff  2016 19 nov 11:24 ..
drwxr-xr-x  16 ioedu  staff   512 27 jun  2020 .git
-rw-r--r--   1 ioedu  staff    26 27 jun  2020 README.md
drwxr-xr-x   5 ioedu  staff   160 27 jun  2020 .
drwxr-xr-x   4 ioedu  staff   128 24 jun  2020 doblerr.es

===========
some-path/projects/prj_amazonws is a dir
==========

total 0
drwxr-xr-x  63 ioedu  staff  2016 19 nov 11:24 ..
drwxr-xr-x  13 ioedu  staff   416 11 feb  2020 .git
drwxr-xr-x   3 ioedu  staff    96  7 feb  2020 platzi_awscloud
drwxr-xr-x   4 ioedu  staff   128  7 feb  2020 . 
...

Autor: Eduardo A. F.
Publicado: 20-11-2022 21:04