Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
707
Help us understand the problem. What is going on with this article?
@yone098@github

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

More than 3 years have 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:

707
Help us understand the problem. What is going on with this article?
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
yone098@github
バスケットボールが大好きです

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
707
Help us understand the problem. What is going on with this article?