Reputation: 1261
I want to check whether there exist directory named "name" in the current working directory. is it possible to do with ls?
ls -l | grep ^-d
shows just directories but how to specify?
thanks in advance
Upvotes: 4
Views: 1249
Reputation: 45670
One should never parse the output of ls. If you are using bash, try this
if [ -d "$DIRECTORY" ]; then
# will enter here if $DIRECTORY exists.
fi
Upvotes: 3
Reputation: 8292
This is what the -d
test is for. Simply:
if [ -d "name" ]; then
echo "yay!"
else
echo "nay!"
fi
Upvotes: 3
Reputation: 995
test -d 'name' && echo "It is there"
The test -d 'name'
can be used in if statements and the like as well.
Upvotes: 2