Replacing char in filename scripts fails


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Replacing char in filename scripts fails
# 1  
Old 09-08-2012
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

https://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:
Mod Comment Please use code tags when posting code and data, not only for code. Thank you.

Last edited by bakunin; 09-08-2012 at 09:08 PM..
# 2  
Old 09-08-2012
What you want to remove?

Please provide input and desired output.
# 3  
Old 09-08-2012
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
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
# 5  
Old 09-08-2012
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
# 6  
Old 09-08-2012
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 helpsSmilie

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-09-2012 at 12:11 AM.. Reason: added more info.
# 7  
Old 09-09-2012
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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with replacing a char

Hello All, I have a file as below . I want to convert the Y with numbers to H From 4, M11, P2521759, Y75,Y70,Y105,Y110,Y700,Y815,Y830,Y900,Y162,Y300, Y291,Y290,Y15,Y20, MR2716014,MR2617014, Yesterday,current 1,201012, 102032,1 11112,0 to 4, M11, P2521759,... (2 Replies)
Discussion started by: arunkumar_mca
2 Replies

2. UNIX for Dummies Questions & Answers

Replacing part of filename

Hi guys! I have quite a lot of files like all_10001_ct1212307460308.alf* and I want to get rid of the first number for all at once like: all_ct1212307460308.alf* How can I do this in the shell? (12 Replies)
Discussion started by: TimmyTiz
12 Replies

3. UNIX for Dummies Questions & Answers

Replacing multiple special chars with single char

Hi I've a string . And i need to replace set of characters with a single character Means .. or . or ... and so on should be replaced with single % character Irrespective of number of dots in between the characters , those should be replaced with single % All the above strings should be... (3 Replies)
Discussion started by: smile689
3 Replies

4. Shell Programming and Scripting

Reading a file and replacing char by position

Hi I'm looking for a way to read a text file that may contain 1000 records or more and each of these records has 460 characters. I need to read each record, and add a string of characters starting at position 256 for each record. Any suggestions using UNIX shell scripting. (4 Replies)
Discussion started by: macastor
4 Replies

5. Shell Programming and Scripting

Replacing one Char in a string of variable length

Hi all, I am trying to find the best way of making a change to 1 char in a string, the string can be between 1 and 14 characters. I am reading a line in from a file which contains 012341231231:2:102939283:NNN: Require :NBN: 012838238232:3:372932:NNN: Require :NNB: I need to change 1 N or a... (8 Replies)
Discussion started by: nkwilliams
8 Replies

6. UNIX for Dummies Questions & Answers

How to copy/move to a file with a special character as the 1st char in the filename?

I am trying to create files with special characters in its filenames for testing purposes. This is on a Linux RHEL4 but this should also be applicable on a Unix shell. I am able to create files with special characters in the filenames...e.g. cp -pv foo.gif \*special.gif cp -pv foo.gif \... (6 Replies)
Discussion started by: sqa777
6 Replies

7. Shell Programming and Scripting

Scripts fails if you change its code during the execution.

So a script is working properly (tested many times) , then you add a new fine piece of code ,finaly its fails generally with a syntax error at the last line of the script. :confused:... does anybody why this happens? >uname -a HP-UX test... (4 Replies)
Discussion started by: Klashxx
4 Replies

8. Programming

replacing char with string

how we can replace char with a string example char *a="a.s" so finally what i ant to do raplace a with ant and s sree so in my array a i want to store the value as "ant.sree" thank u in advance (1 Reply)
Discussion started by: phani_sree
1 Replies

9. UNIX for Advanced & Expert Users

file <filename> fails

Hi Friends I tried to find the file type of a file on my linux machine by using the following command file xy.txt instead of getting the file type i got following message file: couldnot found any magic files! What is wrong on this how can i find the file type? Regards Shekar (2 Replies)
Discussion started by: kingskar
2 Replies

10. UNIX for Dummies Questions & Answers

replacing all 4 first upper char of every rec to lowercase ?

I have a file where some records have been updated the wrong way and need to fix it quickly since the amount can be alot. Every record where any of the first 4 characters are in upper case need to be changed to lowercase. Records can have '#' in position-1 for comments. These musn't be... (2 Replies)
Discussion started by: Browser_ice
2 Replies
Login or Register to Ask a Question