Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • 2
    You also shouldn't parse the output of ls. Simple wildcards and test -f would do. Commented Sep 11, 2019 at 18:50
  • 1
    @JeffSchaller you mean ‘find . -type f’ ? Commented Sep 11, 2019 at 18:55
  • Good point. I think @JeffSchaller was hinting at adding a check inside the for loop. I updated the answer to use find . -type f though Commented Sep 11, 2019 at 18:58
  • No; I meant: for i in *.txt; do if [ -f "$i" ] .... Looping over find's output is equally dangerous. Commented Sep 11, 2019 at 19:02
  • 1
    If any filename contains whitespace (anything in $IFS) it will be split; touch "123_20 space.txt" to see an example. Commented Sep 11, 2019 at 19:14