UFC Insider
UFC Insider

Reputation: 838

Return the number of directories

I have this piped a command that tells me how many directories are inside the current directory:

ls -lR | grep ^d | wc -l

But is there a way to check for a given directory? Something like:

ls -lR | grep ^d | wc -l /folder1/?

Upvotes: 0

Views: 27

Answers (2)

giusti
giusti

Reputation: 3538

I suggest you use find. With -type d you can tell find to search only for directories. Like so

find /folder1 -type d | wc -l

The advantage is that you can easily change this to retrieve the names of the directories and also act on them with -exec.

The drawback is that this command also counts the directory /folder or ./, but that's easily circumvented:

find /folder1 -mindepth 1 -type d | wc -l

Upvotes: 0

tinkertime
tinkertime

Reputation: 3042

I think your just passing /folder1 to the wrong cmd

ls -lR /folder1 | grep ^d | wc -l

Upvotes: 1

Related Questions