Linux Command - Tree
The Linux tree command shows the contents of a directory as a tree. This command is really useful and is one of my favorite commands.
You will probably have to install it because it usally isn’t installed by default. Here is the command to install on Debian/Ubuntu:
sudo apt install tree
Really nice example of how useful this command is:
$tree websocket
websocket
├── client.py
├── jssock
│ ├── index.js
│ ├── node_modules
│ │ └── ws
│ │ ├── browser.js
│ │ ├── index.js
│ │ ├── lib
│ │ │ ├── buffer-util.js
│ │ │ ├── constants.js
│ │ │ ├── event-target.js
│ │ │ ├── extension.js
│ │ │ ├── limiter.js
│ │ │ ├── permessage-deflate.js
│ │ │ ├── receiver.js
│ │ │ ├── sender.js
│ │ │ ├── stream.js
│ │ │ ├── subprotocol.js
│ │ │ ├── validation.js
│ │ │ ├── websocket.js
│ │ │ └── websocket-server.js
│ │ ├── LICENSE
│ │ ├── package.json
│ │ ├── README.md
│ │ └── wrapper.mjs
│ ├── package.json
│ └── package-lock.json
└── server.py
4 directories, 24 files
$
From current directory using defaults:
tree
Some versions of this tool may require you to specify “-A” to see nicely drawn characters for the lines.
tree -A
Specify a dir:
tree /etc
Two levels deep:
tree -L 2
Print dirs only:
tree -d
Full paths and no indentation lines:
tree -if
File and dir sizes in human readable format:
tree -s -h --du
Options:
-a | all, including hidden files |
-d | dirs only |
-l | follow dir links ( except when recursion detected ) |
-f | print full path |
-x | stay on current FS |
-L level | max depth level |
-P pattern | only list files that match pattern |
-I pattern | exlcude files that match pattern |
–matchdirs | apply pattern to dir names, used with -P |
–prune | prune empty dirs unless matched with –matchdirs |
-h | print sizes human readable |
-du | show sizes for dirs |
-s | size of each file |
-i | no indentation lines |
-X | xml output |
-J | JSON output |