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?
19 Answers
find . -mindepth 1 -maxdepth 1 -type d 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 try to use
find $path -type d ?
for current directory
find . -type d
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.
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.
1Here is already much solutions, so only for fun:
file ./**/* | grep directory | sed 's/:.*//' You have to use:
ls -lR | isitadirectory.sh (parameter -R is recursion)
Not sure.. but maybe the tree command is something you should look at.
tree -L 2 -fi 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