I use cygwin on top of windows. I have a windows file that contains files with spaces. I want to get rid of the spaces between the characters and rename the files.
IMG_4089 - Copy - Copy.JPG
IMG_4089 - Copy.JPG
IMG_4092 - Copy - Copy.JPG
IMG_4092 - Copy (2).JPG
IMG_4092 - Copy.JPG
IMG_4093 - Copy - Copy.JPG
IMG_4093 - Copy (2).JPG
IMG_4093 - Copy.JPG
usually the mv command in bash works in linux
$ cat get_rid_of_spaces.sh
#!/bin/bash
IFS=$'\n' ;
for i in *
do
jay=$i ;
jay2=$(echo $i | sed -e "s/ //g")
echo $jay2 "--->" $jay
mv $jay2 $jay
sleep .5 ;
done
However I keep getting these errors.
IMG_4089-Copy-Copy.JPG ---> IMG_4089 - Copy - Copy.JPG
mv: cannot stat 'IMG_4089-Copy-Copy.JPG': No such file or directory
IMG_4089-Copy.JPG ---> IMG_4089 - Copy.JPG
mv: cannot stat 'IMG_4089-Copy.JPG': No such file or directory
IMG_4092-Copy-Copy.JPG ---> IMG_4092 - Copy - Copy.JPG
mv: cannot stat 'IMG_4092-Copy-Copy.JPG': No such file or directory
IMG_4092-Copy(2).JPG ---> IMG_4092 - Copy (2).JPG
mv: cannot stat 'IMG_4092-Copy(2).JPG': No such file or directory
IMG_4092-Copy.JPG ---> IMG_4092 - Copy.JPG
mv: cannot stat 'IMG_4092-Copy.JPG': No such file or directory
IMG_4093-Copy-Copy.JPG ---> IMG_4093 - Copy - Copy.JPG
mv: cannot stat 'IMG_4093-Copy-Copy.JPG': No such file or directory
IMG_4093-Copy(2).JPG ---> IMG_4093 - Copy (2).JPG
mv: cannot stat 'IMG_4093-Copy(2).JPG': No such file or directory
IMG_4093-Copy.JPG ---> IMG_4093 - Copy.JPG
mv: cannot stat 'IMG_4093-Copy.JPG': No such file or directory
Swap $jay
and $jay2
. The mv
command uses the first argument as the source and the second argument as the destination:
mv sourceFile destinationFile
Don't forget to quote, since you have spaces:
mv "$jay" "$jay2"
If you have rename
installed, you can replace your script with the following command:
rename 's/ //g' *
The s/ //g
means substitute (s
) space (/ /
) with the empty string (//
) globally (g
).
The wildcard *
specfies the files to be renamed, that is all files in the working directory.