bash中的格式化目录树

时间:2016-06-29 04:40:56

标签: bash

我想使用简单的for循环和一些模式匹配运算符以下面的格式化方式打印仅代表bash目录的目录树:

.
|
|-------root
|       |
|       |-------d1
|       |       |
|       |       |-------d11
|       |       |-------d12
|       |       |-------d13
|       |       |-------d14


|        |-------d2
...

我正在尝试使用以下脚本执行此操作

 # this function calls itself recursively till it encounters a file which is
 # not a directory
 recdir()
 {
    #echo "$@"
    formatting=${formatting%$dashes}

    for file in "$@"; do

            # move one directory down
            thisfile=$thisfile/$file

            if [ $count -eq 0 ]; then
                    formatting=$formatting$singletab$vert$dashes
            else
                    formatting=${formatting%$singletab*}
                    formatting=$formatting$singletab$vert$dashes
            fi

            if [ -d "$thisfile" ]; then
                    echo -e "$formatting$file"
            fi

            if [ -d "$thisfile" ]; then
                    count=0
                    recdir $(command ls $thisfile)
            fi
            count=$(( $count + 1 ))

            if [ $count -le 1 ]; then
                    formatting=${formatting%$singletab*}
            fi
            # removes the string and moves one directory up
            thisfile=${thisfile%/*}
    done
}

recls()
{
     singletab="       "
     vert="|"
     dashes="-------"

     # This variable will be used in recdir() to detect whether it has reached
     # a directory which has no directory in it and hence remove the trailing
     # spaces which were added to it in recdir() function.

     count=0
     echo "."
     echo -e "$vert"
     for tryfile in "$@"; do

             if [ -d "$tryfile" ]; then
                     formatting="$vert$dashes"
                     echo -e $formatting$tryfile
                     thisfile=$tryfile
                     # makes call to recur
                     recdir $(command ls $tryfile)
             fi
     done

     unset singletab formatting vert dashes tryfile
}

我在.bash_profile文件中添加了这两个函数,并尝试将它们称为:

recls nameofdirectory/

我得到的输出不一致。我确实看到这个脚本中的一个问题是删除了recdir()函数中的空格但是它的行为与我现在想的相反。有人可以建议我可以对此脚本做出哪些更改,以获得所需的结果。

输出不一致的一个例子是:

|
|-------V1.2/
|       |-------100
|       |-------DAT
|       |       |-------000000
|       |-------000001
|       |-------000002
|       |-------000003
|       |-------000004
|       |-------000005
|       |-------000006
|       |-------000007
|       |-------DATA1
|       |       |-------data-msd1-100
|       |       |-------DAT
|       |       |       |-------000000
|       |       |       |-------000001
|       |       |       |-------000002
|       |       |       |-------000003
|       |       |       |-------000004
|       |       |       |-------000005
|       |       |       |-------000006
|       |       |       |-------000007
|       |       |       |-------data-msd2-100
|       |       |       |-------DAT
|       |       |       |       |-------000000
|       |       |       |       |-------000001
|       |       |       |       |-------000002
|       |       |       |       |-------000003
...

输出应该是这样的:

.
|
|-------V1.2/
|       |-------100
|       |       |-------DAT
|       |       |        |-------000000
|       |       |        |-------000001
|       |       |        |-------000002
|       |       |        |-------000003
|       |       |        |-------000004
|       |       |        |-------000005
|       |       |        |-------000006
|       |       |        |-------000007
|       |       |-------DATA1
|       |       |        |-------data-msd1-100
|       |       |        |       |-------DAT
|       |       |        |       |       |-------000000
|       |       |        |       |       |-------000001
|       |       |        |       |       |-------000002
|       |       |        |       |       |-------000003
|       |       |        |       |       |-------000004
|       |       |        |       |       |-------000005
|       |       |        |       |       |-------000006
|       |       |        |       |       |-------000007
|       |       |        |-------data-msd2-100
|       |       |        |       |-------DAT
|       |       |        |       |       |-------000000
|       |       |        |       |       |-------000001
|       |       |        |       |       |-------000002
|       |       |        |       |       |-------000003
...

谢谢!

2 个答案:

答案 0 :(得分:2)

find . -type d | sed -e "s/[^-][^\/]*\//  |/g" -e "s/|\([^ ]\)/|-\1/"
.
  |-storage1
  |-storage2
  |-storage3
  |-cache
  |-temp
  |  |-test
  |  |  |-one
  |  |  |-two
  |  |  |-three
  |  |  |-four
  |-storage4

答案 1 :(得分:0)

您好我通过简单的操作找到了问题的答案:

recdir()
{
    format=$format$spaces$vert

    for file in "$@"; do

            thisfile=$thisfile/$file

            if [ -d "$thisfile" ]; then
                    echo -e $format$dashes$file
                    recdir $(command ls $thisfile)
            fi

            thisfile=${thisfile%/*}
    done

    #tab=${tab%"$singletab"}
    format=${format%"$spaces$vert"}
}

recls()
{

    vert="|"
    dashes="-------"
    spaces="\t"
    echo "."
    echo "|"
    format=$vert
    for tryfile in "$@"; do
            echo -e $format$dashes$tryfile
            if [ -d "$tryfile" ]; then
                    thisfile=$tryfile
                    recdir $(command ls $tryfile)
            fi
    done

    unset vert dashes spaces format
}

这将打印出我需要的所需结果。如果您有更好更简洁的解决方案,请发表评论。