【问题标题】:Bash script Shell ScriptBash 脚本 Shell 脚本
【发布时间】:2014-11-25 18:40:34
【问题描述】:

需要 bash 脚本来显示文件

#!/bin/bash

my_ls() {
  # save current directory then cd to "$1"
  pushd "$1" >/dev/null
  # for each non-hidden (i.e. not starting with .) file/directory...
  for file in * ; do
    # print file/direcotry name if it really exists...
    test -e "$file" && echo "$2$file"
    # if directory, go down and list directory contents too
    test -d "$file" && my_ls "$file" "$2    "
  done
  # restore directory
  popd >/dev/null
}

my_ls

预期输出:

file1
file2
info (directory)
    data
    stuff (directory)
        input
    output
    scripts (directory)
    doit
        helper
    testinput
jobs
results (directory)
    bob
    dave
    mary

【问题讨论】:

  • 我建议使用find 命令并重新格式化其输出。
  • @Jdamian 这正是我需要的
  • file1,file2,jobs 只是文件,我们需要显示(目录下的目录)
  • 或者你可以只使用树。
  • 如果您知道find 命令,只需开始创建代码并对其进行测试。如果它不起作用,请向我们展示代码及其输出;那么我们将能够为您提供帮助。正如@Celeo 提到的,这不是编码服务。

标签: linux bash shell


【解决方案1】:

这是一个 bash 脚本,可以满足您的要求。

$ cat tree
#!/bin/bash
# tree [DIR]

ls_tree() {
  dir="${1:-.}"
  while read name ; do
    if [[ -n "$name" ]]; then
      if [[ "$name" =~ :$ ]]; then
        dir="${name%:}"
      else
        echo "$dir/${name%*}"
      fi
    fi
  done
}

show_tree() {
  sed -e 's|^./||' | sort | sed -e 's|/$||' -e 's|[^/]*/|    |g'
}


/bin/ls -1RF "${1:-.}" | ls_tree "${1:-.}" | show_tree

这是一个示例输出:

$ tree
fun.rb
fun
    data
    filter.awk
    good
    inputtest.sh
    reqs
    test1.awk
input
pairs
    pairs.sh
split-collections
    indir
        col1
        col2
        col3
    outdir
        file1
        file2
        file21
        file22
        file23
        file3
        file31
        file32
        file33
    split-collections.sh
    t1
tree1

【讨论】:

  • 如何在目录前面打印(目录)?
  • 如何在目录前面添加目录??
  • 我应该在哪里添加(目录)?
  • 要插入'(目录)',修改“show_tree”中的sed命令:sed -e 's|^./||' |排序 | sed -e 's|^(.*)/$|(目录) \1|' -e 's|[^/]*/| |g'
  • 你能帮我这不是工作吗
【解决方案2】:

这是一个基本的单行解决方案:

find . -type d -printf '%p (directory)\n' -o -print | LC_ALL=C sort | sed 's:[^/]*/:    :g'

为了便于阅读,将相同的代码分成多行:

find . -type d -printf '%p (directory)\n' -o -print |
LC_ALL=C sort |
sed 's:[^/]*/:    :g'

LC_ALL=C 是为了防止 sort 根据语言环境做出奇怪的事情。网络上有很多解决此类问题的方法。尝试搜索find sed tree。第一个命中是 StackOverflow 问题:Tree functionality using sed and find command

请注意,此解决方案不适用于 Mac OS,因为它的“查找”是不支持“-printf”选项的旧解决方案。

【讨论】:

  • @user4186509,我已经在几个系统上测试了单行(从 Stackoverflow 页面剪切和粘贴),没有发现明显问题。你看到了什么问题?您是否遇到错误,或者输出不是您所期望的?
  • @user4186509,我怀疑您遇到了剪切和粘贴错误。代码中的所有引号都匹配。也许有些字符从长线的末尾丢失了。我在答案中添加了一个多行版本,所以也许可以尝试使用它。
  • @user4186509,我修改了答案,说它不适用于 Mac OS。这个问题有一个“linux”标签,所以我认为只有 linux 的解决方案是可以接受的。可以修改它以在 Mac OS 上工作,但在输出中获取“(目录)”字符串会很麻烦。
  • @user4186509,摆脱“.”的一种选择行是将tail -n +2 紧跟在find 之后的管道中:find . ... | tail -n +2 | LC_ALL=C sort | ...
  • @user4186509,这个输出可能更接近你想要的:find . -type d -printf '%P (directory)\n' -o -printf '%P\n' | tail -n +2 | LC_ALL=C sort | sed 's:[^/]*/: :g'
猜你喜欢
  • 2018-02-23
  • 1970-01-01
  • 2014-06-05
  • 2021-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-10
  • 2019-10-31
相关资源
最近更新 更多