【问题标题】:Listing files and folders hierarchy in a limited shell在有限的外壳中列出文件和文件夹层次结构
【发布时间】:2014-12-12 01:11:09
【问题描述】:

我正在开发一个使用非常有限的 linux busybox shell 的项目。

我的 shell 没有诸如 findawkgrep 之类的命令,我正在尝试获取该机器上文件的完整列表。

到目前为止没有运气,但运行 ls -la /* 完成了一半的工作并显示了一层深度的文件。
您对我如何递归运行ls 以获取文件和文件夹的完整列表有任何想法吗?也许您知道其他方法可以做到这一点?

编辑#1:

我的 ls 没有 -R 选项。

ls -1 -LR /

ls: invalid option -- R
BusyBox v1.01 multi-call binary

Usage: ls [-1AacCdeilnLrSsTtuvwxXk] [filenames...]

List directory contents

Options:
    -1  list files in a single column
    -A  do not list implied . and ..
    -a  do not hide entries starting with .
    -C  list entries by columns
    -c  with -l: show ctime
    -d  list directory entries instead of contents
    -e  list both full date and full time
    -i  list the i-node for each file
    -l  use a long listing format
    -n  list numeric UIDs and GIDs instead of names
    -L  list entries pointed to by symbolic links
    -r  sort the listing in reverse order
    -S  sort the listing by file size
    -s  list the size of each file, in blocks
    -T NUM  assume Tabstop every NUM columns
    -t  with -l: show modification time
    -u  with -l: show access time
    -v  sort the listing by version
    -w NUM  assume the terminal is NUM columns wide
    -x  list entries by lines instead of by columns
    -X  sort the listing by extension

【问题讨论】:

    标签: linux bash busybox


    【解决方案1】:

    BusyBox的页面我可以看到你有ls的选项-R

    -R 递归列出子目录

    所以你可以写:

    $ ls -R /
    

    由于您没有 -R 选项,您可以尝试使用这样的递归 shell 函数:

    myls() {
        for item in "$1"/* "$1"/.*; do
            [ -z "${item##*/.}" -o -z "${item##*/..}" -o -z "${item##*/\*}" ] && continue
            if [ -d "$item" ]; then
                echo "$item/"
                myls "$item"
            else
                echo "$item"
            fi    
        done
    }
    

    然后你可以不带参数地从/开始调用它。

    $ myls
    

    如果你想从/home开始:

    $ myls /home
    

    如果你想制作一个脚本:

    #!/bin/sh
    
    # copy the function here
    
    myls "$1"
    

    说明

    • [ -z "${item##*/.}" -o -z "${item##*/..}" -o -z "${item##*/\*}" ] && continue 此行仅排除目录... 以及未展开的项目(如果文件夹中没有文件,则shell 将模式保留为<some_folder>/*)。
      这有一个限制。 它不显示名称只是 * 的文件。
    • 如果文件是目录,它会打印目录名称并在末尾附加/ 以改进输出,然后为该目录递归调用函数。
    • 如果项目是常规文件,它只会打印文件名并转到下一个。

    【讨论】:

      【解决方案2】:

      使用

      ls -1 -LR /
      

      垂直格式也很好看

      【讨论】:

        猜你喜欢
        • 2012-05-03
        • 1970-01-01
        • 2018-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多