Installing Tree in Linux via Command Line

Posted on

This article will used to describe steps on installing ‘tree’. It is one of the among the others defined as useful tools or command in Linux operating system variant. The function of this tool is to list contents of directories in a tree-like format, just like the manual of ‘tree’ command stated. Just type the following command to install the tool :

apt install tree

This is the pattern of the command execution :

apt install package_name

Description : 
apt : It is a tool which is executed as a command-line interface for package management system. 
install : It is one of the operative available and provided in using apt to install new package or tool 
package_name : It is the value which is defined the name of package or tool to be installed.  

Pay attention to the description above that ‘apt’ itself is a package manager tool used for Debian Linux operating system variant including Ubuntu. Below is the execution of the above command in a real example :

root@hostname:~# apt install tree
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
tree
0 upgraded, 1 newly installed, 0 to remove and 542 not upgraded.
1 not fully installed or removed.
Need to get 40,6 kB of archives.
After this operation, 138 kB of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu xenial/universe amd64 tree amd64 1.7.0-3 [40,6 kB]
Fetched 40,6 kB in 0s (40,8 kB/s)
Selecting previously unselected package tree.
(Reading database ... 829505 files and directories currently installed.)
Preparing to unpack .../tree_1.7.0-3_amd64.deb ...
Unpacking tree (1.7.0-3) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up redmine (3.2.1-2) ...
Determining localhost credentials from /etc/mysql/debian.cnf: succeeded.
dbconfig-common: writing config to /etc/dbconfig-common/redmine/instances/default.conf
granting access to database redmine_default for redmine/instance@localhost: already exists.
creating database redmine_default: already exists.
dbconfig-common: flushing administrative password
rake aborted!
ActiveRecord::AdapterNotSpecified: 'development' database is not configured. Available: ["production"]
/var/lib/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/connection_specification.rb:248:in `resolve_symbol_connection'
/var/lib/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/connection_specification.rb:211:in `resolve_connection'
/var/lib/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/connection_specification.rb:139:in `resolve'
/var/lib/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/connection_specification.rb:169:in `spec'
/var/lib/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_handling.rb:50:in `establish_connection'
/var/lib/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/railtie.rb:120:in `block (2 levels) in <class:Railtie>'
/var/lib/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:38:in `instance_eval'
/var/lib/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:38:in `execute_hook'
/var/lib/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
/var/lib/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:44:in `each'
/var/lib/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
/var/lib/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/base.rb:315:in `<module:ActiveRecord>'
/var/lib/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/base.rb:26:in `<top (required)>'
/var/lib/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `require'
/var/lib/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `block in require'
/var/lib/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:240:in `load_dependency'
/var/lib/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `require'
/usr/share/redmine/lib/plugins/acts_as_activity_provider/init.rb:2:in `block in <top (required)>'
/usr/share/redmine/config/initializers/00-core_plugins.rb:12:in `eval'
/usr/share/redmine/config/initializers/00-core_plugins.rb:12:in `block in <top (required)>'
/usr/share/redmine/config/initializers/00-core_plugins.rb:2:in `each'
/usr/share/redmine/config/initializers/00-core_plugins.rb:2:in `<top (required)>'
/var/lib/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'
/var/lib/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `block in load'
/var/lib/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:240:in `load_dependency'
/var/lib/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'
/var/lib/gems/2.3.0/gems/railties-4.2.6/lib/rails/engine.rb:652:in `block in load_config_initializer'
/var/lib/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/notifications.rb:166:in `instrument'
/var/lib/gems/2.3.0/gems/railties-4.2.6/lib/rails/engine.rb:651:in `load_config_initializer'
/var/lib/gems/2.3.0/gems/railties-4.2.6/lib/rails/engine.rb:616:in `block (2 levels) in <class:Engine>'
/var/lib/gems/2.3.0/gems/railties-4.2.6/lib/rails/engine.rb:615:in `each'
/var/lib/gems/2.3.0/gems/railties-4.2.6/lib/rails/engine.rb:615:in `block in <class:Engine>'
/var/lib/gems/2.3.0/gems/railties-4.2.6/lib/rails/initializable.rb:30:in `instance_exec'
/var/lib/gems/2.3.0/gems/railties-4.2.6/lib/rails/initializable.rb:30:in `run'
/var/lib/gems/2.3.0/gems/railties-4.2.6/lib/rails/initializable.rb:55:in `block in run_initializers'
/var/lib/gems/2.3.0/gems/railties-4.2.6/lib/rails/initializable.rb:44:in `each'
/var/lib/gems/2.3.0/gems/railties-4.2.6/lib/rails/initializable.rb:44:in `tsort_each_child'
/var/lib/gems/2.3.0/gems/railties-4.2.6/lib/rails/initializable.rb:54:in `run_initializers'
/var/lib/gems/2.3.0/gems/railties-4.2.6/lib/rails/application.rb:352:in `initialize!'
/usr/share/redmine/config/environment.rb:14:in `<top (required)>'
/var/lib/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `require'
/var/lib/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `block in require'
/var/lib/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:240:in `load_dependency'
/var/lib/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `require'
/var/lib/gems/2.3.0/gems/railties-4.2.6/lib/rails/application.rb:328:in `require_environment!'
/var/lib/gems/2.3.0/gems/railties-4.2.6/lib/rails/application.rb:457:in `block in run_tasks_blocks'
/var/lib/gems/2.3.0/gems/rake-11.2.2/exe/rake:27:in `<top (required)>'
/var/lib/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:63:in `load'
/var/lib/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:63:in `kernel_load'
/var/lib/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli/exec.rb:24:in `run'
/var/lib/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli.rb:304:in `exec'
/var/lib/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/var/lib/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/var/lib/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/vendor/thor/lib/thor.rb:359:in `dispatch'
/var/lib/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/vendor/thor/lib/thor/base.rb:440:in `start'
/var/lib/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/cli.rb:11:in `start'
/var/lib/gems/2.3.0/gems/bundler-1.12.5/exe/bundle:27:in `block in <top (required)>'
/var/lib/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/friendly_errors.rb:98:in `with_friendly_errors'
/var/lib/gems/2.3.0/gems/bundler-1.12.5/exe/bundle:19:in `<top (required)>'
/usr/local/bin/bundle:23:in `load'
/usr/local/bin/bundle:23:in `<main>'
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)
dpkg: error processing package redmine (--configure):
subprocess installed post-installation script returned error exit status 1
Setting up tree (1.7.0-3) ...
Errors were encountered while processing:
redmine
E: Sub-process /usr/bin/dpkg returned an error code (1)
root@hostname:~#

There are some errors which is not related to the ‘tree’ installation so ignored it. The above output will not be similar with the one executed in each host, workstation or server used for the installation, it depends on the condition of the machine itself. To prove that ‘tree’ has already been installed, an easy way to do it is by typing the command in the bash prompt or any command line available  in the directory where the contents is going to be viewed :

user@hostname:~/test-folder$ tree
.
├── file-02102016
├── file-03102016
├── file-04102016
├── file-05102016
├── file-06102016
├── file-07102016
├── file-08102016
├── file-09102016
├── file-10102016
├── file-11102016
├── file-12102016
├── file-13102016
├── file-14102016
├── file-15102016
├── file-16102016
├── file-17102016
├── file-18102016
├── file-19092016
├── file-20092016
├── file-21092016
├── file-22092016
├── file-23092016
├── file-25092016
├── file-26092016
├── file-27092016
├── file-28092016
├── file-29092016
├── file-30092016
├── folder-1
│   └── test-file-1-folder-1
├── folder-2
├── my-bash-script
├── test-file
├── test-file-1
├── test-file-2
└── test-file-3

2 directories, 34 files
user@hostname:~/test-folder$ cd ..
user@hostname:~$

The above is a folder contains several files which is almost all them generated through Linux’s cron scheduler.

If the command doesn’t followed with any other parameters or attribute, it will display the contents of the current working directory to its lowest level. To check the full path of current working directory, it can be done by executing command ‘pwd’ or read this article titled ‘Check current working directory in Linux using pwd command‘ for details.

Below are another example using a directory which is actually a laravel directory project.

Displaying only directory with is specified with -d parameter for only the first level depth marked with -L 1 parameter. In other words, display all the first level directories in the current working directory which is in /var/www/html/laravel-testing. The execution is shown below :

user@hostname:/var/www/html/laravel-testing# tree -d -L 1
.
├── app
├── bootstrap
├── config
├── database
├── logs
├── public
├── resources
├── routes
├── storage
├── tests
└── vendor

11 directories
user@hostname:/var/www/html/laravel-testing#

Display all contents whether those are files or directory until the second level depth from the current working directory which is in /var/www/html/laravel-testing. The execution shown below :

user@hostname:/var/www/html/laravel-testing# tree -L 2
.
├── app
│   ├── Console
│   ├── Exceptions
│   ├── Http
│   ├── Providers
│   └── User.php
├── artisan
├── bootstrap
│   ├── app.php
│   ├── autoload.php
│   └── cache
├── composer.json
├── composer.lock
├── config
│   ├── app.php
│   ├── auth.php
│   ├── broadcasting.php
│   ├── cache.php
│   ├── compile.php
│   ├── database.php
│   ├── filesystems.php
│   ├── mail.php
│   ├── queue.php
│   ├── services.php
│   ├── session.php
│   └── view.php
├── database
│   ├── factories
│   ├── migrations
│   └── seeds
├── gulpfile.js
├── logs
│   ├── localhost.testing.web-access_log
│   └── localhost.testing.web-error_log
├── package.json
├── phpunit.xml
├── public
│   ├── css
│   ├── favicon.ico
│   ├── index.php
│   ├── js
│   ├── robots.txt
│   └── web.config
├── readme.md
├── resources
│   ├── assets
│   ├── lang
│   └── views
├── routes
│   ├── api.php
│   ├── console.php
│   └── web.php
├── server.php
├── storage
│   ├── app
│   ├── framework
│   └── logs
├── tests
│   ├── ExampleTest.php
│   └── TestCase.php
└── vendor
├── autoload.php
├── bin
├── classpreloader
├── composer
├── dnoegel
├── doctrine
├── fzaninotto
├── hamcrest
├── jakub-onderka
├── jeremeamia
├── laravel
├── league
├── mockery
├── monolog
├── mtdowling
├── myclabs
├── nesbot
├── nikic
├── paragonie
├── phpdocumentor
├── phpspec
├── phpunit
├── psr
├── psy
├── ramsey
├── sebastian
├── swiftmailer
├── symfony
├── vlucas
└── webmozart

56 directories, 35 files
user@hostname:/var/www/html/laravel-testing#

This is another example which is shown all the contents within /var/www/html/laravel-testing/app/Http :

user@hostname:/var/www/html/laravel-testing/app/Http# tree
.
├── Controllers
│ ├── Auth
│ │ ├── ForgotPasswordController.php
│ │ ├── LoginController.php
│ │ ├── RegisterController.php
│ │ └── ResetPasswordController.php
│ ├── CheckLaravelVersion.php
│ ├── Controller.php
├── Kernel.php
└── Middleware
├── EncryptCookies.php
├── RedirectIfAuthenticated.php
└── VerifyCsrfToken.php
3 directories, 12 files
user@hostname:/var/www/html/laravel-testing/app/Http#

The path or location can also relatively specified by mentioning it after the command. Below is an example of command using tree which is used to display the content in routes/ folder which is located in /var/www/html/laravel-testing. It is specified only to display the first level depth from routes folder :

user@hostname:/var/www/html/laravel-testing# tree routes/ -L 1
routes/
├── api.php
├── console.php
└── web.php

0 directories, 3 files
You have new mail in /var/mail/root
user@hostname:/var/www/html/laravel-testing#

Leave a Reply