Ubuntu – How to write out all directory names into a text file

bash

I want to make a list of my subdirectories. When I execute the following for loop, the first directory name occurs two times

for d in */; do
     echo $d >> directories.txt
done

The output is like :

ONE/
TWO/
ONE/

Where is the problem?

Best Answer

Improving you current code, this command should be helpful:

for d in ./*;do [[ -d "$d" ]] && echo "$d" >> dir.txt; done

To remove the ./ from the output:

for d in ./*;do [[ -d "$d" ]] && echo "${d##./}" >> dir.txt; done