mercredi 25 avril 2018

Test -d directory true - subdirectory false (POSIX)

I'm trying to print all directories/subdirectories from a given start directory.

for i in $(ls -A -R -p)/*; do 
    if [ -d "$i" ]; then
            printf "$PWD/$i \n"
    fi
done; 

This script returns all of the directories found in the . directory and all of the files in that directory, but for some reason the test fails for subdirectories. All of the directories end up in $i and the output looks exactly the same.
Let's say I have the following structure:

foo/bar/test

echo $i prints

foo/
bar/
test/

While the contents of the folders are listed like this:

./foo:
file1
file2
./bar:
file1
file2

However the test statement just prints:

PWD/TO/THIS/DIRECTORY/foo

For some reason it returns true for the first level directories, but false for all of the subdirectories.

(ls is probably not a good way of doing this and I would be glad for a find statement that solves all of my issues, but first I want to know why this script doesn't work the way you'd think.)

Aucun commentaire:

Enregistrer un commentaire