Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-08-2012
Registered User
 
Join Date: Sep 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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


Code:
$ 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

http://www.unix.com/unix-dummies-que...haracters.html

but get the error


Code:
$ 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.


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

Any suggestions greatly appreciated.

Cheers SailingDreams

Moderator's Comments:
Please use code tags when posting code and data, not only for code. Thank you.

Last edited by bakunin; 09-08-2012 at 07:08 PM..
Sponsored Links
    #2  
Old 09-08-2012
Registered User
 
Join Date: Mar 2012
Location: Pune, India
Posts: 1,488
Thanks: 56
Thanked 426 Times in 422 Posts
What you want to remove?

Please provide input and desired output.
Sponsored Links
    #3  
Old 09-08-2012
alister alister is offline Forum Advisor  
Registered User
 
Join Date: Dec 2009
Posts: 2,603
Thanks: 123
Thanked 718 Times in 601 Posts
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
    #4  
Old 09-08-2012
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 3,301
Thanks: 27
Thanked 457 Times in 356 Posts
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
Sponsored Links
    #5  
Old 09-08-2012
Registered User
 
Join Date: Sep 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #6  
Old 09-08-2012
Registered User
 
Join Date: Mar 2012
Location: Pune, India
Posts: 1,488
Thanks: 56
Thanked 426 Times in 422 Posts
Try something this...

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


Code:
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..


Code:
#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


Last edited by pamu; 09-08-2012 at 10:11 PM.. Reason: added more info.
Sponsored Links
    #7  
Old 09-09-2012
Registered User
 
Join Date: Sep 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Wow, sed is incredibly powerful and complex. I tried your script and it worked! Many thanks.

Also spent time reading this tutorial. Very handy.
grymoire.com/Unix/Sed.html
Sponsored Links
Closed Thread

Tags
filename, replace character

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Reading a file and replacing char by position macastor Shell Programming and Scripting 4 04-19-2011 12:18 PM
Replacing one Char in a string of variable length nkwilliams Shell Programming and Scripting 8 07-28-2009 01:31 PM
replacing char with string phani_sree Programming 1 11-20-2006 07:57 AM
file <filename> fails kingskar UNIX for Advanced & Expert Users 2 08-14-2006 09:07 AM
replacing all 4 first upper char of every rec to lowercase ? Browser_ice UNIX for Dummies Questions & Answers 2 08-02-2006 12:06 PM



All times are GMT -4. The time now is 07:23 PM.