|
Home > Archive > Unix questions > February 2004 > renaming files with a space in the filename
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
renaming files with a space in the filename
|
|
| entropic 2004-02-23, 12:33 pm |
| I've got a funny problem on my hands: my daughter sent me a CD with
hundreds of pictures burned on a Mac that ended up having two-word
filenames with a space; their general form is
100-0051_IMG.JPG copy
I'm trying to rename them on my HD with a ksh script (I use MKS
Toolkit on W2K), but I can't figure out how to handle that space in
each filename. A typical script I've tried was
for i in '* copy'; do cp $i `echo $i | sed 's/ copy//p'`; done
Also, a lot of variations of the '* copy' part, double quotes,
character classes etc etc, but nothing seems to work. Only the octal
representation of space is left untried, but I don't even know what it
is... <G>.
I'd appreciate a hint or two on how to handle this tough space
character.
z.entropic
| |
| Tim Heaney 2004-02-23, 4:35 pm |
| subPlanck@excite.com (entropic) writes:
> I've got a funny problem on my hands: my daughter sent me a CD with
> hundreds of pictures burned on a Mac that ended up having two-word
> filenames with a space; their general form is
>
> 100-0051_IMG.JPG copy
>
> I'm trying to rename them on my HD with a ksh script (I use MKS
> Toolkit on W2K), but I can't figure out how to handle that space in
> each filename. A typical script I've tried was
>
> for i in '* copy'; do cp $i `echo $i | sed 's/ copy//p'`; done
>
> Also, a lot of variations of the '* copy' part, double quotes,
> character classes etc etc, but nothing seems to work. Only the octal
> representation of space is left untried, but I don't even know what it
> is... <G>.
I'm not familiar with MKS, but ksh should recognize a backslash as an
escape, so that
for i in *\ copy; do cp "$i" `echo $i | sed 's/ copy//'`; done
should do what you describe. Actually, to rename them, you'd want to
use mv rather than cp
for i in *\ copy; do mv "$i" `echo $i | sed 's/ copy//'`; done
Alternatively, you could use awk to grab all the stuff before the
space
for i in *\ copy; do mv "$i" `echo $i | awk '{print $1}'`; done
You could also use PERL to do it
perl -e 'opendir D, q/./ or die; for (grep / copy$/,
readdir D){$n=$_; $n=~s/ copy$//; rename $_, $n}'
One of the joys of using Linux is the terrific rename command, with
which you could do the above with
rename \ copy '' *\ copy
Schweet!
For future reference, an ASCII space is 40 octal
$ PERL -e 'printf "%03o\n", ord " "'
040
I hope this helps,
Tim
| |
|
|
| entropic 2004-02-24, 12:34 am |
| pjacklam@online.no (Peter J. Acklam) wrote in message news:<y8qt53rm.fsf@online.no>...
> subPlanck@excite.com (entropic) wrote:
>
>
> for i in *\ copy; do cp -- "$i" "${i% copy}"; done
>
> Peter
Great thanks to both Tim and Peter! The thought of escaping the space
with a backslash did cross my mind, but the idea seemed too crazy in
this instance... Next time, I'll try. <G>
z.entropic
|
|
|
|
|