gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:30:55 24 4
gpt4 key购买 nike

我正在做我的一个项目,它使用非常有限的 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

最佳答案

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

-R List subdirectories recursively

所以你可以这样写:

$ 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>/* )。
    这有一个限制。 它不显示名称只是 * 的文件..
  • 如果文件是一个目录,它会打印目录名和/。附加在末尾以改进输出,然后为该目录递归调用该函数。
  • 如果项目是常规文件,它只打印文件名并转到下一个文件。

关于linux - 在有限的 shell 中列出文件和文件夹层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27435336/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com