Bash-递归在目录及其子目录中递归找到所有文件的类型的好方法是什么?

发布于 2025-01-23 22:23:04 字数 425 浏览 0 评论 0原文

我的材料不太深,很少使用bash脚本。 即使有了一些研究,我也无法快速学习Bash中的所有内容,因此我可以搜索一个用子目录的整个目录中的文件,然后输出其类型。现在,我已经了解了功能的方向,但是再次不知道该如何递归地做到这一点。另外,我只想考虑文件而不是文件夹。 这是我自己已经完成的事情:

for item in "$1"/*
do
    if ! [ -d $item ]; then
        echo $(file $item)
    fi
done;

因此,当调用脚本时,路径将作为一个参数传递。然后,将路径搜索非直接导向,并使用命令文件输出它们的类型。 但是,该递归以及子目录如何实现? 我还通过迭代LS -R进行了尝试,但随后仍然附加了文件夹的名称,如果它是文件夹或文件,我无法再通过方式检查。 编辑:我找不到找到!

我很高兴任何帮助!

I am not too deep in the material and rarely use Bash scripts.
Even with some research, I couldn't quickly learn everything in Bash so that I could search an entire directory with its sub-directories for files and then output their type. I've now gotten a bit into the direction of functions, but again don't quite know how to do this recursively. Also, I want to consider only files and not folders.
Here is something I have already done on my own:

for item in "$1"/*
do
    if ! [ -d $item ]; then
        echo $(file $item)
    fi
done;

So when the script is called, the path is passed as an argument. The path is then searched for non-directories and their type is output with the command file.
But how is this recursive and also implementable for sub-directories?
I have also tried it by iterating over ls -R, but then names of folders are still appended and I can no longer check via my way if it is a folder or a file.
Edit: I can't use find!

I am glad about any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨的味道风的声音 2025-01-30 22:23:04

您可以使用bash扩展的地球功能:

$ shopt -s dotglob globstar
$ for i in **/*; do [ -d "$i" ] && continue; file "$i"; done

You can use bash extended globbing capabilities:

$ shopt -s dotglob globstar
$ for i in **/*; do [ -d "$i" ] && continue; file "$i"; done
和影子一齐双人舞 2025-01-30 22:23:04

这可能会有所帮助:如何递归列出bash中的子目录,而无需使用“查找”或“ ls”命令?

也就

#!/bin/bash

recurse() {
 for i in "$1"/*;do
    if [ -d "$i" ];then
        echo "dir: $i"
        recurse "$i"
    elif [ -f "$i" ]; then
        echo "file: $i"
    fi
 done
}

recurse $1

)然后只需卸下埃利夫并在其下方排队即可。我把它留在原始帖子时也留在那里。希望这会有所帮助。

This may help: How to recursively list subdirectories in Bash without using "find" or "ls" commands?

That said, I modified it to accept user input as follows:

#!/bin/bash

recurse() {
 for i in "$1"/*;do
    if [ -d "$i" ];then
        echo "dir: $i"
        recurse "$i"
    elif [ -f "$i" ]; then
        echo "file: $i"
    fi
 done
}

recurse $1

If you didn't want the files portion (which it appears you don't) then just remove the elif and line below it. I left it in as the original post had it also. Hope this helps.

蒗幽 2025-01-30 22:23:04

建议

   file $(find -type f 2>/dev/null)

说明:

  1. 查找命令将递归搜索到当前文件夹中。

  2. 仅从find> find命令:-type f

  3. 忽略任何查找使用2>/dev/null

    的权限/访问错误

  4. 运行file> file> file命令的权限/访问错误。

Suggesting

   file $(find -type f 2>/dev/null)

Explanation:

  1. find command that search recursively into current folder.

  2. Filter only files from find command: -type f .

  3. Ignore any find permission/access errors with 2>/dev/null

  4. Run file command on each found file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文