macOS: "Operation timed out" in shell commands (grep, etc.)

I spent way too much time to find out, why a recursive grep command in a directory failed with the ominous Operation timed out error.

This could happen if you have very large files in a directory synchronized by iCloud Drive. For example, because you're searching through your desktop or documents folder. Usually the files will be downloaded, but due to network changes or faulty VPN connections the download could time out.

The quick fix is to manually download the directory in Finder: