如何对指定路径下的所有文件执行for-each循环? [英] How to perform a for-each loop over all the files under a specified path?
本文介绍了如何对指定路径下的所有文件执行for-each循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下命令尝试枚举当前目录中的所有*.txt
文件并对其进行逐一处理:
The following command attempts to enumerate all *.txt
files in the current directory and process them one by one:
for line in "find . -iname '*.txt'"; do
echo $line
ls -l $line;
done
为什么会出现以下错误?
Why do I get the following error?:
ls: invalid option -- 'e'
Try `ls --help' for more information.
推荐答案
这是遍历文件的更好方法,因为它可以处理文件名中的空格和换行符:
Here is a better way to loop over files as it handles spaces and newlines in file names:
find . -type f -iname "*.txt" -print0 | while IFS= read -r -d $'\0' line; do
echo "$line"
ls -l "$line"
done
这篇关于如何对指定路径下的所有文件执行for-each循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文