Adilli Adil
Adilli Adil

Reputation: 1261

find a particular directory in working directory in unix

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

Answers (4)

Fredrik Pihl
Fredrik Pihl

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

Christopher Neylan
Christopher Neylan

Reputation: 8292

This is what the -d test is for. Simply:

if [ -d "name" ]; then
    echo "yay!"
else
    echo "nay!"
fi

Upvotes: 3

eatonphil
eatonphil

Reputation: 13712

Try this?

ls -l | grep ^d name

Upvotes: 1

Dr.Tower
Dr.Tower

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

Related Questions