Skip to content

Instantly share code, notes, and snippets.

@yokawasa
Last active October 4, 2025 00:20
Show Gist options
  • Save yokawasa/abf581a5fcbe6fe97d197dd1a0bf57f6 to your computer and use it in GitHub Desktop.
Save yokawasa/abf581a5fcbe6fe97d197dd1a0bf57f6 to your computer and use it in GitHub Desktop.

Revisions

  1. yokawasa revised this gist Oct 4, 2025. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions awk-tree
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    #!/bin/sh
    # ディレクトリが指定されていればそれを使用、なければ現在のディレクトリを使用
    # 指定ディレクトリまたは現在のディレクトリを使用
    [ -d "$1" ] && DIR=$1 && shift || DIR=.
    # ディレクトリを絶対パスに変換
    (cd ${DIR}; pwd)
    # ディレクトリ内のファイルとサブディレクトリをリストアップし、ツリー形式で表示
    # ディレクトリ内のファイルとサブディレクトリをツリー表示
    find "${DIR}" | \
    awk -v dir="${DIR}" '
    function is_dir(path, cmd, result) {
    @@ -13,11 +13,11 @@ function is_dir(path, cmd, result) {
    return result == 1
    }
    {
    sub("^" dir, "", $0) # 先頭のディレクトリパスを削除
    if ($0 == "") next # 空行はスキップ
    sub("^" dir, "", $0) # 先頭ディレクトリパス削除
    if ($0 == "") next # 空行スキップ
    n = split($0, parts, "/") # パスをスラッシュで分割
    indent = ""
    for (i = 2; i < n; i++) indent = indent " " # インデント作成
    for (i = 2; i < n; i++) indent = indent " "
    name = parts[n]
    if (is_dir($0)) {
    # ディレクトリは青色
  2. yokawasa created this gist Jul 19, 2025.
    30 changes: 30 additions & 0 deletions awk-tree
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    #!/bin/sh
    # ディレクトリが指定されていればそれを使用、なければ現在のディレクトリを使用
    [ -d "$1" ] && DIR=$1 && shift || DIR=.
    # ディレクトリを絶対パスに変換
    (cd ${DIR}; pwd)
    # ディレクトリ内のファイルとサブディレクトリをリストアップし、ツリー形式で表示
    find "${DIR}" | \
    awk -v dir="${DIR}" '
    function is_dir(path, cmd, result) {
    cmd = "test -d \"" dir path "\" && echo 1 || echo 0"
    cmd | getline result
    close(cmd)
    return result == 1
    }
    {
    sub("^" dir, "", $0) # 先頭のディレクトリパスを削除
    if ($0 == "") next # 空行はスキップ
    n = split($0, parts, "/") # パスをスラッシュで分割
    indent = ""
    for (i = 2; i < n; i++) indent = indent " " # インデント作成
    name = parts[n]
    if (is_dir($0)) {
    # ディレクトリは青色
    print indent "+---\033[34m " name "\033[0m"
    } else {
    # ファイルは色なし
    print indent "+-- " name
    }
    }
    '