So, I need to call out one thing. For the people mentioning `ls | grep` and the like, in auto mode `ls` disables colors when it sees it is not running in a terminal. So using it in a shell script (or similar construction) doesn’t have this issue. Otherwise your loop variable would be full of weird ANSI escape color sequences!
It clearly depends on what good that subshell running ls does for you, but the trivial form would be:
for i in *txt
do
...
done
The reason to stuff every file name through ls (keep in mind that it's always your shell doing glob expansions, ls is not involved) would be to sort them by creation date or some other processing that ls can do. But as soon as there are many files involved, or those sorting arguments gets non-trivial, it will fall apart completely.
Use find | while read instead, or xargs. That's going to be easier on the eyes and actually work.
works even better as it can handle files with whitespace. I usually disable file globbing but enable it specifically for code like this.
EDIT: Actually, one issue with the above is that if there aren't any matching files then you get a literal "*". But I'd rather deal with that then break when filenames have embedded whitespace.
Also, we didn't see any timings for that 1,000x claim, so let's say it's an estimate. :)