大佬教程收集整理的这篇文章主要介绍了Shell 循环遍历特定目录中的所有文件,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如何使用sHell脚本遍历特定目录中的所有文件?我在当前工作目录中有一个目录,即 .temp。在 .temp 中还有另一个目录索引,其中包含一些文件。
我尝试了以下方法:
for file in ./.temp/index/*
do
echo "file in index: $file"
done
for file in "./.temp/index"/*
do
echo "file in index: $file"
done
但是,它会将 $file
打印为 ./.temp/index/$file
。例如:
file in index: ./.temp/index/first.txt
file in index: ./.temp/index/second.txt
file in index: ./.temp/index/third.txt
...
是否有符合 POSIX 标准的方式来循环遍历该目录中的实际文件?
for _file in `find . -type f`;
do
echo "File: $_file";
done
-type f
将在目录中查找文件。您可以根据需要添加其他过滤器。
您可以通过使用 find 和 -printf 使用 %f 仅打印文件名来避免同时使用循环:
find ./.temp/index -maxdepth 1 -type f -printf "File in index: %f\n"
以上是大佬教程为你收集整理的Shell 循环遍历特定目录中的所有文件全部内容,希望文章能够帮你解决Shell 循环遍历特定目录中的所有文件所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。