フォルダ構造をツリー形式で表示してくれる、「tree」コマンドの紹介をします。
treeでディレクトリ構成を表示
「tree」コマンドを実行すると、フォルダの構成を視覚的にわかりやすく表示してくれるので、ドキュメントを作成する時などに役立ちますよ。
まずは「apt」で「tree」をインストールします。
$ sudo apt update $ sudo apt install tree
使い方はとても簡単で、「tree」コマンドを下記のように実行するだけです。
これで、カレントディレクトリ(現在いるディレクトリ)内の構造を下記のように表示してくれます。
一番上の「.」は、現在のディレクトリを示しています。
$ tree . ├── data │ └── index.html ├── images │ ├── 2025 │ │ ├── 01 │ │ │ └── 20250101.jpg │ │ └── 02 │ └── image_01.png └── test_01.txt 6 directories, 4 files
ディレクトリを指定して「tree」コマンドを実行することも出来ます。
こちらの方が、より何処のディレクトリの中身を表示しているかが分りやすいですね。
$ tree /home/tamohiko/work/ /home/tamohiko/work/ ├── data │ └── index.html ├── images │ ├── 2025 │ │ ├── 01 │ │ │ └── 20250101.jpg │ │ └── 02 │ └── image_01.png └── test_01.txt 6 directories, 4 files
オプションの使い方
普段使いそうなオプションの説明をします。
- -d ディレクトリのみ表示
- -f フルパスで表示
- -o ファイル 表示結果をファイルへ出力
- -L 表示階層数 表示する階層の指定
- -X XMLアウトプット
- -J JSONアウトプット
-d ディレクトリのみ表示
「-d」オプションを使用すると、ディレクトリのみを表示してくれます。
$ tree -d /home/tamohiko/work /home/tamohiko/work ├── data └── images └── 2025 ├── 01 └── 02 6 directories
-f フルパスで表示
「-f」オプションを使用すると、フルパスで構造を表示してくれます。
$ tree -f /home/tamohiko/work /home/tamohiko/work ├── /home/tamohiko/work/data │ └── /home/tamohiko/work/data/index.html ├── /home/tamohiko/work/images │ ├── /home/tamohiko/work/images/2025 │ │ ├── /home/tamohiko/work/images/2025/01 │ │ │ └── /home/tamohiko/work/images/2025/01/20250101.jpg │ │ └── /home/tamohiko/work/images/2025/02 │ └── /home/tamohiko/work/images/image_01.png └── /home/tamohiko/work/test_01.txt 6 directories, 4 files
-o 出力先ファイル 表示結果をファイルへ出力
「-o 出力先ファイル」と指定すると出力結果をファイルに出力してくれます。
$ tree -o work-tree.txt /home/tamohiko/work
出力先として指定したファイルを「cat」コマンドで確認すると、「tree」の表示結果が記録されていることが確認できます。
$ cat work-tree.txt /home/tamohiko/work ├── data │ └── index.html ├── images │ ├── 2025 │ │ ├── 01 │ │ │ └── 20250101.jpg │ │ └── 02 │ └── image_01.png └── test_01.txt 6 directories, 4 files
-L 表示階層数 表示する階層の指定
「-L 表示階層数」オプションをすると、表示するディレクトリの階層数を指定することが出来ます。
こちらが「-L 表示階層数」オプションを指定しない場合の表示です。
$ tree /home/tamohiko/work /home/tamohiko/work ├── data │ └── index.html ├── images │ ├── 2025 │ │ ├── 01 │ │ │ └── 20250101.jpg │ │ └── 02 │ └── image_01.png └── test_01.txt 6 directories, 4 files
「-L 1」と指定して1階層目までの表示と指定すると、以下のように2階層目以降は表示され無くなりました。
$ tree -L 1 /home/tamohiko/work /home/tamohiko/work ├── data ├── images └── test_01.txt 3 directories, 1 file
-X XML形式で表示
「-X」オプションを使用すると、下記のように「XML」形式で表示することが出来ます。
$ tree -X /home/tamohiko/work <?xml version="1.0" encoding="UTF-8"?> <tree> <directory name="/home/tamohiko/work"> <directory name="data"> <file name="index.html"></file> </directory> <directory name="images"> <directory name="2025"> <directory name="01"> <file name="20250101.jpg"></file> </directory> <directory name="02"></directory> </directory> <file name="image_01.png"></file> </directory> <file name="test_01.txt"></file> </directory> <report> <directories>6</directories> <files>4</files> </report> </tree>
-J JSON形式で表示
「-J」オプションを使用すると、下記のように「JSON」形式で表示することが出来ます。
$ tree -J /home/tamohiko/work [ {"type":"directory","name":"/home/tamohiko/work","contents":[ {"type":"directory","name":"data","contents":[ {"type":"file","name":"index.html"} ]}, {"type":"directory","name":"images","contents":[ {"type":"directory","name":"2025","contents":[ {"type":"directory","name":"01","contents":[ {"type":"file","name":"20250101.jpg"} ]}, {"type":"directory","name":"02"} ]}, {"type":"file","name":"image_01.png"} ]}, {"type":"file","name":"test_01.txt"} ]} , {"type":"report","directories":6,"files":4} ]
コメント