Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

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:

yone098@github
バスケットボールが大好きです
http://yone098.hatenablog.com/
abby
少数精鋭の会社です!
https://www.abby.co.jp
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした