Windows tree

March 5, 2019 - Reading time: 6 minutes

In which we go off the deep end with tree

Yesterday, maiki posted on Mastodon asking if tree was included in modern versions of Microsoft Windows. maiki wanted an easier way for some friends and family to show them the directory structure of their computer without doing screenshots. Frankly, I didn't know the answer to that as I haven't had the need to use it in quite some time.

My first thought was just to try it.  I use Windows 10 at home, and I just opened a cmd prompt, and sure enough, output started pouring onto the screen. I hit ctrl-c to stop it. I started wondering, though:  Was that tree Windows tree or some other tree I might have installed. Typing tree /? gave me no real indication of its source. A quick trip to a search engine led me to the Microsoft Docs page on tree.  The documentation clearly lined up with the command I was using in the cmd shell. I also wondered, however, what versions of Windows supported tree, since one person said that it had been removed from DOS at one point.  I was not really sure if that was the case, but usually in Microsoft online documentation, the version that it applies to is given at the top of the page. I did not see anything indicating the Windows version this applied to, though. I was a little concerned, also, that the page appeared to be geared towards "Server," and presumably this could apply only to certain server versions rather than all versions. A little clicking around led me to this page where it mentions the command line reference page applies to several Windows versions, including 8.1 and 10.

I reported to maiki my findings that it worked on my copy of Windows 10 and would work on 8.1.  I suggested the form tree c:\ /A >out.txt which should give the output in plain ASCII format and redirect the output to a file out.txt.

Can we accomplish this another way?

I started wondering what if these users have some other version of Windows?  Can they accomplish the same task?  If we're talking about an unsophisticated user, it may not be possible.  After all, you have to get them to install a utility or something else entirely. That may be more complicated than the average user can achieve.  For example, Cygwin comes with tree as a package. Also, there is a tree for Windows with GNUWin32. Both of these require a separate installation and possible dependencies. That may not be too difficult for the average user.

A little search engine work led me to believe that there are several "free" utilities out there that may be able to do this, but none were particularly comforting to me.  While "free," as in beer, they were not "free" as in speech.  I wasn't willing to install them, particularly since tree was clearly working in Windows 10 for me.

A user here suggested you can use dir in Windows to get the directory tree, and even apply some sorting.  The problem with Windows native tree is that it simply dumps the results to the screen (or file) as it finds them.  While it may appear to be sorted, it really isn't.  So if you want sorted output, dir might be the answer.  Microsoft documentation for dir is found here.

A small diversion to WSL

Windows 10 now has what is called Windows Subsystem for Linux which is a bash shell with Ubuntu. If you install that, you could then use an Ubuntu shell to run tree. It does not come installed by default.  Install it with the following:

sudo apt install tree

Once you do that, you can run tree as you would in Linux. To address the Windows file system run it on /mnt/c where c: is the Windows drive you want the tree of.

tree /mnt/c/ -d >output.txt

Use the -d flag to only show directories.

Surprise! tree works on Windows 7

I came into the office today and logged into one of our Windows 7 computers, and found out that tree runs in Windows 7 as well, even though it isn't mentioned on the docs page. I ran it with tree c:\ >out.txt and found that I needed to be in a directory where I could write the output file.  I ran it again. This time, it took a long time.  Later I timed it and it took over three minutes to write a file over 5mb in size! You don't expect there to be that many directories, but indeed there were.

[Edited to add the -d flag for the WSL version. Re-posted to new installation of Bludit on 2019-03-05]

About

Every so often, I want to post something in a longer format than would typically be allowed on Twitter or Mastodon. Sometimes it's just for myself, so I can remember things. Sometimes I want to see what people think about something or other. In the past, I have posted on Blogger, but there's a certain lack of control there. Using Bludit will allow me a bit more control, but won't require me to install a bunch of stuff.