Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing all subdirectories with a specified name

I am trying to get a list of paths of all subdirectories (recursively) which have some specified name, e.g. "bin". The problem is that if current directory contains subdirectory of that name, DIR command will be executed only within that subdirectory, ignoring other subdirectories.

Example:

C:\DEVELOPMENT\RESEARCH>ver

Microsoft Windows [Version 6.1.7601]

C:\DEVELOPMENT\RESEARCH>dir *bin* /ad /s /b
C:\DEVELOPMENT\RESEARCH\bin
C:\DEVELOPMENT\RESEARCH\Apache\2bin
C:\DEVELOPMENT\RESEARCH\Apache\bin
C:\DEVELOPMENT\RESEARCH\Apache\bin1
C:\DEVELOPMENT\RESEARCH\C#\ConsoleApps\MiscTests\bin

C:\DEVELOPMENT\RESEARCH>dir bin* /ad /s /b
C:\DEVELOPMENT\RESEARCH\bin
C:\DEVELOPMENT\RESEARCH\Apache\bin
C:\DEVELOPMENT\RESEARCH\Apache\bin1
C:\DEVELOPMENT\RESEARCH\C#\ConsoleApps\MiscTests\bin

C:\DEVELOPMENT\RESEARCH>dir bin /ad /s /b
C:\DEVELOPMENT\RESEARCH\bin\test    

C:\DEVELOPMENT\RESEARCH>rmdir bin /s /q

C:\DEVELOPMENT\RESEARCH>dir bin /ad /s /b
C:\DEVELOPMENT\RESEARCH\Apache\bin
C:\DEVELOPMENT\RESEARCH\C#\ConsoleApps\MiscTests\bin

C:\DEVELOPMENT\RESEARCH>

dir *bin* /ad /s /b outputs all subdirectories that have bin in their name. And this output is ok. Same with dir bin* /ad /s /b which outputs all subdirectories which name begins with bin. But dir bin /ad /s /b outputs only the content of the first child of the current directory which has name bin. Desired output is:

C:\DEVELOPMENT\RESEARCH\bin
C:\DEVELOPMENT\RESEARCH\Apache\bin
C:\DEVELOPMENT\RESEARCH\C#\ConsoleApps\MiscTests\bin

How can I achieve this?

NOTE: If current directory does not contain bin child, output is as expected. (I deleted bin child to show this)

like image 519
Bojan Komazec Avatar asked Apr 03 '12 11:04

Bojan Komazec


People also ask

How do I list files in all subdirectories?

By default, ls lists just one directory. If you name one or more directories on the command line, ls will list each one. The -R (uppercase R) option lists all subdirectories, recursively.

How can I list subdirectories recursively?

Try any one of the following commands to see recursive directory listing: ls -R : Use the ls command to get recursive directory listing on Linux. find /dir/ -print : Run the find command to see recursive directory listing in Linux. du -a . : Execute the du command to view recursive directory listing on Unix.

How do you grep all subdirectories?

To Search Subdirectories To include all subdirectories in a search, add the -r operator to the grep command. This command prints the matches for all files in the current directory, subdirectories, and the exact path with the filename.


1 Answers

This should work:

for /R /D %A in (*bin*) do echo %A
like image 103
Aacini Avatar answered Sep 23 '22 18:09

Aacini