Stupid spaces

Stupid spaces

am 12.02.2004 22:08:04 von Scott Taylor

Hello all,

I've done this before, but so long ago I can't find it again. I have a
bunch of files with spaces in them and I want to rename them with the
spaces removed.

I have a rename command that came with RH7.2 but doesn't do the job
rename 's/\ //g' *
does nothing in bash

So I wrote a script many moons ago to do this but I can't remember which
server it was on, let alone how I did it. Something with tr and mv methinks.

anyone?

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: Stupid spaces

am 12.02.2004 22:23:16 von adhir

This is a multi-part message in MIME format.
--------------040905060209030008010102
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

The attached rename.pl is invaluable for such tasks.

Use it exactly like your bash rename attempt. You can use pretty much
any perl in-place-modifier construct - s/X/Y/, tr/A-Z/a-z/
(upper->lower), etc.

Not sure where I found this originally, but it's extremely handy.

Al

Scott Taylor wrote:

> Hello all,
>
> I've done this before, but so long ago I can't find it again. I have
> a bunch of files with spaces in them and I want to rename them with
> the spaces removed.
>
> I have a rename command that came with RH7.2 but doesn't do the job
> rename 's/\ //g' *
> does nothing in bash
>
> So I wrote a script many moons ago to do this but I can't remember
> which server it was on, let alone how I did it. Something with tr and
> mv methinks.
>
> anyone?
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-admin" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>

--
Alok K. Dhir
Symplicity Corporation
http://solutions.symplicity.com
703 351 6987 (w) | 703 351-6357 (f)


--------------040905060209030008010102
Content-Type: text/plain;
name="rename.pl"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="rename.pl"

#!/usr/bin/perl

if (@ARGV == 0) {
die "usage: rename PATTERN FILESPEC\n";
}

$pattern=shift;

foreach (@ARGV) {
my $oldname=$_;
my $newname=$_;
eval ("\$newname=~$pattern");
rename($oldname, $newname);
}


--------------040905060209030008010102--
-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: Stupid spaces

am 12.02.2004 22:39:02 von Yu Chen

On RH, it does have a command 'rename', but not the way you used it.
Just type
rename ' ' '' *
couple of times to get rid of all the spaces

Chen


On Thu, 12 Feb 2004, Scott Taylor wrote:

> Hello all,
>
> I've done this before, but so long ago I can't find it again. I have a
> bunch of files with spaces in them and I want to rename them with the
> spaces removed.
>
> I have a rename command that came with RH7.2 but doesn't do the job
> rename 's/\ //g' *
> does nothing in bash
>
> So I wrote a script many moons ago to do this but I can't remember which
> server it was on, let alone how I did it. Something with tr and mv methinks.
>
> anyone?
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-admin" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: Stupid spaces

am 12.02.2004 23:18:26 von Scott Taylor

At 01:39 PM 02/12/2004, Yu Chen wrote:
>On RH, it does have a command 'rename', but not the way you used it.
>Just type
>rename ' ' '' *
>couple of times to get rid of all the spaces

Oh! Hehe, looks like a DOS command. LOL

Cheers.

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: Stupid spaces

am 12.02.2004 23:19:43 von Scott Taylor

At 01:23 PM 02/12/2004, Alok K. Dhir wrote:
>The attached rename.pl is invaluable for such tasks.
>
>Use it exactly like your bash rename attempt. You can use pretty much any
>perl in-place-modifier construct - s/X/Y/, tr/A-Z/a-z/ (upper->lower), etc.
>
>Not sure where I found this originally, but it's extremely handy.
>
>
>#!/usr/bin/perl
>
>if (@ARGV == 0) {
> die "usage: rename PATTERN FILESPEC\n";
>}
>
>$pattern=shift;
>
>foreach (@ARGV) {
> my $oldname=$_;
> my $newname=$_;
> eval ("\$newname=~$pattern");
> rename($oldname, $newname);
>}

Very. Thanks. Not a BASH script, but works great.

Thanks.

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: Stupid spaces

am 13.02.2004 03:15:24 von rich+ml

How about:

for x in *; do mv $x ${x// /_}; done

On Thu, 12 Feb 2004, Scott Taylor wrote:

> Date: Thu, 12 Feb 2004 13:08:04 -0800
> From: Scott Taylor
> To: linux-admin@vger.kernel.org
> Subject: Stupid spaces
>
> Hello all,
>
> I've done this before, but so long ago I can't find it again. I have a
> bunch of files with spaces in them and I want to rename them with the
> spaces removed.
>
> I have a rename command that came with RH7.2 but doesn't do the job
> rename 's/\ //g' *
> does nothing in bash
>
> So I wrote a script many moons ago to do this but I can't remember which
> server it was on, let alone how I did it. Something with tr and mv methinks.
>
> anyone?
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-admin" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: Stupid spaces

am 13.02.2004 05:07:57 von Emiliano Castagnari

El [ Thu 12, Feb 04 - 13:08 ] , Scott Taylor expreso:
> Hello all,
>
> I've done this before, but so long ago I can't find it again. I have a
> bunch of files with spaces in them and I want to rename them with the
> spaces removed.
>
> I have a rename command that came with RH7.2 but doesn't do the job
> rename 's/\ //g' *
> does nothing in bash
>
> So I wrote a script many moons ago to do this but I can't remember which
> server it was on, let alone how I did it. Something with tr and mv
> methinks.
>
> anyone?

Here you go, my simple aproach using the virtudes of bash scripting:

-- cut --
#!/bin/bash

DIR=$1
TOKEN="_"

cd $DIR;

# Here, we first wipe out spaces (actually, we replace them), otherwise
# a name like "my tex file.tex" would produce 3 elements, "my", "tex" and "file.tex"
for file in $(ls | sed -r 's/ /_:_/g'); do

new_name="$(echo $file | sed -r "s/_:_/$TOKEN/g")"
name="$(echo $file | sed -r 's/_:_/ /g')"

if [ -f "$name" ]; then
echo " Moving $name => $new_name"
mv "$name" "$new_name"
else
echo "$file does not exists (or element was not splited correctly in command: ls | tr ' ' ':' )"
fi

done

echo " done ..."
-- /cut --


Hope you find this usefull

"...
Master Foo once said to a visiting programmer:
There is more Unix-nature in one line of shell script than there is in ten thousand lines of C.
...."

Rootles Root - Eric S. Raymond.
http://catb.org/~esr/writings/unix-koans/ten-thousand.html

--
Emiliano Castagnari

# Debian Sarge - GNU/Linux - Athos 2.6.2 #
# JID: pretorian@jabber.sk
# ICQ: 107462374 - Nick: mem
--------------------------------------------------
- } [ Libera tu mente - Libera tu Codigo ] { -
--------------------------------------------------

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: Stupid spaces

am 13.02.2004 07:51:46 von Agus Budy Wuysang

Scott Taylor wrote:
> Hello all,
>
> I've done this before, but so long ago I can't find it again. I have a
> bunch of files with spaces in them and I want to rename them with the
> spaces removed.
>
> I have a rename command that came with RH7.2 but doesn't do the job
> rename 's/\ //g' *
> does nothing in bash

rename doesn't work like DOS' rename, but:

rename old_pattern new_pattern files...

> So I wrote a script many moons ago to do this but I can't remember which
> server it was on, let alone how I did it. Something with tr and mv
> methinks.
>
> anyone?

If you're lucky enough to have bash V2, use (faster):

for f in *;do mv -i "$f" "${f// /}";done

otherwise:

for f in *;do mv -i "$f" "$(echo $f|tr -d ' ')";done

If your actual intention of getting rid of spaces were
to get around difficulties in shell command globbing/parsing,
you should learn more about "find -print0" & "xargs -0" instead.

--
+-R-| Mozilla 1.6 Gecko20040116 |-H-| Powered by Linux 2.4.x |-9-+
|/v\ Agus Budy Wuysang MIS Department |
| | Phone: +62-21-344-1316 ext 317 GSM: +62-816-1972-051 |
+------------| http://www.fasw.co.id/person/supes/ |-------------+
-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: Stupid spaces

am 13.02.2004 17:33:33 von Scott Taylor

At 06:15 PM 02/12/2004, rich+ml@lclogic.com wrote:
>How about:
>
> for x in *; do mv $x ${x// /_}; done

That's kinda what I was thinking, only problem with that is, the file named
"blah blah.p" will cause an error as bash/sh/ksh will think it is two files.
mv: cannot stat `blah': No such file or directory
mv: cannot stat `blah.p': No such file or directory

even tried using `ls -c1` same thing. Worse thing is I know I did this
before, so long ago, maybe I used Perl after all. Hmm...

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: Stupid spaces

am 13.02.2004 17:56:01 von Scott Taylor

At 08:07 PM 02/12/2004, Emiliano Castagnari wrote:
>El [ Thu 12, Feb 04 - 13:08 ] , Scott Taylor expreso:
> > Hello all,
> >
> > I've done this before, but so long ago I can't find it again. I have a
> > bunch of files with spaces in them and I want to rename them with the
> > spaces removed.
> >
> > I have a rename command that came with RH7.2 but doesn't do the job
> > rename 's/\ //g' *
> > does nothing in bash
> >
> > So I wrote a script many moons ago to do this but I can't remember which
> > server it was on, let alone how I did it. Something with tr and mv
> > methinks.
> >
> > anyone?
>
>Here you go, my simple aproach using the virtudes of bash scripting:

Interesting. Only it doesn't "remove" the spaces it simply replaces them
with something else.


-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: Stupid spaces

am 13.02.2004 18:15:36 von Scott Taylor

At 10:51 PM 02/12/2004, Agus Budy Wuysang wrote:
>Scott Taylor wrote:
>
>If you're lucky enough to have bash V2, use (faster):
>
>for f in *;do mv -i "$f" "${f// /}";done
>
>otherwise:
>
>for f in *;do mv -i "$f" "$(echo $f|tr -d ' ')";done

This last works, it also works in more then just bash so I like it best. I
have more then one OS that store Windows files for users. :)
I don't remember using echo in it last time but, it works...

Cheers.

Scott.

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: Stupid spaces

am 13.02.2004 18:26:04 von rich+ml

Works for me, however I did forget to quote $x in original example.

[rich@deadrat ~/test]$ ls -l
total 0
-rw-rw-r-- 1 rich rich 0 Feb 13 09:19 blah blah.p
-rw-rw-r-- 1 rich rich 0 Feb 13 09:19 this is a test

[rich@deadrat ~/test]$ for x in *; do mv "$x" ${x// /_}; done

[rich@deadrat ~/test]$ ls -l
total 0
-rw-rw-r-- 1 rich rich 0 Feb 13 09:19 blah_blah.p
-rw-rw-r-- 1 rich rich 0 Feb 13 09:19 this_is_a_test

[rich@deadrat ~/test]$ bash --version
GNU bash, version 2.05.8(1)-release (i386-redhat-linux-gnu)
Copyright 2000 Free Software Foundation, Inc.


On Fri, 13 Feb 2004, Scott Taylor wrote:

> Date: Fri, 13 Feb 2004 08:33:33 -0800
> From: Scott Taylor
> To: linux-admin@vger.kernel.org
> Subject: Re: Stupid spaces
>
> At 06:15 PM 02/12/2004, rich+ml@lclogic.com wrote:
> >How about:
> >
> > for x in *; do mv $x ${x// /_}; done
>
> That's kinda what I was thinking, only problem with that is, the file named
> "blah blah.p" will cause an error as bash/sh/ksh will think it is two files.
> mv: cannot stat `blah': No such file or directory
> mv: cannot stat `blah.p': No such file or directory
>
> even tried using `ls -c1` same thing. Worse thing is I know I did this
> before, so long ago, maybe I used Perl after all. Hmm...
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-admin" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: Stupid spaces

am 13.02.2004 18:33:35 von Scott Taylor

At 09:26 AM 02/13/2004, rich+ml@lclogic.com wrote:
>Works for me, however I did forget to quote $x in original example.

That would do it.
for x in *; do mv "$x" ${x// /}; done
removes the spaces, but only works in bash 2. :(


-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

Re: Stupid spaces

am 13.02.2004 20:13:11 von urgrue

well since we're all sharing space-removal tactics, here's my version: a
script i made to remove space (and other chars i dont like) from all files
in the current dir:
ls -1A > /tmp/desp.tmp
while read line
do
new=`echo -n "$line" | tr [:space:] _ | tr -d
[=\(=][=\)=][=]=][=[=][=!=][=\'=][=,=][=?=] | tr [:upper:] [:lower:]`
if [ "$new" = "$line" ] ; then
continue
fi
mv -i -- "$line" "$new"
done < /tmp/desp.tmp
rm /tmp/desp.tmp



>>At 09:26 AM 02/13/2004, rich+ml@lclogic.com wrote:
>>>Works for me, however I did forget to quote $x in original example.
>>
>>That would do it.
>>for x in *; do mv "$x" ${x// /}; done
>>removes the spaces, but only works in bash 2. :(
>>
>>
>>-
>>To unsubscribe from this list: send the line "unsubscribe linux-admin" in
>>the body of a message to majordomo@vger.kernel.org
>>More majordomo info at http://vger.kernel.org/majordomo-info.html

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html