If you have been working in the mac environment or Linux system, Then It’s very essential to know about different command lines. Tree command is very common in the technical world. And It’s very easy to learn. Apart from mac, It also works with Windows, Linux, and some custom OS. The main task of it; is to list things, In many cases, list directories. Although developers use it for more than that. If you ever felt that feel management in the command line is messy, Then you’ll thoroughly love this program. Because It makes the life of a developer very easy.
What is the tree on mac
Before we dive into the article, Let’s get our mind to wrap around the contextual idea of tree command. In a nutshell, Tree is just another command line. Although I must say a neat and decluttered one. But if we understand a little deeper, It works as a system that lists all the content of directories, In an order. That looks similar to that of the tree. Not like the geometric shape, But the idea of branching into multiple fields. Like how a single branch has many sub-branch with multiple leaves. This is a metaphoric explanation of how it works.
You can use this to do a lot of things, For i.e, if you are working with a react application, And you want to see all the files under the following folder. Then you can use the Tree, Not just look for files, But also navigate through them. It’s similar to what we use to do with the native command line of mac. But here the process is a little simple and tidy. A good sum of developers is moving towards the tree. Because It’s much more efficient than the default command line. You must also realize, The work environment will still be done in the same command line.
How to install the tree on mac
Now that we know the amazing benefits of Tree commands, Let’s not make any delay for installing it. So Installing the Tree can be done in three methods. And I’m gonna share with you all three of them.
The easiest way to install any program is by using homebrew. It’s such a liberating software for developers. But In case you don’t have one, Then you can learn to install homebrew. It’s very simple and powerful tool. For any reason that you are unable to install the software, And want to install tree by others means. Then stick to the end of where I’have shares the other two ways to install It. But if you have one or installed one. Then follow this simple task. Just add the following command in your homebrew, Once done, you would be good to go.
brew install tree
2) Installing using the package manager
MacOS is blessed with great package mangers. Some of the big-name include; MacPorts, Frink, and Homebrew. Any of them will work just fine. But If you won’t take my two cents, Then I’ll suggest you choose Homebrew. Avoid installing all three, Just go with only one. Okay, Now that you have decided which package manager you want to install, follow the instruction below according o what you have chosen. Enter commands according to your package manager.
brew install tree
sudo port install tree
fink install tree
Note: These Package mangers have many other command lines software. This includes Graphical and Linux also, In most cases they are available on macOS as default.
3) Installing from source
Great, I’m pretty sure most of the people would have installed Tree successful. But If there are some, Who has bad luck. Do not worry because this method will work you 100%; Because It’s the most reliable way of installing third-party programs.
1) Install the Xcode command line tools; Run the following command Xcode-select –install.
2) Download the Tree source from the following website.
3) Change Makefile settling, Comment the Linux option, and uncomment the macOS option.
4) Run ./configure, then make
5) Move the Tree binary to an execution path.
6) Edit your bash profile bash_profile
7) Include in your profile: export PATH=”/usr/local/bin:$PATH”
8) Reload, And you have successfully installed the tree.
How to use It
There are many ways to use the Tree to your benefit. But for many developers and normal users also, They use it from some quite a few tasks. I’ll share with you the most used things on Tree. You can list the first directory level with a lot of alphanumeric files. If your output setting is set to UTF-8 then It is the default encoding of file output. To achieve the first list directory is very easy, You just need to run the following command on your terminal: tree -v -L 1, And for additional or multiple arguments -o backup.txt you can save all the tree output in just a file. Saving the output is also a piece of cake; hello.txt, Instead of whats you save -0 hello.txt.
There could be many cases in which you are required to save your files frequently. And if you have a certain task. It could get quite messy at time. So if you are backing up your file on a daily, weekly, and monthly basis then making a good scheduling approach would be a great help. A very good method that many developers, And people managing servers onsite uses is to combine date and time with the file for output. I have shared with you a weekly output file, That uses the same method as I have described earlier in the blog.
tree -v -L 1 --charset utf-8 > backup-foldername-$(date +%Y-%m-%d).txt.
This was a very simple and easy tutorial about Installing Tree. And using some basic command for performing different tasks. This is just the tips of the iceberg. You can do many more things with this program. The only thing that you have to do is; visit the help command in Tree. To see whats all possible.