In zsh you can qualify globs with file type assertions e.g. *(/)
matches only directories, *(.)
only normal files, is there a way to do the same thing in bash without resorting to find?
views:
29answers:
2
A:
I don't think that there's a way to do this directly, but don't forget that you can use the test options -d
and -f
to determine whether name refers to a directory or a file.
for a in *; do
if [ -d "$a" ]; then
echo Directory: $a
elif [ -f "$a" ]; then
echo File: $a
fi
done
Tim
2010-10-26 05:29:53
Unfortunately that's worse than using find - I can use find with xargs to get a command line to pass to another script instead of testing each result separately. I just wanted a globbing based way because you can't use xargs with bash functions...
tobyodavies
2010-10-26 05:36:35
I'm surprised that you think that invoking several new processes is in some way better this direct approach. Could you explain more about what you want to do in the question?
Tim
2010-10-26 05:40:42
@Tim to clarify, using ghostdog74's solution, i want to call a bash function `fooBar */` if i were to make `fooBar` a script i could `find . -maxdepth 1 -type d -print0 | xargs -0 fooBar` which is much easier than constructing an array inside a for loop
tobyodavies
2010-10-26 06:38:47
+1
A:
you can try
ls -ltrd */ #match directories using -d and the slash "/"
or
echo */
or
for dir in */
do
...
done
If you need to do it recursive, and you have Bash 4+
$ shopt -s globstar
$ for dir in **/*/; do echo $dir; done
ghostdog74
2010-10-26 05:38:08
that is so obvious, i feel a little stupid... tho all the options to ls other than the -d are superfluous :)
tobyodavies
2010-10-26 06:33:52