Replace Filenames in Directory with Bash Script

The following bash script converts all filenames within the /path/to/file/ directory that contain spaces to contain hyphens instead.

For example, Screen Shot 2018 09 25.png becomes Screen-Shot-2018-09-25.png after running this script.

for f in /path/to/file/*; do
mv "$f" $(echo "$f" | tr ' ' '-') ;
done

In order to run this bash script, save the file in your linux terminal to something like updateFileName.sh. Then, run the script by entering in the following command:

bash updateFileName.sh

Note: You may get error messages if some of the files don’t have spaces, and thus don’t need to convert a space to hyphen, but it will replace all filenames that do have spaces.

Leave a Reply

Your email address will not be published. Required fields are marked *