Linux 在有限的shell中列出文件和文件夹层次结构

Linux 在有限的shell中列出文件和文件夹层次结构,linux,bash,busybox,Linux,Bash,Busybox,我正在从事一个使用非常有限的linux busybox外壳的项目 我的shell没有诸如find、awk、grep之类的命令,我正在尝试获取该计算机上文件的完整列表 到目前为止没有运气,但运行ls-la/*完成了一半的工作,并将文件显示了一层深度。 你知道我如何递归运行ls来获取文件和文件夹的完整列表吗?也许你知道其他的方法吗 编辑#1: 我的ls没有-R选项 ls -1 -LR / ls: invalid option -- R BusyBox v1.01 multi-call binary

我正在从事一个使用非常有限的linux busybox外壳的项目

我的shell没有诸如
find
awk
grep
之类的命令,我正在尝试获取该计算机上文件的完整列表

到目前为止没有运气,但运行
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
从的页面中,我可以看到您有
ls
的选项
-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将模式保留为
    /*
    )。
    这是有限制的它不显示名称仅为
    *
    的文件
  • 如果文件是一个目录,它将打印目录名,并在末尾附加一个
    /
    ,以改进输出,然后递归调用该目录的函数
  • 如果该项是常规文件,它只打印文件名并转到下一个文件
使用

ls -1 -LR /
垂直格式看起来也不错