Mac OS X equivalent of the Ubuntu "tree" command
Is there an equivalent to the Ubuntu tree
command for Mac OS X?
Solution 1:
You can get the tree
command on macOS, too. If you have Homebrew:
brew install tree
If you do not have Homebrew installed, try one approach below.
Installing a package manager approach
Follow the instructions on these websites to install Homebrew, MacPorts, or Fink. Do not install more than one package manager at the same time!
Follow the prompt for whichever you installed.
For Homebrew: brew install tree
For MacPorts: sudo port install tree
For Fink: fink install tree
Installing from source approach
-
Install the Xcode command line tools by running
xcode-select --install
. -
Download the
tree
source -
Change the Makefile to get it to work, which is also explained in @apuche's answer below. Commenting out the Linux options and uncommenting the macOS options should be enough.
-
Then, run
./configure
, thenmake
. -
Now you have to move the
tree
binary file to a location that's in your executable path. For example:sudo mkdir -p /usr/local/bin sudo cp tree /usr/local/bin/tree
-
Now edit
~/.bash_profile
to include:export PATH="/usr/local/bin:$PATH"
-
Reload the shell, and now
which tree
should point to/usr/local/bin/tree
.
Solution 2:
Not exactly the same, but one quick way on the Mac is:
find .
and that's it. It will list all file paths in the current directory as a list.
Solution 3:
Or if your administrator won't let you install any of the brew
, fink
, port
tools you can always build it from the source :
curl -O ftp://mama.indstate.edu/linux/tree/tree-1.5.3.tgz
tar xzvf tree-1.5.3.tgz
cd tree-1.5.3/
ls -al
Edit the Makefile to comment linux part and uncomment osx area:
# Linux defaults:
#CFLAGS=-ggdb -Wall -DLINUX -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
#CFLAGS=-O2 -Wall -fomit-frame-pointer -DLINUX -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
#LDFLAGS=-s
# Uncomment for OS X:
CC=cc
CFLAGS=-O2 -Wall -fomit-frame-pointer -no-cpp-precomp
LDFLAGS=
XOBJS=strverscmp.o
Optional: Forcing color output
And while you're at it, if you want to force tree to always colorize the output, you can always edit the main
method of the tree.c
file and add force_color=TRUE;
before setLocale(LC_TYPE,"");
Finally hit make
and you're done building tree
for mac.
Tribute goes to Shaun Chapman for his original post on his blog.
Solution 4:
There isn’t a formal tree
command per se however you can do this:
Save the following script to /usr/local/bin/tree
#!/bin/bash
SEDMAGIC='s;[^/]*/;|____;g;s;____|; |;g'
if [ "$#" -gt 0 ] ; then
dirlist="$@"
else
dirlist="."
fi
for x in $dirlist; do
find "$x" -print | sed -e "$SEDMAGIC"
done
Change the permissions so you can run it:
chmod 755 /usr/local/bin/tree
Of course you may have to create /usr/local/bin
:
sudo mkdir -p /usr/local/bin/tree