• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 370
  • Last Modified:

How to find the directories in a particular directory?

Hi,
I need to check if any directories present in a given directory as it is very tough to check when I have more than 3000 files in the given directory.

thanks
0
roshanmatre
Asked:
roshanmatre
1 Solution
 
TintinCommented:
Depends a little on what flavour Unix you are running.

If you have Linux or a Unix with GNU find, you can do:

find /some/dir -type d -maxdepth 1

or you can do

(cd /some/dir ; ls -1F) | grep /

or a really slow method is:

#!/bin/sh
for i in /some/dir/*
do
  [ -d $i ] && echo $i
done
0
 
wesly_chenCommented:
> check if any directories present in a given directory
cd /the-dir
ls -ld | grep dir-name
option -d in "ls" mean directory

or

find /the-dir -type d -name "dir-name"
0
 
TintinCommented:
wesley_chen.

Your ls solution is not very suitable.

Doing a

ls -ld

is only going to result in . being returned.



0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
wesly_chenCommented:
Hi Tintin, thanks for point out this.
ls -ld *.* | grep dir-name
for the dir in current dir.
0
 
TintinCommented:
That approach is still flawed as you will still pick up file names.

$ ls -l
total 2
drwxrwxr-x   2 user1  other     512 Apr  7 15:54 foo
-rw-rw-r--   1 user1   other       0 Apr  7 15:54 foobar

$ ls -ld *|grep foo
drwxrwxr-x   2 user1  other      512 Apr  7 15:54 foo
-rw-rw-r--   1 user1   other        0 Apr  7 15:54 foobar
0
 
wesly_chenCommented:
Thank Tintin again. I misunderstand "-d" option for ls. Thanks a lot.
0
 
byttaCommented:
This gives you directories only...

case sensitive:
find . -name dir_name  -type d

not case sensitive:
find . -iname dir_name  -type d
0
 
Kent OlsenData Warehouse Architect / DBACommented:

find <base directory> -type d


This is recursive and find all of the subdirectories, too.

0
 
ahoffmannCommented:
> I need to check if any directories present in a given directory

test -d /path/this-dir && echo exists
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now