truncate directory names to certain length

Ryan Byrd ryanbyrd at gmail.com
Wed May 16 09:24:09 MDT 2007


I wanted to get first post...

Here's my solution:

#!/bin/bash

for i in $( ls  );
do
src=$i
tgt=`expr substr $i 1 5`
mv $src $tgt
done


On 5/16/07, Dave Smith <dave at thesmithfam.org> wrote:
>
> Wade Preston Shearer wrote:
> > I have a directory full of directories that are all named in the
> > following manner:
> >
> > #####_alpha_name
> >
> >
> > I need them renamed to just:
> >
> > #####
> >
> >
> > Is there an easy way via the command line to truncate them to just the
> > first five characters (which would be just their ID number)?
>
> This works for me, but is probably overkill:
>
> for file in *; do
>     new_name=$(echo $file | sed 's/^\([0-9]\{5\}\).*/\1/')
>     echo Renaming "$file" to "$new_name"
>     mv -v "$file" "$new_name"
> done
>
>
> You can change the \{5\} to whatever number you want to indicate the
> number of digits to capture in a row.
>
> --Dave
>
> /*
> PLUG: http://plug.org, #utah on irc.freenode.net
> Unsubscribe: http://plug.org/mailman/options/plug
> Don't fear the penguin.
> */
>



-- 
http://www.ryanbyrd.net


More information about the PLUG mailing list