Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: renaming with number prefix



On Fri, Jan 23, 1998 at 04:54:04PM +0100, Sven Guckes wrote:

> Problem:
> Rename all files within a directory such that their names
> get a numeral prefix in the default sort order.
> 
> Example:
> 
> 	$ ls
> 	abc bar baz foo zyxxy
> 	$ <command>
> 	$ ls
> 	1.abc 2.bar 3.baz 4.foo 5.zyxxy
> 
> So - what's that <command>?

w=1 ; for i in `ls` ; do mv $i ${w}.${i} ; w=$[w+1] ; done

Not very elegant, perhaps...

> Btw, "leading zeroes" would be a bonus.
 
w=1 ; for i in `ls` ; do
if [ $w -gt 99 ] ; then
      mv $i ${w}.${i} ; w=$[w+1]
   else if [ $w -gt 9 ] ; then
      mv $i 0${w}.${i} ; w=$[w+1]
   else
      mv $i ${w}.${i} ; w=$[w+1]
   fi
fi
done

> Sven

CU,
Thomas



Messages sorted by: Reverse Date, Date, Thread, Author