Kodama's home / tips.

ディレクトリのツリー表示(ディレクトリの tree 構造を見る)

ディレクトリの tree 構造を見るコマンドは探せばいくつかあるけど, 自分で簡単につくれるので sed 等の練習に自分でやってみるとよい. dtree とか云う名で スクリプトを作っておくと良いよね. 内部では find でディレクトリを探っているので, find のオプションを改造して, 特定の条件のファイルやディレクトリを図示するのにも使える.

実行例

$ dtree /home/ftp
/home/ftp
+--bin
+--etc
+--incoming
+--lib
+--pub
|  +--knot
|  |  +--old
|  +--tools
|  :  +--dic
|  :  +--sather
+--usr
:  +--bin

sed による例

Linux Journal で見かけたものを改造. 1 行に詰め込んじゃおう. find の結果を sed で編集して木形式にしている.
$ cd /home/ftp
$ find . -type d|sort|sed -ne'1b;s/[^\/]*\//+--/g;s/+--+/|  +/g;s/+--+/|  +/g;s/+--|/|  |/g;p'
+--bin
+--etc
+--incoming
+--lib
+--pub
|  +--knot
|  |  +--old
|  +--tools
|  |  +--dic
|  |  +--sather
+--usr
|  +--bin

Kodama's home / tips.