🌲

ターミナルにtreeコマンドを追加した話

に公開

treeコマンド

プロジェクト内のディレクトリ構造を確認したい時や、一覧が欲しい時に使えるコマンド

treeコマンドの導入

Macの場合

brew install tree

.zshrcに追加

aliasに追加

alias tree="tree -I 'node_modules'"

functionsに追加

# gitignore を反映した tree を実行する関数
gitree() {
  if [[ ! -f .gitignore ]]; then
    echo ".gitignore が見つかりません"
    return
  fi

  IGNORE=$(grep -v '^#' .gitignore | grep -v '^$' \
    | sed 's:^\/*::' \
    | sed 's:/$::' \
    | sed 's/\./\\./g' \
    | sed 's/\*/.*/g' \
    | tr '\n' '|' )

  IGNORE="${IGNORE%|}"

  # "$@" をそのまま tree に渡す(任意オプション)
  # -L 2 などのオプションを受け取る → "$@"
  tree -I "$IGNORE" "$@"
}

screenshot

最後に更新

source .zshrc

Discussion