1

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!

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
doubleOK
  • 353
  • 6
  • 19
  • possible duplicate of [How to loop over directories in Linux?](http://stackoverflow.com/questions/2107945/how-to-loop-over-directories-in-linux). Good luck with your lab marking ;) – Tom Fenech May 13 '14 at 07:58
  • I checked that one out... but it did not work for me, or maybe I did not understand it... I managed to make it work simply by trial and error, but I am still struggling with understanding why $PWD/students did not work. it informed me that the directory did not exist... – doubleOK May 13 '14 at 10:31
  • If it didn't work and you're sure that this question is not a duplicate, you should edit it so that the difference is clear. If you are in the directory `~/lab2` there's no reason why `for d in students/*/;do echo "$d";done` shouldn't work (I was going to post an answer) but that's pretty much exactly what was posted on the other question. – Tom Fenech May 13 '14 at 10:56

3 Answers3

3

Check if following helps:

find ~/lab2/students -type d -maxdepth 1

And in case you want the directory(students name) names only:

find ~/lab2/students -maxdepth 1 -type d  | awk -F/ '{print $NF}'

Please read man page of find, it will help a lot.

awk -F/ '{print $NF}' is filtering the last field.

And in case you want to something with those directory names in your bash script you can do something like:

#!/bin/bash

for file in $(find ~/lab2/students -maxdepth 1 -type d) #or you can use the awk one here
do

echo $file
#your stuff here

done
PradyJord
  • 2,136
  • 12
  • 19
  • I actually found a way around it and "cd"ed into the students directory at the top of the script simply by cd students/ after that defining the route by smth like ls $PWD/$DIR works.... SO why did not it work before...??? Basically here is what I have right now, except my last part doesn't work yet=)))) (where I am trying to manually invoke bash to run gettysburg files... – doubleOK May 13 '14 at 10:18
  • mmmm I think I will have to update after the timer allows me to post an answer... right now it does not let me post the code - a few characters too long, and formatting looks really crappy – doubleOK May 13 '14 at 10:18
  • 1
    This gives the wrong result if either the path or any of the folders contain spaces. It then reports each "word" in the path separately in $file. A workaround I found is to do `find ~/lab2/students -maxdepth 1 -type d -print0 | while read -d $'\0' file`. – uliwitness Nov 04 '15 at 01:10
1

Expanding on @Jord's answer, you can use find with some options to get some very fine grained control over the output:

  • -mindepth and -maxdepth control the recursion depth (which is 0-infinite by default).
  • -type d lists only directories.
  • -printf %f prints the name of the directory (or file, or symlink, etc.) without the leading directories.

man find has more information, and info --raw-escapes --subnodes find | less --raw-control-chars has full details.

l0b0
  • 55,365
  • 30
  • 138
  • 223
1
$MYDIR="$PWD/students"

but it did not work...

That's no wonder - the correct syntax is

MYDIR="$PWD/students"

(without leading $).

Armali
  • 18,255
  • 14
  • 57
  • 171