933
763

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

tree コマンドが無い環境で tree コマンドを実現

Last updated at Posted at 2014-10-07

tree コマンドは便利

Linux における tree コマンドは便利ですね。しかし標準コマンドではないため初めて操作するサーバや手軽に tree をインストール出来ない環境だとディレクトリ構造を奇麗にツリー上に表示することが出来ません。
その場合は find や sed を使って tree コマンドを代替しましょう。

find と sed で代替

pwd;find . | sort | sed '1d;s/^\.//;s/\/\([^/]*\)$/|--\1/;s/\/[^/|]*/|  /g'

実行例

以下のようなディレクトリ構造が(ファイル名・ディレクトリ名にスペースを含む)

$ find .
.
./AAA
./AAA/aaa
./B B
./B B/aaa
./B B/bbb
./B B/vvv
./B B/vvv/www
./CCC
./CCC/aaa
./CCC/bbb
./CCC/ccc
./CCC/dd
./CCC/ee ee
./CCC/yyy
./CCC/yyy/iii
./CCC/yyy/iii/eee
./CCC/yyy/iii/eee/foo.txt
./dd
./hoge.txt

疑似 tree で

$ pwd;find . | sort | sed '1d;s/^\.//;s/\/\([^/]*\)$/|--\1/;s/\/[^/|]*/|  /g'
/Users/yone098/work/yone098
|--AAA
|  |--aaa
|--B B
|  |--aaa
|  |--bbb
|  |--vvv
|  |  |--www
|--CCC
|  |--aaa
|  |--bbb
|  |--ccc
|  |--dd
|  |--ee ee
|  |--yyy
|  |  |--iii
|  |  |  |--eee
|  |  |  |  |--foo.txt
|--dd
|--hoge.txt

alias

alias として .zshrc.bashrc などに設定して使ってください

alias tree="pwd;find . | sort | sed '1d;s/^\.//;s/\/\([^/]*\)$/|--\1/;s/\/[^/|]*/| /g'"

:mahjong:

933
763
8

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
933
763

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?