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

Re: filename manipulation



On Thu, Feb 17, 2005 at 11:16:27AM -0500, jeff elkins wrote:
> Howdy,
> 
> I need a shell script to strip X leading characters from a filename. I'm using 
> basename to change extensions, any analog of this I could use?
> 
> #!/bin/sh
> #
>   for i in *.zzz; do
>      if test -f $i; then
>          NAME=$(basename $i .zzz)  
>          echo $NAME
>          mv ${NAME}.zzz ${NAME}.xxx
>  fi
>  done
> 
> Thanks,
> 
> Jeff Elkins
> 
Hi Jeff,

while I have seen the approach Michael has taken in the other post (and
I see its intrinsic beauty), here is another take on it with sed:

--- 8< ----------------------------------------------------------------
#!/bin/bash

for b in *.xxx; do
   c=`echo $b | sed -e s/^..//g` #change nr. of dots for different
   mv $b $c                      #nr. of chars stripped
done
--- 8< ----------------------------------------------------------------

Care has to be taken for whitespace and other nastinesses in the
filename.

-- 
Andreas Rippl -- I prefer encrypted mail

Attachment: signature.asc
Description: Digital signature


Reply to: