42

How do I limit the number of levels that the tree command goes through in Windows? I need to output the results to a text file for work, but because by default the tree command lists every single directory under the one you ran the command in, the output I'm getting is over 44,000 lines long, which isn't helpful at all for my work. How do I restrict it to listing just the first couple levels?

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
iqover9000
  • 573
  • 1
  • 5
  • 7
  • `tree` itself doesn't seem to have the ability to limit the depth of the tree. It just has two switches, one for additionally display the filenames in each level, and one to switch to ASCII characters for the tree lines. – BlackJack Aug 10 '15 at 19:33

7 Answers7

24

Since I didn't found complete answer here. Here it is:

Windows CMD doesn't support -L depth levels.

  1. Install CygWin https://www.cygwin.com.
  2. In Cygwin make sure you pick Utilities / Tree package installed.
  3. Open CygWin and navigate to your folder, like cd ../../cygdrive/c/myFolder.
  4. List tree structure and save as result.txt tree -L 3 >result.txt.
copolii
  • 14,208
  • 10
  • 51
  • 80
Dariusz Sikorski
  • 4,309
  • 5
  • 27
  • 44
  • 2
    it should be "tree -L 3 >result.txt" – psaha4 Jul 31 '17 at 18:20
  • Future visitor: Now that it does say `-L 3` I will save you a few clicks and leave this note saying that it was `-L -3` previously. My curiosity got the better of me and I had to check the edit log. – Samir Feb 22 '21 at 23:17
  • Thanks for the note @Samir, may I ask why did you point out previous `-L -3` version instead of current `-L 3` and said it saves a few cliks? If the previous command before editing is somewhat better, then of course I will edit the answer. Currently I'm using Linux and can't verify what's the benefit in the previous version. – Dariusz Sikorski Feb 23 '21 at 11:06
  • You can safely ignore me. There is no need to edit. The current command is correct, the previous one was not. I'm sorry for the confusion. – Samir Feb 24 '21 at 09:40
  • The first comment states how to write the command correctly rather than what (specifically) was incorrect about the command that was written at the time. I find this a bit odd. At least in my mind, that's a reversed thought process. I prefer to see what's wrong with a solution first, followed by a fix, in that order. This way, when someone comes along later (author or editor), reads the comments and then makes corrections, I can immediately see what was wrong and how it was addressed (only the most recent edit is visible of course). It makes my brain happy when I can easily trace the changes. – Samir Feb 24 '21 at 09:44
  • (In Git terms, I like to view the comments section as a place for reporting issues rather than making pull requests. Since it runs long, it can serve as a change log, so I don't need to click to view revisions in detail. That's what I meant by saving a few clicks.) – Samir Feb 24 '21 at 11:15
21

Actually, the tree command in DOS and Windows does not have the option for specifying the directory level that the command goes through. You can refer to the documentation of tree on Microsoft Docs.

But you can use Git Bash instead. This tool is provided when you install Git for Windows. So in this way, you can use the command that @Zhengquan Feng mentioned which was a deleted answer.

tree -L 3 // three levels show
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
Jesse Chen
  • 469
  • 5
  • 7
  • 1
    Babun is [discontinued](https://github.com/babun/babun/issues/868) and I don't think you were ever able to get the Unix version of the `tree` command on Windows by simply installing Git for Windows. Git Bash only supports built-in commands, and `tree` is not one of them. You would have needed Babun at least, or Cygwin and friends. – Samir Feb 23 '21 at 00:06
  • 1
    It's possible that it only worked by accident, if you had Babun installed alongside Git for Windows. In that case, Babun would have been responsible for giving you the Unix version of the `tree` command, not Git Bash. – Samir Feb 23 '21 at 00:17
11
  1. Try to download WSL in your Windows system.

  2. In your command prompt:

    bash

  3. Then you can use Linux commands, this example is for 3 levels deep:

    tree -L 3

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
Newt
  • 787
  • 8
  • 15
5

You can try this project on Windows :

https://github.com/MrRaindrop/tree-cli

Usage:

use -l levelNumber to specify the path level:

treee -l 2

Willard
  • 369
  • 4
  • 6
4

Example 3 levels:

tree|findstr /v /r /c:"^........│" /c:"^│....... " /c:"^ ....... "
SF Q
  • 41
  • 1
4

If you use WSL you can also export the result straight to the Windows clipboard

  • From the WSL Cline, if you don't already have tree installed: sudo apt install tree
  • Then: tree -L 5 | clip.exe (replace 5 with however many levels deep you want)

That'll bypass saving it to a txt file and go straight to your clipboard for quick pasting into forums, etc.

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
4

I know the question is old, but here in Powershell :

(I know, it needs a little cleaning, hiding variables for the end user, etc... but it works without installing anything)

$sb = New-Object System.Text.StringBuilder

Function Get-Tree {
    Param ([String]$Path, [Int]$LevelMax, [Int]$Level = 0, [Bool]$LastOfTheList=$true, [String]$Lead="")

    if ($Level -eq 0) {$sb.AppendLine($Path)}
    if ($Level -eq $LevelMax) { Return }

    $Lead = if($LastOfTheList){"$Lead   "}else{"$Lead$([char]0x2502)  "}

    [Array]$Liste = $Path | Get-ChildItem -Directory

    For($x=0;$x -lt $Liste.count;$x++) {
        $item = $Liste[$x]
      
        if ($x -eq $Liste.Count-1) { 
            $sb.AppendLine("$($Lead)$([char]0x2514)$([char]0x2500)$([char]0x2500)$($item.Name)") | Out-Null 
            Get-Tree -Path $item.Fullname -level ($level +1) -LevelMax $LevelMax -Lead $Lead -LastOfTheList $true
        }
        else { 
            $sb.AppendLine("$($Lead)$([char]0x251c)$([char]0x2500)$([char]0x2500)$($item.Name)") | Out-Null 
            Get-Tree -Path $item.Fullname -level ($level +1) -LevelMax $LevelMax -Lead $Lead -LastOfTheList $false
        }
    }
}

Get-Tree -Path "MyPath" -LevelMax 3
$sb.ToString() | Out-File "MyOutputFile.txt" -Encoding UTF8
Epervier 666
  • 97
  • 1
  • 8