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*

4
  • 3
    But then you'll still have problems with filenames containing newline or glob characters. See also: Why is looping over find's output bad practice?. If using zsh, you can use IFS=$'\0' and use -print0 (zsh doesn't do globbing upon expansions so glob characters are not a problem there). Commented Feb 28, 2018 at 14:10
  • 1
    This works with file names containing spaces, but it doesn't work against potentially hostile file names or accidental “nonsensical” file names. You can easily fix the issue of file names containing wildcard characters by adding set -f. On the other hand, your approach fundamentally fails with file names containing newlines. When dealing with data other than file names, it also fails with empty items. Commented Mar 1, 2018 at 18:52
  • Right, my caveat is that it won't work with newlines in filenames. However, I believe we have to draw the line just shy of madness ;-) Commented Mar 8, 2018 at 18:01
  • And I'm not sure why this received a downvote. This is a perfectly reasonable method for iterating over filenames with spaces. Using -print0 requires xargs, and there are things that are difficult using that chain. I'm sorry someone does not agree with my answer, but that's no reason to downvote it. Commented Mar 8, 2018 at 18:03