はじめに
tree
コマンドを使用して、ディレクトリ構造をツリー表示したことがあり、今後も使うかもなーということで備忘録としてメモしておく。
環境
MacOS sonoma 14.2.1
Homebrew 4.2.4
treeコマンドのインストール
brew install tree
brew install tree
==> Downloading https://formulae.brew.sh/api/formula.jws.json
###################################################################################################################################### 100.0%
==> Downloading https://formulae.brew.sh/api/cask.jws.json
###################################################################################################################################### 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/tree/manifests/2.1.1_1
###################################################################################################################################### 100.0%
==> Fetching tree
==> Downloading https://ghcr.io/v2/homebrew/core/tree/blobs/sha256:af3d14eb91e4bf756bb5ef5f6a489aeb33e6cf5fc4f72c99d70352bec364e282
###################################################################################################################################### 100.0%
==> Pouring tree--2.1.1_1.sonoma.bottle.tar.gz
🍺 /usr/local/Cellar/tree/2.1.1_1: 8 files, 160.2KB
==> Running `brew cleanup tree`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
使ってみる
まずは --help
を読んでおこう。
tree --help
usage: tree [-acdfghilnpqrstuvxACDFJQNSUX] [-L level [-R]] [-H baseHREF]
[-T title] [-o filename] [-P pattern] [-I pattern] [--gitignore]
[--gitfile[=]file] [--matchdirs] [--metafirst] [--ignore-case]
[--nolinks] [--hintro[=]file] [--houtro[=]file] [--inodes] [--device]
[--sort[=]<name>] [--dirsfirst] [--filesfirst] [--filelimit #] [--si]
[--du] [--prune] [--charset[=]X] [--timefmt[=]format] [--fromfile]
[--fromtabfile] [--fflinks] [--info] [--infofile[=]file] [--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.
--gitfile X Explicitly read gitignore file.
--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.
--prune Prune empty directories from the output.
--info Print information about files found in .info files.
--infofile X Explicitly read info file.
--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).
--du Compute size of directories by their contents.
-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.
--hintro X Use file X as the HTML intro.
--houtro X Use file X as the HTML outro.
------- Input options -------
--fromfile Reads paths from files (.=stdin)
--fromtabfile Reads trees from tab indented files (.=stdin)
--fflinks Process link information when using --fromfile.
------- Miscellaneous options -------
--version Print version and exit.
--help Print usage and this help message and exit.
-- Options processing terminator.
一番単純にツリー構造の表示をさせてみる。
tree
例えば、このブログの記事ファイルが格納されている content
ディレクトリ内のツリー構造を表示してみる。
tree
.
├── about.md
└── post
├── column
│ ├── assigned-books
│ │ ├── book-2024-01.md
│ │ └── book-2024-02.md
│ ├── buy-benq-mobiuz-ex2510s.md
│ ├── introduction.md
│ ├── pass-bookkeeping3.md
│ ├── pass-db.md
│ ├── pass-fp2.md
│ ├── pass-fp3.md
│ ├── pass-nw.md
│ ├── recentry-2020-01-2w.md
│ ├── recentry-2020-01-3w.md
│ ├── recentry-2020-01-4w.md
│ ├── recentry-2020-02-1w.md
│ ├── recentry-2020-02-2w.md
│ ├── recentry-2020-02-3w.md
│ ├── recentry-2020-02-4w.md
│ ├── recentry-2020-02-5w.md
│ ├── recentry-2020-03-1w.md
│ ├── recentry-2020-03-2w.md
│ ├── recentry-2020-03-3w.md
│ ├── recentry-2020-03-4w.md
│ ├── recentry-2020-04-1w.md
│ ├── recentry-2020-04-4w.md
│ ├── recentry-2022-07-4w.md
│ ├── resolution-2023y.md
│ ├── resolution-2024y.md
│ └── review-2023y.md
├── study
│ └── university
│ └── senior-thesis.md
└── tech
├── ai
│ ├── ai-kiritan-play.md
│ ├── renew-article.md
│ └── use-chatgpt-api.md
├── cdn
│ └── failed-to-find-a-valid-digest.md
├── cloud
│ ├── aws-backup-success-job-notifications.md
│ ├── building-gitlab-on-ec2.md
│ ├── monitoring-server-with-route53-healthcheck.md
│ ├── send-mail-ec2-with-postfix-use-aws-ses.md
│ └── use-elemental-media-convert.md
├── container
│ ├── docker-autoheal.md
│ ├── docker-lamp.md
│ └── ssh-2fa-for-docker.md
├── database
│ ├── client-character-set.md
│ ├── compare-tables-with-checksum-statement.md
│ ├── mysql-case-sensitive.md
│ ├── mysql-create-history-table-with-trigger.md
│ ├── mysql-replication.md
│ ├── use-mysql-tuner.md
│ └── use-sql-with.md
├── environment
│ ├── construct-eol-apache-tomcat.md
│ ├── construct-gitlab-ce-docker.md
│ ├── construct-gitlab-runner-docker.md
│ ├── construct-hugo-docker-2.md
│ ├── construct-hugo-docker.md
│ ├── docker-init.md
│ ├── gitlab-mr-review-with-chatgpt-2.md
│ ├── gitlab-mr-review-with-chatgpt.md
│ ├── upgrade-gitlab-15-to-16.md
│ ├── vscode-nuxt.md
│ └── vscode-php.md
├── game
│ ├── play-2048.md
│ ├── valheim-migrate-from-local-to-ec2-server.md
│ └── valheim-server-ec2-arm-instance.md
├── infrastructure
│ ├── construct-apache-tomcat-mysql-to-amazonlinux2023-with-ansible.md
│ ├── construct-ec2-public-private-subnet-alb.md
│ ├── construct-ec2-simple-architecture-with-terraform.md
│ ├── construct-gitlab-ce-with-ansible.md
│ ├── deploy-portfolio-to-vercel.md
│ ├── emulate-raspbian-os-with-qemu.md
│ ├── hugo-theme-update-2.md
│ ├── hugo-theme-update.md
│ ├── introduction-google-analytics-for-hugo.md
│ ├── migrate-from-route53-to-cloudflare.md
│ ├── migrate-from-s3-to-wasabi.md
│ ├── use-ansible-the-first-time.md
│ └── use-cloudflare-ddns.md
├── migrate
│ └── vue2-to-vue3.md
├── programming
│ ├── golang
│ │ ├── build-web-application-with-golang-1.md
│ │ ├── build-web-application-with-golang-10.md
│ │ ├── build-web-application-with-golang-11.md
│ │ ├── build-web-application-with-golang-12.md
│ │ ├── build-web-application-with-golang-13.md
│ │ ├── build-web-application-with-golang-14.md
│ │ ├── build-web-application-with-golang-15.md
│ │ ├── build-web-application-with-golang-16.md
│ │ ├── build-web-application-with-golang-17.md
│ │ ├── build-web-application-with-golang-18.md
│ │ ├── build-web-application-with-golang-19.md
│ │ ├── build-web-application-with-golang-2.md
│ │ ├── build-web-application-with-golang-20.md
│ │ ├── build-web-application-with-golang-21.md
│ │ ├── build-web-application-with-golang-22.md
│ │ ├── build-web-application-with-golang-23.md
│ │ ├── build-web-application-with-golang-24.md
│ │ ├── build-web-application-with-golang-25.md
│ │ ├── build-web-application-with-golang-26.md
│ │ ├── build-web-application-with-golang-27.md
│ │ ├── build-web-application-with-golang-28.md
│ │ ├── build-web-application-with-golang-29.md
│ │ ├── build-web-application-with-golang-3.md
│ │ ├── build-web-application-with-golang-4.md
│ │ ├── build-web-application-with-golang-5.md
│ │ ├── build-web-application-with-golang-6.md
│ │ ├── build-web-application-with-golang-7.md
│ │ ├── build-web-application-with-golang-8.md
│ │ ├── build-web-application-with-golang-9.md
│ │ ├── go-competitive-programming-code-01.md
│ │ └── go-competitive-programming-code-02.md
│ ├── javascript
│ │ └── vue
│ │ └── refactor-vue3-composition-api.md
│ ├── php
│ │ ├── convert-html-to-pdf-with-headless-chrome.md
│ │ ├── implement-caesar-cipher.md
│ │ └── implement-substitution-cipher.md
│ └── python
│ └── eol-notify-python.md
├── scm
│ └── using-cvs2git.md
├── summarize
│ └── web-develop-watch-list.md
├── tips
│ ├── apache-tomcat-configuration-to-let-apache-handle-static-resources.md
│ ├── convert-excel-to-pdf.md
│ ├── convert-heic-to-png-with-imagemagick.md
│ ├── convert-html-to-pdf.md
│ ├── create-certificates-with-san.md
│ ├── diff-two-files-with-vscode.md
│ ├── ec2-attach-volume.md
│ ├── ec2-eni-take-over-ip.md
│ ├── fix-code-quote-shaping.md
│ ├── git-attention.md
│ ├── git-mv.md
│ ├── install-gitlab-update-script-amzn-2023-to-2023.md
│ ├── install-lightbox2-to-hugo-blog.md
│ ├── list-files-modified-after-the-specify-datetime.md
│ ├── lost-aws-ec2-keypair.md
│ ├── mount-wasabi-bucket-with-aws-cli.md
│ ├── nginx-before-tls-1_1-disable.md
│ ├── reduce-volume-ebs.md
│ ├── remove-exif.md
│ ├── replace-html-code-to-shorcode.md
│ ├── rescue-ec2-instance.md
│ ├── update-hugo-version-from-0.110-to-0.111.md
│ ├── update-hugo-version-from-0.111.3-to-0.122.0.md
│ ├── use-aamath.md
│ ├── use-ansiweather.md
│ ├── use-apg.md
│ ├── use-apvlv.md
│ ├── use-barcode.md
│ ├── use-bash-shortcut.md
│ ├── use-busybox.md
│ ├── use-calc.md
│ ├── use-cbm.md
│ ├── use-circumflex.md
│ ├── use-codequery.md
│ ├── use-colordiff.md
│ ├── use-countdown.md
│ ├── use-csview.md
│ ├── use-csvtomd.md
│ ├── use-exiftool.md
│ ├── use-fcrackzip.md
│ ├── use-figure-code-jane.md
│ ├── use-flock.md
│ ├── use-github-connect-with-ssh-over-https.md
│ ├── use-htop.md
│ ├── use-jq.md
│ ├── use-logrotate.md
│ ├── use-markdown-vscode-extension.md
│ ├── use-md5-sum.md
│ ├── use-ntpdate.md
│ ├── use-nyancat.md
│ ├── use-pdfcrack.md
│ ├── use-pdfseparate.md
│ ├── use-pdftocairo.md
│ ├── use-pdfunite.md
│ ├── use-qrencode.md
│ ├── use-rlogin-portforward.md
│ ├── use-shortcode-hugo.md
│ ├── use-sl.md
│ ├── use-slack-notifications-gitlab.md
│ ├── use-tesseract.md
│ ├── use-tree.md
│ ├── use-watch.md
│ ├── use-webp.md
│ ├── use-xargs.md
│ ├── use-yum-whatprovides-option.md
│ ├── use-zcat.md
│ └── what-i-think-about-when-migrating-php-versions-to-vanilla-php.md
└── web
├── login-form-save-password-dialog.md
├── setting-google-custom-search-for-hugo.md
└── setting-ogp-and-x-card-for-hugo.md
26 directories, 184 files
いい感じ。
--help
で見た通り、他にも多くオプションがあるので試してみる。
1階層のみの表示
tree -L 1
tree -L 1
.
├── about.md
└── post
2 directories, 1 file
1階層のみ+サイズ表示
tree -L 1 -h
tree -L 1 -h
[ 128] .
├── [1.5K] about.md
└── [ 160] post
2 directories, 1 file
1階層のみ+json表示
tree -L 1 -J
tree -L 1 -J
[
{"type":"directory","name":".","contents":[
{"type":"file","name":"about.md"},
{"type":"directory","name":"post"}
]}
,
{"type":"report","directories":2,"files":1}
]
ソート表示(アルファベット順)
tree -v
.
├── about.md
└── post
├── column
│ ├── assigned-books
│ │ ├── book-2024-01.md
│ │ └── book-2024-02.md
│ ├── buy-benq-mobiuz-ex2510s.md
│ ├── introduction.md
│ ├── pass-bookkeeping3.md
│ ├── pass-db.md
│ ├── pass-fp2.md
│ ├── pass-fp3.md
│ ├── pass-nw.md
│ ├── recentry-2020-01-2w.md
│ ├── recentry-2020-01-3w.md
│ ├── recentry-2020-01-4w.md
│ ├── recentry-2020-02-1w.md
│ ├── recentry-2020-02-2w.md
│ ├── recentry-2020-02-3w.md
│ ├── recentry-2020-02-4w.md
│ ├── recentry-2020-02-5w.md
│ ├── recentry-2020-03-1w.md
│ ├── recentry-2020-03-2w.md
│ ├── recentry-2020-03-3w.md
│ ├── recentry-2020-03-4w.md
│ ├── recentry-2020-04-1w.md
│ ├── recentry-2020-04-4w.md
│ ├── recentry-2022-07-4w.md
│ ├── resolution-2023y.md
│ ├── resolution-2024y.md
│ └── review-2023y.md
├── study
│ └── university
│ └── senior-thesis.md
└── tech
├── ai
│ ├── ai-kiritan-play.md
│ ├── renew-article.md
│ └── use-chatgpt-api.md
├── cdn
│ └── failed-to-find-a-valid-digest.md
├── cloud
│ ├── aws-backup-success-job-notifications.md
│ ├── building-gitlab-on-ec2.md
│ ├── monitoring-server-with-route53-healthcheck.md
│ ├── send-mail-ec2-with-postfix-use-aws-ses.md
│ └── use-elemental-media-convert.md
├── container
│ ├── docker-autoheal.md
│ ├── docker-lamp.md
│ └── ssh-2fa-for-docker.md
├── database
│ ├── client-character-set.md
│ ├── compare-tables-with-checksum-statement.md
│ ├── mysql-case-sensitive.md
│ ├── mysql-create-history-table-with-trigger.md
│ ├── mysql-replication.md
│ ├── use-mysql-tuner.md
│ └── use-sql-with.md
├── environment
│ ├── construct-eol-apache-tomcat.md
│ ├── construct-gitlab-ce-docker.md
│ ├── construct-gitlab-runner-docker.md
│ ├── construct-hugo-docker-2.md
│ ├── construct-hugo-docker.md
│ ├── docker-init.md
│ ├── gitlab-mr-review-with-chatgpt-2.md
│ ├── gitlab-mr-review-with-chatgpt.md
│ ├── upgrade-gitlab-15-to-16.md
│ ├── vscode-nuxt.md
│ └── vscode-php.md
├── game
│ ├── play-2048.md
│ ├── valheim-migrate-from-local-to-ec2-server.md
│ └── valheim-server-ec2-arm-instance.md
├── infrastructure
│ ├── construct-apache-tomcat-mysql-to-amazonlinux2023-with-ansible.md
│ ├── construct-ec2-public-private-subnet-alb.md
│ ├── construct-ec2-simple-architecture-with-terraform.md
│ ├── construct-gitlab-ce-with-ansible.md
│ ├── deploy-portfolio-to-vercel.md
│ ├── emulate-raspbian-os-with-qemu.md
│ ├── hugo-theme-update-2.md
│ ├── hugo-theme-update.md
│ ├── introduction-google-analytics-for-hugo.md
│ ├── migrate-from-route53-to-cloudflare.md
│ ├── migrate-from-s3-to-wasabi.md
│ ├── use-ansible-the-first-time.md
│ └── use-cloudflare-ddns.md
├── migrate
│ └── vue2-to-vue3.md
├── programming
│ ├── golang
│ │ ├── build-web-application-with-golang-1.md
│ │ ├── build-web-application-with-golang-2.md
│ │ ├── build-web-application-with-golang-3.md
│ │ ├── build-web-application-with-golang-4.md
│ │ ├── build-web-application-with-golang-5.md
│ │ ├── build-web-application-with-golang-6.md
│ │ ├── build-web-application-with-golang-7.md
│ │ ├── build-web-application-with-golang-8.md
│ │ ├── build-web-application-with-golang-9.md
│ │ ├── build-web-application-with-golang-10.md
│ │ ├── build-web-application-with-golang-11.md
│ │ ├── build-web-application-with-golang-12.md
│ │ ├── build-web-application-with-golang-13.md
│ │ ├── build-web-application-with-golang-14.md
│ │ ├── build-web-application-with-golang-15.md
│ │ ├── build-web-application-with-golang-16.md
│ │ ├── build-web-application-with-golang-17.md
│ │ ├── build-web-application-with-golang-18.md
│ │ ├── build-web-application-with-golang-19.md
│ │ ├── build-web-application-with-golang-20.md
│ │ ├── build-web-application-with-golang-21.md
│ │ ├── build-web-application-with-golang-22.md
│ │ ├── build-web-application-with-golang-23.md
│ │ ├── build-web-application-with-golang-24.md
│ │ ├── build-web-application-with-golang-25.md
│ │ ├── build-web-application-with-golang-26.md
│ │ ├── build-web-application-with-golang-27.md
│ │ ├── build-web-application-with-golang-28.md
│ │ ├── build-web-application-with-golang-29.md
│ │ ├── go-competitive-programming-code-01.md
│ │ └── go-competitive-programming-code-02.md
│ ├── javascript
│ │ └── vue
│ │ └── refactor-vue3-composition-api.md
│ ├── php
│ │ ├── convert-html-to-pdf-with-headless-chrome.md
│ │ ├── implement-caesar-cipher.md
│ │ └── implement-substitution-cipher.md
│ └── python
│ └── eol-notify-python.md
├── scm
│ └── using-cvs2git.md
├── summarize
│ └── web-develop-watch-list.md
├── tips
│ ├── apache-tomcat-configuration-to-let-apache-handle-static-resources.md
│ ├── convert-excel-to-pdf.md
│ ├── convert-heic-to-png-with-imagemagick.md
│ ├── convert-html-to-pdf.md
│ ├── create-certificates-with-san.md
│ ├── diff-two-files-with-vscode.md
│ ├── ec2-attach-volume.md
│ ├── ec2-eni-take-over-ip.md
│ ├── fix-code-quote-shaping.md
│ ├── git-attention.md
│ ├── git-mv.md
│ ├── install-gitlab-update-script-amzn-2023-to-2023.md
│ ├── install-lightbox2-to-hugo-blog.md
│ ├── list-files-modified-after-the-specify-datetime.md
│ ├── lost-aws-ec2-keypair.md
│ ├── mount-wasabi-bucket-with-aws-cli.md
│ ├── nginx-before-tls-1_1-disable.md
│ ├── reduce-volume-ebs.md
│ ├── remove-exif.md
│ ├── replace-html-code-to-shorcode.md
│ ├── rescue-ec2-instance.md
│ ├── update-hugo-version-from-0.110-to-0.111.md
│ ├── update-hugo-version-from-0.111.3-to-0.122.0.md
│ ├── use-aamath.md
│ ├── use-ansiweather.md
│ ├── use-apg.md
│ ├── use-apvlv.md
│ ├── use-barcode.md
│ ├── use-bash-shortcut.md
│ ├── use-busybox.md
│ ├── use-calc.md
│ ├── use-cbm.md
│ ├── use-circumflex.md
│ ├── use-codequery.md
│ ├── use-colordiff.md
│ ├── use-countdown.md
│ ├── use-csview.md
│ ├── use-csvtomd.md
│ ├── use-exiftool.md
│ ├── use-fcrackzip.md
│ ├── use-figure-code-jane.md
│ ├── use-flock.md
│ ├── use-github-connect-with-ssh-over-https.md
│ ├── use-htop.md
│ ├── use-jq.md
│ ├── use-logrotate.md
│ ├── use-markdown-vscode-extension.md
│ ├── use-md5-sum.md
│ ├── use-ntpdate.md
│ ├── use-nyancat.md
│ ├── use-pdfcrack.md
│ ├── use-pdfseparate.md
│ ├── use-pdftocairo.md
│ ├── use-pdfunite.md
│ ├── use-qrencode.md
│ ├── use-rlogin-portforward.md
│ ├── use-shortcode-hugo.md
│ ├── use-sl.md
│ ├── use-slack-notifications-gitlab.md
│ ├── use-tesseract.md
│ ├── use-tree.md
│ ├── use-watch.md
│ ├── use-webp.md
│ ├── use-xargs.md
│ ├── use-yum-whatprovides-option.md
│ ├── use-zcat.md
│ └── what-i-think-about-when-migrating-php-versions-to-vanilla-php.md
└── web
├── login-form-save-password-dialog.md
├── setting-google-custom-search-for-hugo.md
└── setting-ogp-and-x-card-for-hugo.md
26 directories, 184 files
おわりに
ディレクトリ構造を知る際に、tree
での表示をするのは全体構造の把握によく使うので知っておきたい。