Replacing char in filename scripts fails

Hi

I'm trying to remove what I "think" is a bad character. How I got the bad character is when I downloaded jpgs onto my PC and then renamed the files using windows explorer. In cygwin, the files look like

$ dir -l
total 7840
----------+ 1 None 3647968 Jul 21 08:41 2012-07-21\ (1).JPG
----------+ 1 None 3635983 Jul 21 10:29 2012-07-21\ (2).JPG
----------+ 1 None  738515 Jul 21 10:34 2012-07-21\ (3).JPG

I tried the script from this thread

but get the error

$ sh replaceChar.sh
mv: target `(1).JPG' is not a directory
mv: target `(2).JPG' is not a directory
mv: target `(3).JPG' is not a directory

I tested the script below and it works for other characters, but not the "\" which I think is causing the problem.

#!/bin/bash
for file in *
do
  mv "$file" $(echo $file | sed -e "s/[\%]/_/g")
done

Any suggestions greatly appreciated.

Cheers SailingDreams

What you want to remove?

Please provide input and desired output.

The unquoted whitespace in the filename is interpreted by the shell as a field separator. mv sees more than two arguments and expects the last one to be a directory.

Regards and welcome to the forum,
Alister

You might be interested in a little How-To i just wrote:

How to manage file names with special characters

I hope this helps.

bakunin

Hi Pamu

I'd like to remove the white space, and then my plan is to modify the windows explorer names so that I can add more files

Eg. I'd like to change
<quote>----------+ 1 None 3647968 Jul 21 08:41 2012-07-21\ (1).JPG
----------+ 1 None 3635983 Jul 21 10:29 2012-07-21\ (2).JPG
----------+ 1 None 738515 Jul 21 10:34 2012-07-21\ (3).JPG</quote>

to
<quote>
----------+ 1 None 3647968 Jul 21 08:41 2012-07-21(0100).JPG
----------+ 1 None 3635983 Jul 21 10:29 2012-07-21(0200).JPG
----------+ 1 None 738515 Jul 21 10:34 2012-07-21(0300).JPG</quote>

with the zeros added, I can add other photos like
<quote>----------+ 1 None 3647968 Jul 21 08:41 2012-07-21(0101).JPG</quote>

and they will be in proper chronological sequence.

Hi Alister

Thanks for the welcome.

How do I modify my script to replace spaces?

Many thanks

Try something this...

Assuming you have files with this format only. Here script is adding 2 more zeros at the number in the bracket

for file in *
do
new_file_name=$(echo "$file" | sed -e 's/\\ //g' -e 's/([0-9]*/&00/g' )
mv "$file" "$new_file_name"  
done

Hope this helps:)

One more -

Assuming you have two directories one is input and second destination. And assuming you have image names like 1,2,3,.... so on..

#Count gives you latest digit/number of a image you have...
Count=$(ls /Dest_dir/* | awk -F "[()]" '{ print $2}' | sort | tail -1)

ls /input_dir/* | while read line
do
let Count+=1        #Increment the counter
new_file_nanme=$(echo "$file" | sed -e 's/\\ //g' -e 's/([0-9]*)/('"$Count"')/g')
mv "$file" "$new_file_nanme"  
done

Wow, sed is incredibly powerful and complex. I tried your script and it worked! Many thanks.

Also spent time reading this tutorial. Very handy.