|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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
Last edited by bakunin; 09-08-2012 at 07:08 PM.. |
| Sponsored Links | ||
|
|
#2
|
|||
|
|||
|
What you want to remove?
Please provide input and desired output. |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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"
doneLast edited by pamu; 09-08-2012 at 10:11 PM.. Reason: added more info. |
| Sponsored Links | |
|
|
#7
|
|||
|
|||
|
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 | ||
|
![]() |
| Tags |
| filename, replace character |
| Thread Tools | Search this Thread |
| 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 |
|
|