How do you use files with spaces in a (Linux)for?

Summary

This incident stemmed from attempting to rename files containing spaces using a for loop that splits on whitespace. The loop consumed filenames incorrectly, causing mv to receive malformed arguments. The core issue was unsafe shell iteration over filenames.

Root Cause

  • Using for i in $(cat file), which forces word splitting and glob expansion.
  • Filenames with spaces were broken into multiple tokens.
  • mv received incomplete or misinterpreted paths.
  • Escaping attempts (sed, backslashes, quotes) failed because the shell re‑interpreted them during expansion.

Why This Happens in Real Systems

  • Shells treat whitespace as a delimiter unless explicitly told otherwise.
  • for i in $(...) is almost always unsafe for filenames.
  • Escaping inside command substitution is error‑prone because:
    • The shell performs multiple rounds of parsing.
    • Backslashes and quotes lose meaning when re‑evaluated.
  • Windows‑originated files often contain spaces, increasing the likelihood of failure.

Real-World Impact

  • Data loss if incorrect mv commands overwrite files.
  • Partial renames leaving systems in inconsistent states.
  • Broken automation pipelines when filenames are not processed atomically.
  • Hard‑to‑debug errors because the shell hides how it tokenizes arguments.

Example or Code (if necessary and relevant)

A safe, senior‑engineer‑approved solution uses find with null‑terminated strings:

find . -type f -print0 | while IFS= read -r -d '' f; do
    mv "$f" "${f// /_}"
done

How Senior Engineers Fix It

  • Use null‑terminated iteration (-print0 + read -d '') to avoid whitespace splitting.
  • Always quote variables when referencing filenames.
  • Prefer parameter expansion (${var// /_}) over fragile sed pipelines.
  • Avoid for i in $(...) for anything involving filenames.
  • Test rename logic with echo before executing destructive operations.

Why Juniors Miss It

  • They assume quoting inside the loop preserves whitespace—it doesn’t.
  • They underestimate how aggressively the shell performs word splitting.
  • They try to “escape their way out” instead of changing the iteration model.
  • They haven’t yet internalized that filenames are arbitrary byte sequences, not tokens.

Leave a Comment