我对 bash 脚本还很陌生,现在我正在尝试递归地遍历文件,以便接收有关这些文件的一些信息(名称、大小……)
到目前为止我的尝试:
for i in *.txt; do
stat -c '%n' "$i" >> $2
wc -l -w >> $2
stat -c '%a %A %U' "$i" >> $2
done
$2 is the file, where i want to log this info...
提前致谢!
编辑 我也应该发布问题,抱歉
我收到此错误消息:
stat: cannot stat '*.txt': No such file or directory
但是文件应该在 $i 变量中,不是吗?
最佳答案
如果没有匹配 glob 的文件,Bash 默认只返回 glob 本身。所以你正在尝试处理一个名为“*.txt”的文件(顺便说一下,这实际上是一个有效的文件名)。您可能想要的是 shopt -s nullglob
将不匹配的 glob 扩展为空。
关于linux - 递归遍历文件并接收文件信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54065793/