Using tree command to show the tree structure in MAC system

Time:2019-12-8

In Linux, you can easily use the tree command to view the directory tree mechanism in the current directory. But in MAC, you need to install at least one tool

At first, I found a way to print tree structure with shell command from the Internet

find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'

You can directly enter the above shell command under your terminal to view the structure tree of the current directory

How to turn it into a command and use it directly

We came up with a way for alias to specify aliases

alias tree="find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"

If you use Bash

Open bash configuration file, copy and paste the above command

➜  /Users/zhangzhi  >nano .bash_profile

Put at the bottom of the profile

Source. Bash? Profile let the configuration file take effect immediately

If you use Zsh

Open the Zsh configuration file, copy and paste the above command

➜  /Users/zhangzhi  >nano .zshrc

Source. Zshrc let the configuration file take effect immediately

After the installation is successful, how to use it? After entering your target directory, enter the tree command directly. Because there are too many directories, one screen can’t be displayed, so a part of it is intercepted, as follows: