How to Remove Trailing Spaces from Multiple Files
Below are concise, practical methods for removing trailing spaces across many files—choose based on your OS, tools available, and comfort level.
1) Use command-line tools (Linux/macOS/WSL)
- Quick (in-place) using sed:
- Single file:
sed -i “ -e ’s/[[:space:]]+\(//' file.txt</code> (macOS) or <code class="qlv4I7skMF6Meluz0u8c wZ4JdaHxSAhGy1HoNVja _dJ357tkKXSh_Sup5xdW">sed -i -e 's/[[:space:]]\+\)//’ file.txt(Linux). - Multiple files in directory:
- Bash:
for f in.txt; do sed -i -e ’s/[[:space:]]+\(//' "\)f”; done - Recursively:
find . -type f -name ‘*.txt’ -exec sed -i -e ’s/[[:space:]]+\(//' {} +</code></li> </ul> </li> </ul> </li> <li>Using perl (portable): <ul> <li><code class="qlv4I7skMF6Meluz0u8c wZ4JdaHxSAhGy1HoNVja _dJ357tkKXSh_Sup5xdW">perl -pi -e 's/\s+\)//’ .txtor recursive:find . -type f -name ‘.txt’ -exec perl -pi -e ’s/\s+\(//' {} +</code></li> </ul> </li> </ul> <h3>2) Use GNU awk</h3> <ul> <li>Single command recursive: `find . -type f -name '*.txt' -exec awk '{ sub(/[ \t\r\n]+\)/, “”); print }’ {} > /tmp/tmp && mv /tmp/tmp {} \;Get-ChildItem -Path . -Filter .txt -Recurse | ForEach-Object { (Get-Content \(<em>.FullName) | ForEach-Object { \) -replace ‘\s+\(','' } | Set-Content \)_.FullName }
<ul> <li>Note: create safe backups or use more robust scripting to avoid data loss.</li> </ul> </li> </ul> <h3>3) Use ripgrep + xargs (fast)</h3> <ul> <li><code class="qlv4I7skMF6Meluz0u8c wZ4JdaHxSAhGy1HoNVja _dJ357tkKXSh_Sup5xdW">rg -l --hidden --glob '!node_modules' '\s+$' | xargs -d '\n' -r sed -i -e 's/\s\+$//'</code></li> </ul> <h3>4) Windows (PowerShell)</h3> <ul> <li>Single file: <code class="qlv4I7skMF6Meluz0u8c wZ4JdaHxSAhGy1HoNVja _dJ357tkKXSh_Sup5xdW">Get-Content file.txt | ForEach-Object { $_ -replace '\s+$','' } | Set-Content file.txt</code></li> <li>Multiple files: <ul> <li></li> </ul> </li> </ul> <h3>5) Editor/IDE options</h3> <ul> <li>VS Code: open folder → Search (regex) <code class="qlv4I7skMF6Meluz0u8c wZ4JdaHxSAhGy1HoNVja _dJ357tkKXSh_Sup5xdW">\s+$</code> → Replace with empty → Replace in Files. Or use "Trim Trailing Whitespace" on save (settings).</li> <li>Sublime Text: Find with regex <code class="qlv4I7skMF6Meluz0u8c wZ4JdaHxSAhGy1HoNVja _dJ357tkKXSh_Sup5xdW">\s+$</code> across files; or use "trim_trailing_white_space_on_save".</li> <li>Emacs: <code class="qlv4I7skMF6Meluz0u8c wZ4JdaHxSAhGy1HoNVja _dJ357tkKXSh_Sup5xdW">M-x delete-trailing-whitespace</code> (use in dired or multiple buffers).</li> <li>Vim: <code class="qlv4I7skMF6Meluz0u8c wZ4JdaHxSAhGy1HoNVja _dJ357tkKXSh_Sup5xdW">:%s/\s\+$//e</code> for current buffer; to apply to many files: <code class="qlv4I7skMF6Meluz0u8c wZ4JdaHxSAhGy1HoNVja _dJ357tkKXSh_Sup5xdW">argdo %s/\s\+$//e | update</code>.</li> </ul> <h3>6) Git-aware approaches</h3> <ul> <li>To remove trailing spaces in tracked files and commit: <ul> <li>git ls-files ‘.txt’ | xargs sed -i -e ’s/[[:space:]]+$//’ && git add -u && git commit -m “Trim trailing whitespace”`
- Bash:
- Prevent future issues: add
.gitattributeswith* text=autoand enable hooks or editorconfig (.editorconfig settingtrim_trailing_whitespace = true).
Safety and best practices
- Make backups before batch edits:
cp -a project project.bakor use-i/create.bakin sed/perl. - Test commands on a small sample first.
- Use version control to review changes.
- Prefer tools that preserve file permissions and binary detection; avoid running on binary files (filter by extension or use
file/rg).
If you want, I can generate a ready-to-run script for your OS and target file types — tell me your OS and file extensions.
- Single file:
Leave a Reply