bash find directories

75,146

Solution 1

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.

Solution 2

find . -mindepth 1 -maxdepth 1 -type d

Solution 3

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

Solution 4

try to use

find $path -type d ?

for current directory

find . -type d

Solution 5

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.

Share:
75,146
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    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?

  • Lynch
    Lynch almost 13 years
    ls -l will not work. Also consider using * instead of parsing ls output (Why you shouldn't parse the output of ls).