0

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
capser
  • 2,442
  • 5
  • 42
  • 74
  • 1
    Quote your variables – 123 Sep 26 '17 at 14:41
  • You're trying to use the new filename as the first argument to `mv`, but it has to be the second one...? Also, you have to quote the filenames with spaces. – Benjamin W. Sep 26 '17 at 14:41
  • 1
    Consider making a habit of running your code through http://shellcheck.net/ before asking questions here. – Charles Duffy Sep 26 '17 at 14:52
  • shell check dot net - never heard of it - will do - the question was worth that ! – capser Sep 26 '17 at 14:55
  • BTW, consider `for i in *[[:space:]]*; do` to not even look at filenames that don't contain spaces at all. (If you use `shopt -s nullglob` before that point, that will prevent the glob from expanding to itself if there are no matches). – Charles Duffy Sep 26 '17 at 14:55

1 Answers1

2

Solution

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"

Alternative

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.

Socowi
  • 25,550
  • 3
  • 32
  • 54
  • Leaving the OP using `sed` rather than a [parameter expansion](http://wiki.bash-hackers.org/syntax/pe) in the non-`rename` case is unfortunate. Much more efficient to `mv "$jay" "${jay//[[:space:]]/}"` than to run a separate copy of `sed` for each file. – Charles Duffy Sep 26 '17 at 14:51
  • @Socowi - thanks man - layup ! – capser Sep 26 '17 at 14:56