user485498
user485498

Reputation:

bash find directories

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?

Upvotes: 43

Views: 88998

Answers (9)

Tono Nam
Tono Nam

Reputation: 36080

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

Upvotes: 0

Jason Jewel
Jason Jewel

Reputation: 57

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.

Upvotes: 6

fiorentinoing
fiorentinoing

Reputation: 1007

In the particular case you are looking for a 1) directory which you know the 2) name, why not trying with this:

find . -name "octave" -type d

Upvotes: 48

clt60
clt60

Reputation: 63972

Here is already much solutions, so only for fun:

 file ./**/* | grep directory | sed 's/:.*//'

Upvotes: 4

Learner
Learner

Reputation: 672

Following lines may give you an idea...what you are asking for

#!/bin/bash

for FILE in `ls -l`
do
    if test -d $FILE
    then
      echo "$FILE is a subdirectory..."
    fi
done

You may have a look into bash 'for' loop.

Upvotes: 5

Johan
Johan

Reputation: 1988

Not sure.. but maybe the tree command is something you should look at. http://linux.die.net/man/1/tree

tree -L 2 -fi

Upvotes: 0

Jan Marek
Jan Marek

Reputation: 11200

You have to use:

ls -lR | isitadirectory.sh

(parameter -R is recursion)

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799310

find . -mindepth 1 -maxdepth 1 -type d

Upvotes: 81

zb'
zb'

Reputation: 8059

try to use

find $path -type d ?

for current directory

find . -type d

Upvotes: 8

Related Questions