How do I get a list of the fullpaths of every .git subfolder in a directory tree?

1

I am unable to figure out the right set of commands in a Windows Command prompt FOR /? or bash ls to do this.

The following batch file I wrote demonstrates what I want, but I would like to know if there's a way to do it without a batch file.

@echo off
for /r %%i in (.) do if %%~nxi==.git echo %%i

Seems like I should be able to issue a DIR command with a combination of /s /b and /ad to do this. I'm willing to filter through grep or FIND if I need to.

I obviously don't need a solution that require a script since I already have that as demonstrated above.

Also, Powershell may be a candidate as long as it's a simple command.

Daisha Lynn

Posted 2017-08-18T03:24:24.220

Reputation: 13

Replace %% with % to use the command line: for /r %i in (.) do if %~nxi==.git echo %i – DavidPostill – 2017-08-18T14:08:28.333

That won't work. If you have 1000s of files and directories, you will see 1000s of lines scroll by, and you'll hardly see the one where it echo's out. You'd have to echo >> to some file and the 'type' that file out. Seems painful. – Daisha Lynn – 2017-08-19T21:55:08.840

{shrug} You can tweak that command to only echo the stuff you want (using @) and pipe | the output to more – DavidPostill – 2017-08-19T22:01:06.250

David, I did try the "@" trick before I posted this question. It didn't work. I did not try the pipe more trick. I'll try that and let you know. – Daisha Lynn – 2017-08-19T23:02:41.843

It worked. I put the @ on the if not on the for. So the line looks like this: for /r %i in (.) do @if %~nxi==.git echo %i. Can you please create an answer with your suggestion so I can accept it? – Daisha Lynn – 2017-08-19T23:05:32.397

Done. Answer provided. – DavidPostill – 2017-08-20T07:49:40.207

Answers

0

The following batch file I wrote demonstrates what I want

but I would like to know if there's a way to do it without a batch file.

@echo off
for /r %%i in (.) do if %%~nxi==.git echo %%i

You can do this from the command line by making the following changes:

  1. Replace %% with %

  2. Replace if with @if` to remove superfluous output

So the command becomes:

for /r %i in (.) do @if %~nxi==.git echo %i

If the output is more than one screenfull you can either:

  • Pipe the output to more

    for /r %i in (.) do @if %~nxi==.git echo %i | more
    

Or:

  • Redirect the output to a file and examine it later.

    for /r %i in (.) do @if %~nxi==.git echo %i > output.txt
    more output.txt
    

DavidPostill

Posted 2017-08-18T03:24:24.220

Reputation: 118 938