4
3

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 コマンド (awk版実装)

Posted at

以前 sed(1) を利用した tree コマンドの実装が投稿されていたので、awk(1) 版を投稿。
ワンライナーで済む分だけ sed(1) 版の方が遙かに美しいですが折角なので。

遙か昔 (1997年頃) に必要に迫られてでっち上げたスクリプトなので、表示が崩れるなど不具合のある可能性もありますがそのまま公開しちゃいます。

#!/bin/sh
LANG=C
du -a $* |
sort -k 2|
awk '{
    num = split($2, path, "/")
    max = (max < num) ? num : max
    for(i=1; i<=num; i++)
        if(old[i] == path[i])
            printf("  |")
        else
            break
        printf("- %s \n", path[i])
        old[i] = path[i]

        while(++i <= max)
            old[i] = ""
  }'
4
3
1

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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?