how do I add the 'tree' command to git-bash on Windows?

97,318

Solution 1

You could also use "cmd //c tree" to use Windows' tree

Explanation:

  • Launch cmd with '/c' argument and run tree, then terminate

/C Carries out the command specified by string and then terminates

(extra slash for escaping)

/a use to run with ascii chars in case it doesn't display right.

Answered in greater detail here: https://stackoverflow.com/q/515309/1261166

Solution 2

I have downloaded the tree.exe inside the zip file from here http://gnuwin32.sourceforge.net/packages/tree.htm as suggested.

Then I have extracted the tree.exe file to C:\Program Files\Git\usr\bin (I have added this folder to windows path to make it work with the regular CMD but it works with GITBash too). Git Bash with tree command on windows

I hope this helps you somehow !

Solution 3

There is a tree command in windows already — only problem is it is tree.com and git bash will not automatically add extension .com and execute it.

However it will find it if you press tab after you type tree or tre

To see files you have to use //f — you have to use // or bash will think it is folder name

I also used //a to show ascii lines but you don't have to use it

Example:

dean@dean:~/java$ tree
bash: tree: command not found

dean@dean:~/java$ tree.com //a
Folder PATH listing for volume c
Volume serial number is 4E70-B37A
C:.
+---atom
+---sublime
\---vscode

dean@dean:~/java$ tree.com //a //f
Folder PATH listing for volume c
Volume serial number is 4E70-B37A
C:.
+---atom
|       test1
|
+---sublime
|       test2
|
\---vscode
        test3

dean@dean:~/java$

Solution 4

The GnuWin32 build of tree is at http://gnuwin32.sourceforge.net/packages/tree.htm - you'd need to add it to your path manually if you're not already using GnuWin32.

If you want to use it at a Windows console also you'd need to rename or copy tree.exe to something else, e.g. lstree.exe, otherwise the Windows tree command will take precedence. The advantage of the GnuWin version is that it has lots of options - e.g. tree -L 2 would limit the recursion depth to 2.

> tree --help
usage: tree [-adfghilnpqrstuvxACDFNS] [-H baseHREF] [-T title ] [-L level [-R]]
        [-P pattern] [-I pattern] [-o filename] [--version] [--help] [--inodes]
        [--device] [--noreport] [--nolinks] [--dirsfirst] [--charset charset]
        [--filelimit #] [<directory list>]
  -a            All files are listed.
  -d            List directories only.
  -l            Follow symbolic links like directories.
  -f            Print the full path prefix for each file.
  -i            Don't print indentation lines.
  -q            Print non-printable characters as '?'.
  -N            Print non-printable characters as is.
  -p            Print the protections for each file.
  -u            Displays file owner or UID number.
  -g            Displays file group owner or GID number.
  -s            Print the size in bytes of each file.
  -h            Print the size in a more human readable way.
  -D            Print the date of last modification.
  -F            Appends '/', '=', '*', or '|' as per ls -F.
  -v            Sort files alphanumerically by version.
  -r            Sort files in reverse alphanumeric order.
  -t            Sort files by last modification time.
  -x            Stay on current filesystem only.
  -L level      Descend only level directories deep.
  -A            Print ANSI lines graphic indentation lines.
  -S            Print with ASCII graphics indentation lines.
  -n            Turn colorization off always (-C overrides).
  -C            Turn colorization on always.
  -P pattern    List only those files that match the pattern given.
  -I pattern    Do not list files that match the given pattern.
  -H baseHREF   Prints out HTML format with baseHREF as top directory.
  -T string     Replace the default HTML title and H1 header with string.
  -R            Rerun tree when max dir level reached.
  -o file       Output to file instead of stdout.
  --inodes      Print inode number of each file.
  --device      Print device ID number to which each file belongs.
  --noreport    Turn off file/directory count at end of tree listing.
  --nolinks     Turn off hyperlinks in HTML output.
  --dirsfirst   List directories before files.
  --charset X   Use charset X for HTML and indentation line output.
  --filelimit # Do not descend dirs with more than # files in them.

Compared to the Windows tree:

> tree /?
Graphically displays the folder structure of a drive or path.

TREE [drive:][path] [/F] [/A]

   /F   Display the names of the files in each folder.
   /A   Use ASCII instead of extended characters.

Solution 5

Git for Windows (https://gitforwindows.org/) (has Git Bash) but it does not include tree. tree is available for via pacman (Package Manager) but that is only available if you install "Git for Windows SDK" (scroll to the bottom of gitforwindows.org/ which provides a link to download installer for it from https://github.com/git-for-windows/build-extra/releases/latest)

This SO: "Package management in git for windows?" was very helpful https://stackoverflow.com/questions/32712133/package-management-in-git-for-windows

Also as commented in the above SO, they link to this git for windows issue [Pacman missing on fresh 2.5.2 install #397] that it was intended to not include pacman in the default install.

Anyways, I installed "Git for Windows SDK", then in it's bash prompt (SDK-64) I ran the following to install current tree v1.7.0-1 (as of this posting Aug 30, 2018):

[SDK-64: Bash Terminal for Git for Windows SDK]
pacman -S tree
...
Proceed with installation? [Y/n] Y

On my system, Git for Windows SDK is installed under: C:\git-sdk-64, so from my Git for Windows Bash shell (that did not have tree installed), I copied it over tree.exe to it's /usr/bin directory, e.g.

[MINGW64: Bash Terminal for Git for Windows]
cd /usr/bin
cp /c/git-sdk-64/usr/bin/tree.exe .

Now I can run tree v1.7.0 from both Git Bash shells.

So, to make it even easier for others and maybe myself on a future machine, I looked at where pacman was getting the tree package from by running following in my Git for Windows SDK Bash terminal:

$ pacman -S --info tree
Repository      : msys
Name            : tree
Version         : 1.7.0-1
Description     : A directory listing program displaying a depth indented list of files
Architecture    : x86_64
...

The key thing, here is that pacman is getting it from the "msys" repository (FYI: even though it says msys, it really is using msys2), so I looked at /etc/pacman.d/mirrorlist.msys and the first mirror points to http://repo.msys2.org/msys/$arch/

So next time you want a package that is NOT in Git for Windows, you can download them from: http://repo.msys2.org/msys/x86_64/ (for 64-bit) or from http://repo.msys2.org/msys/i686/ (32-bit)

e.g. direct download link for tree v1.7.0-1

Share:
97,318

Related videos on Youtube

jcollum
Author by

jcollum

Updated on September 18, 2022

Comments

  • jcollum
    jcollum over 1 year

    I'm using git-bash on Windows 7. I'd like to see a tree of the current directory. However:

    jcollum@DEVELOPER01 ~/Dev/express_coffee            
    $ tree .                                            
    sh.exe": tree: command not found
    

    OK, so I don't have the tree command. How do I install it? I found an article but it was for Macs.

    • allquixotic
      allquixotic over 11 years
      git-bash is really just a cut down version of Cygwin. Best way to go is install Cygwin from cygwin.com, and use the package manager there to install tree or whatever package it's in (if it exists).
    • jcollum
      jcollum over 11 years
      @allquixotic actually these days it's mingw
  • Jen S.
    Jen S. over 10 years
    So is it impossible in minggw?
  • niken
    niken about 8 years
    IMO, this is the most straight forward way to get tree cmd in Cygwin and I tried the compilation route before unsuccessfully. With this you just download the binary, extract it to a folder on your cygwins path and violla, tree command out of the box
  • cjauvin
    cjauvin over 7 years
    Works very well with git-bash on Windows 7, thanks!
  • Timo
    Timo over 6 years
    Not the solution. The alias shows the commit and not the files / dirs.
  • Timo
    Timo over 6 years
    in cygwin: apt-cyg install tree and tree.exewill be in the cygwin bin folder.
  • Doogle
    Doogle over 6 years
    tree.exe copied to C:\Program Files\Git\usr\bin is what we were looking for. Great share . Thanks. downloads.sourceforge.net/gnuwin32/tree-1.5.2.2-bin.zip Download the zip specified for binaries it has tree.exe. Contrary try to download from the original site.
  • N. Ngo
    N. Ngo over 5 years
    FYI: Git SCM's Window's download at git-scm.com/download pulls the latest from Git for Windows GitHub (github.com/git-for-windows/git/releases) from github.com/git-for-windows/git
  • Charles L.
    Charles L. over 5 years
    In windows 10 I put this in C:\Users\myuser\AppData\Local\Programs\Git\usr\bin I think the rule of thumb is to find where bash.exe or git.exe are located, go up a folder, then find usr/bin/ and put tree there
  • Santosh Kumar Arjunan
    Santosh Kumar Arjunan over 5 years
    awesome, worked like a charm..
  • onlyhuman
    onlyhuman about 5 years
    you can also add this command as an alias to your ~/.bashrc: alias tree='cmd //c tree //a
  • automorphic
    automorphic over 4 years
    This no longer works. Use tree.com /a
  • mgross
    mgross over 3 years
    @automorphic I think it has to be tree.com //a as stated below. With a single forward slash the command does not seem to work.
  • Pat. ANDRIA
    Pat. ANDRIA over 3 years
    At this day it still works. Thank you
  • Smajjk
    Smajjk about 3 years
    cmd //c tree //a //f to also display the names of the files in each directory.