112 words
1 minutes
How to replace all filenames with space with underscore using a shell script

How to replace all filenames with space with underscore using a shell script#

Sometimes, you may need to replace spaces in filenames with underscores for better compatibility. This guide provides a shell script to achieve this.

Shell Script#

Here is a simple shell script to replace spaces with underscores in filenames:

#!/bin/bash

for file in *\ *; do
    mv "$file" "${file// /_}"
done

Explanation#

  • for file in *\ *; do: This loop iterates over all files with spaces in their names.
  • mv "$file" "${file// /_}": This command renames the file by replacing spaces with underscores.

Conclusion#

By using this shell script, you can easily replace spaces with underscores in filenames. This can help improve compatibility and avoid issues with file handling.


How to replace all filenames with space with underscore using a shell script
https://zxce3.net/posts/cli/how-to-replace-all-filenames-with-space-with-underscore-using-a-shell-script/
Author
Memet Zx
Published at
2022-12-12