プログラミングのブログ記事を書いていると、ファイル構造を紹介する際にtreeコマンドを良く使います。
しかしwsl2にインストールしたUbuntuには初期状態でtreeコマンドを打ってもエラーが出て使えませんので、使える様にする方法を今回の記事で紹介します。
目次
treeのインストール
treeが無い時のエラー
$ tree
Command 'tree' not found, but can be installed with:
sudo apt install tree
Ubuntuでtreeコマンドを叩いても初期状態ではインストールされていないので使えません。
treeインストール
sudo apt install tree
上記のコマンドを叩いてインストールしましょう。
インストール完了
$ sudo apt install tree
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
tree
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 47.9 kB of archives.
After this operation, 116 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu jammy/universe amd64 tree amd64 2.0.2-1 [47.9 kB]
Fetched 47.9 kB in 1s (35.2 kB/s)
Selecting previously unselected package tree.
(Reading database ... 24256 files and directories currently installed.)
Preparing to unpack .../tree_2.0.2-1_amd64.deb ...
Unpacking tree (2.0.2-1) ...
Setting up tree (2.0.2-1) ...
Processing triggers for man-db (2.10.2-1) ...
treeコマンド確認
インストールが無事終了しましたら、treeコマンドを叩いてディレクトリやファイル構造が出力される確認してみましょう。
treeコマンド
tree
出力例
$ tree
.
├── Dockerfile
├── app
│ └── hello.py
├── docker-compose.yml
└── requirements.txt
1 directory, 4 files
treeのアンインストール
もしアンインストールしたい場合は以下のコマンドで削除が出来ます。
treeコマンド
sudo apt remove tree
削除例
$ sudo apt remove tree
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
tree
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 116 kB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 24264 files and directories currently installed.)
Removing tree (2.0.2-1) ...
Processing triggers for man-db (2.10.2-1) ...
user@pc:~$ tree
-bash: /usr/bin/tree: No such file or directory
あとがき

ブログなどで解説記事を書いている人などには欠かせないtreeコマンドですが、使わない人は一生使わないコマンドかもしれません。
私はブログの記事で紹介する時に初めて存在を知り、使い始めました。
一度使ってみると手放せない。プログラミングの記事や備忘録を残す時には必須と言っても過言ではないコマンドですね。
コメント