Search code examples
bashdirectoryrelative-pathdirectory-listing

Enter a child directory and list its subdirectories in bash script


I am just starting to learn bash script and have a question that I cannot find an answer to. I am currently in a directory called lab2. Inside this directory I have another directory "students" which contains directories named after each student's netid. Like ~/lab2/students/johndoe. So there are many directories inside students. My script is located inside the lab2 directory and I need to write a script to print out the names of directories inside students directory ( and of course I need to use relative paths).... How do I do that? I tried a few thing, one of which is

$MYDIR="$PWD/students"

DIRS=`ls -l . | egrep '^d' | awk '{print $8}'`

for DIR in $DIRS    
do    
    echo  ${DIR}    
done

but it did not work.... thank you!


Solution

  • $MYDIR="$PWD/students"
    

    but it did not work...

    That's no wonder - the correct syntax is

    MYDIR="$PWD/students"
    

    (without leading $).