「Linuxでファイル・ディレクトリ一覧を確認したい」
「Ubuntuでtreeコマンドを使いたい」
このような場合には、この記事の内容が参考になります。
この記事では、Linuxでtreeコマンドを利用する方法を解説しています。
本記事の内容
- treeコマンドとは?
- treeパッケージのインストール
- treeコマンドの動作確認
それでは、上記に沿って解説していきます。
treeコマンドとは?
treeコマンドは、ディレクトリ・ファイル一覧表示コマンドです。
特定のディレクトリ以下を再帰的に表示できます。
例えば、「/proc/self」ディレクトリ以下のディレクトリ一覧を次のように表示できます。

ファイルも表示させる場合は、「-d」オプションを外すだけです。
ディレクトリ・ファイル構成を確認する際には、treeコマンドが大活躍します。
個人的には、treeコマンドはよく使うコマンドになります。
treeコマンドなくして、Linuxの管理はできないぐらいです。
また、配色は環境変数LS_COLORSをもとに設定されています。

あと、treeコマンドは以下の環境に移植されています。
- Linux
- FreeBSD
- OS X
- Solaris
- HP/UX
- Cygwin
- HP Nonstop
- OS/2.
このメンツを見れば、GUIがない場面でtreeコマンドが必要とされていることがわかります。
以上、treeコマンドについて説明しました。
次は、treeパッケージのインストールを説明します。
treeパッケージのインストール
今回は、次のOSにtreeパッケージをインストールします。
$ cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=22.04 DISTRIB_CODENAME=jammy DISTRIB_DESCRIPTION="Ubuntu 22.04 LTS"
デフォルト状態では、treeコマンドは利用できません。
$ tree コマンド 'tree' が見つかりません。次の方法でインストールできます: snap install tree # version 1.8.0+pkg-3fd6, or apt install tree # version 2.0.2-1 他のバージョンについては 'snap info tree' を確認してください。
インストールする前に、treeパッケージの情報を確認しましょう。
$ apt info tree Package: tree Version: 2.0.2-1 Priority: optional Section: universe/utils Origin: Ubuntu Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com> Original-Maintainer: Florian Ernst <florian@debian.org> Bugs: https://bugs.launchpad.net/ubuntu/+filebug Installed-Size: 116 kB Depends: libc6 (>= 2.34) Homepage: http://mama.indstate.edu/users/ice/tree/ Task: xubuntu-desktop, lubuntu-desktop, ubuntu-mate-core, ubuntu-mate-desktop, ubuntu-budgie-desktop, ubuntu-budgie-desktop-raspi Download-Size: 47.9 kB APT-Sources: http://archive.ubuntu.com/ubuntu jammy/universe amd64 Packages Description: displays an indented directory tree, in color Tree is a recursive directory listing command that produces a depth indented listing of files, which is colorized ala dircolors if the LS_COLORS environment variable is set and output is to tty.
これは、Ubuntu 22.04 LTSにおけるOS標準のtreeとなります。
バージョン2.0.2は、現時点(2022年6月)における最新バージョンです。

treeは、結構高頻度で更新されています。
バージョン2系が公開されたのは、2021年12月末となっています。
では、バージョン2.0.2のtreeをインストールします。
インストールは、aptコマンドで一撃です。
$ sudo apt install -y tree
インストールは、すぐに終わります。
インストールできたかどうかは、次のコマンドで確認できます。
$ tree --version tree v2.0.2 (c) 1996 - 2022 by Steve Baker, Thomas Moore, Francesc Rocher, Florian Sesser, Kyosuke Tokoro
上記のようにtreeコマンドのバージョンが表示されれば、インストールは成功です。
以上、treeパッケージのインストールを説明しました。
次は、treeコマンドの動作確認を説明します。
treeコマンドの動作確認
treeコマンドのヘルプは、以下。
$ tree --help
usage: tree [-acdfghilnpqrstuvxACDFJQNSUX] [-L level [-R]] [-H baseHREF]
[-T title] [-o filename] [-P pattern] [-I pattern] [--gitignore]
[--matchdirs] [--metafirst] [--ignore-case] [--nolinks] [--inodes]
[--device] [--sort[=]<name>] [--dirsfirst] [--filesfirst]
[--filelimit #] [--si] [--du] [--prune] [--charset X]
[--timefmt[=]format] [--fromfile] [--noreport] [--version] [--help]
[--] [directory ...]
------- Listing options -------
-a All files are listed.
-d List directories only.
-l Follow symbolic links like directories.
-f Print the full path prefix for each file.
-x Stay on current filesystem only.
-L level Descend only level directories deep.
-R Rerun tree when max dir level reached.
-P pattern List only those files that match the pattern given.
-I pattern Do not list files that match the given pattern.
--gitignore Filter by using .gitignore files.
--ignore-case Ignore case when pattern matching.
--matchdirs Include directory names in -P pattern matching.
--metafirst Print meta-data at the beginning of each line.
--info Print information about files found in .info files.
--noreport Turn off file/directory count at end of tree listing.
--charset X Use charset X for terminal/HTML and indentation line output.
--filelimit # Do not descend dirs with more than # files in them.
-o filename Output to file instead of stdout.
------- File options -------
-q Print non-printable characters as '?'.
-N Print non-printable characters as is.
-Q Quote filenames with double quotes.
-p Print the protections for each file.
-u Displays file owner or UID number.
-g Displays file group owner or GID number.
-s Print the size in bytes of each file.
-h Print the size in a more human readable way.
--si Like -h, but use in SI units (powers of 1000).
-D Print the date of last modification or (-c) status change.
--timefmt <f> Print and format time according to the format <f>.
-F Appends '/', '=', '*', '@', '|' or '>' as per ls -F.
--inodes Print inode number of each file.
--device Print device ID number to which each file belongs.
------- Sorting options -------
-v Sort files alphanumerically by version.
-t Sort files by last modification time.
-c Sort files by last status change time.
-U Leave files unsorted.
-r Reverse the order of the sort.
--dirsfirst List directories before files (-U disables).
--filesfirst List files before directories (-U disables).
--sort X Select sort: name,version,size,mtime,ctime.
------- Graphics options -------
-i Don't print indentation lines.
-A Print ANSI lines graphic indentation lines.
-S Print with CP437 (console) graphics indentation lines.
-n Turn colorization off always (-C overrides).
-C Turn colorization on always.
------- XML/HTML/JSON options -------
-X Prints out an XML representation of the tree.
-J Prints out an JSON representation of the tree.
-H baseHREF Prints out HTML format with baseHREF as top directory.
-T string Replace the default HTML title and H1 header with string.
--nolinks Turn off hyperlinks in HTML output.
------- Input options -------
--fromfile Reads paths from files (.=stdin)
------- Miscellaneous options -------
--version Print version and exit.
--help Print usage and this help message and exit.
-- Options processing terminator.
個人的には、「-L」オプションをよく使います。
「-L」オプションにより、ディレクトリの深さを指定できます。
1つ下の階層のディレクトリまでを表示する場合
$ tree -d -L 1 /proc/self/ /proc/self/ ├── attr ├── cwd -> /tmp ├── fd ├── fdinfo ├── map_files ├── net ├── ns ├── root -> / └── task 9 directories
2つ下の階層のディレクトリまでを表示する場合
$ tree -d -L 2 /proc/self/
/proc/self/
├── attr
│ ├── apparmor
│ └── smack
├── cwd -> /tmp
├── fd
│ └── 3 -> /proc/17999/fd
├── fdinfo
├── map_files
├── net
│ ├── dev_snmp6
│ ├── netfilter
│ └── stat
├── ns
├── root -> /
└── task
└── 17999
16 directories
あと、「-a」オプションも利用します。
隠しディレクトリ・ファイルを表示する際に「-a」オプションを用います。
隠しディレクトリ・ファイルを無視する場合は、以下の結果となります。
$ tree -L 1 . ├── check.py └── test.py
隠しディレクトリ・ファイルを表示する場合は、以下の結果となります。
$ tree -a -L 1 . ├── .bash_history ├── .bash_logout ├── .bashrc ├── .cache ├── .keras ├── .local ├── .profile ├── .ssh ├── .wget-hsts ├── check.py └── test.py
以上、treeコマンドの動作確認を説明しました。

