How to replace spaces in file names using a bash script

Létrehozva: 2022. október 25. - 10:37
This post thumbnail

As Linux users, we frequently encounter issues when file commands or applications stop working because of a space in a filename. This can often be caused by file operations or habits like saving files with spaces in their names. In this article, we’ll explore several methods for renaming such files by removing or replacing the spaces in the filename.

Bash script:

#!/bin/bash

ls | while read -r FILE
do
    mv -v "$FILE" `echo $FILE | tr ' ' '_' `
done