Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now
1 of 2

Loop that lists folders with spaces

Approach

I have a directory with named folders, randomly, with blank spaces and periods.

I created a small loop (in a script) with the intention of renaming these folders. On the basis that these are my directories (test and empty):

$ ls -la
./
../
35._JK_io/
'43A. io kl_ -'/
'Mi.Nombre.es Adios'/

Note: We have tried (creo) that all the possibilities of listing and have worked at the time of differentiating between directories and folders.

Now, I proceeded to create a loop so that I would list only the directories and (in the future) rename them:

for archives in `sh -c 'ls -q */' sh`

do

echo  "$archives"
done

Issue

The problem is that when I run the script I get:

35._JK_io/:
43A.
io
kl_
-/:
Mi.Nombre.es
Adios/:

Question

How do I prevent this from happening to me and to appear with spaces and as only three files?

Observation

When executing other scripts on the folders it has obtained that 35._JK_io / is a directory but the other two are not

Thank you!!