How to loop through a list of files with spaces and rename them

  bash, linux

In this example, we have a directory with a lot of files which have no extension. We need to add “.jpg” at the end of each file name. We’re using Git Bash in Windows because we can do it in 1 line.

First, do this so that files with spaces in the name are not broken into pieces:

1. save the settings for when we’re done

OIFS="$IFS"
 
IFS=$'\n'

2. run the rename script, which takes all files and adds .jpg to the end of the file name:

for file in `ls`; do mv "$file" "$file.jpg"; done

3. put the settings back

IFS="$OIFS"