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
5
  • 1
    Quote your variables
    – 123
    Commented Sep 26, 2017 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. Commented Sep 26, 2017 at 14:41
  • 1
    Consider making a habit of running your code through shellcheck.net before asking questions here. Commented Sep 26, 2017 at 14:52
  • shell check dot net - never heard of it - will do - the question was worth that !
    – capser
    Commented Sep 26, 2017 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). Commented Sep 26, 2017 at 14:55

1 Answer 1

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.

2
  • Leaving the OP using sed rather than a parameter expansion 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. Commented Sep 26, 2017 at 14:51
  • @Socowi - thanks man - layup !
    – capser
    Commented Sep 26, 2017 at 14:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.