bash find directories

Viewed 79282

I am new to bash scripts. I'm just trying to make a script that will search through a directory and echo the names of all subdirectories.

The basis for the code is the following script (call it isitadirectory.sh):

     #!/bin/bash

     if test -d $1
         then
                echo "$1"
     fi

so in the command line if I type

       $bash isitadirectory.sh somefilename 

It will echo somefilename, if it is a directory.

But I want to search through all files in the parent directory.

So, I'm trying to find a way to do something like

           ls -l|isitadirectory.sh

But of course the above command doesn't work. Can anyone explain a good script for doing this?

9 Answers
find ./path/to/directory -iname "test" -type d

I found this very useful for finding directory names using -iname for case insensitive searching. Where "test" is the search term.

In my case I needed the full path so I ended up using

find $(pwd) -maxdepth 1 -type d -not -path '*/\.*' | sort

I have a lot of repos that I need to pull with one bash script. Here is the script:

#!/bin/bash

cd /path/where/to/look/for/dirs

# Iterate over files ending with .pub
echo -e "Pulling all repos"
FILES=`find $(pwd) -maxdepth 1 -type d -not -path '*/\.*' | sort`
for f in $FILES
do
    echo -e "pulling from repo $f..."
    cd $f
    git pull
done
Related