Re: Stupid spaces

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx
# ICQ: 107462374 - Nick: mem
--------------------------------------------------
  - } [ Libera tu mente - Libera tu Codigo ] { -  
--------------------------------------------------

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

[Index of Archives]     [Linux Newbie]     [Audio]     [Hams]     [Kernel Newbies]     [Util Linux NG]     [Security]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Device Drivers]     [Samba]     [Video 4 Linux]     [Git]     [Fedora Users]

  Powered by Linux