Bash-递归在目录及其子目录中递归找到所有文件的类型的好方法是什么?
我的材料不太深,很少使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用bash扩展的地球功能:
You can use bash extended globbing capabilities:
这可能会有所帮助:如何递归列出bash中的子目录,而无需使用“查找”或“ ls”命令?
也就
)然后只需卸下埃利夫并在其下方排队即可。我把它留在原始帖子时也留在那里。希望这会有所帮助。
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:
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.
建议
说明:
查找
命令将递归搜索到当前文件夹中。仅从
find> find
命令:-type f
。忽略任何
查找
使用2>/dev/null
的权限/访问错误
运行
file> file> file
命令的权限/访问错误。Suggesting
Explanation:
find
command that search recursively into current folder.Filter only files from
find
command:-type f
.Ignore any
find
permission/access errors with2>/dev/null
Run
file
command on each found file.