zsh-users
 help / color / mirror / code / Atom feed
* renaming with number prefix
@ 1998-01-23 15:54 Sven Guckes
  1998-01-23 16:21 ` Thomas Koehler
                   ` (6 more replies)
  0 siblings, 7 replies; 9+ messages in thread
From: Sven Guckes @ 1998-01-23 15:54 UTC (permalink / raw)
  To: ZShell Users

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>?

Btw, "leading zeroes" would be a bonus.

Sven


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: renaming with number prefix
  1998-01-23 15:54 renaming with number prefix Sven Guckes
@ 1998-01-23 16:21 ` Thomas Koehler
  1998-01-23 16:24 ` Bruce Stephens
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: Thomas Koehler @ 1998-01-23 16:21 UTC (permalink / raw)
  To: zsh-users

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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: renaming with number prefix
  1998-01-23 15:54 renaming with number prefix Sven Guckes
  1998-01-23 16:21 ` Thomas Koehler
@ 1998-01-23 16:24 ` Bruce Stephens
  1998-01-23 16:40 ` Nate Johnston
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: Bruce Stephens @ 1998-01-23 16:24 UTC (permalink / raw)
  To: ZShell Users

guckes@math.fu-berlin.de said:
> Rename all files within a directory such that their names get a
> numeral prefix in the default sort order. 

What about:

i=1; for j in *; do mv $j $i.$j; ((i++)); done



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: renaming with number prefix
  1998-01-23 15:54 renaming with number prefix Sven Guckes
  1998-01-23 16:21 ` Thomas Koehler
  1998-01-23 16:24 ` Bruce Stephens
@ 1998-01-23 16:40 ` Nate Johnston
  1998-01-23 17:09 ` Stephen Marley
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: Nate Johnston @ 1998-01-23 16:40 UTC (permalink / raw)
  To: Sven Guckes; +Cc: ZShell Users

On Fri, 23 Jan 1998, Sven Guckes wrote:

>Problem:
>Rename all files within a directory such that their names
>get a numeral prefix in the default sort order.
>Btw, "leading zeroes" would be a bonus.

An amateur's stab at it (I don't know the extended features of
globbing....I must learn).  Again, I beg you to forgive the horrendous
inefficiencies of this approach. 

foreach FILE (`ls -1 | cat -b | tr ' ' ':' | tr '\t' ':'`)
NUMBER=`echo $FILE | cut -c 4-6 | tr ':' '0'`
NAME=`echo $FILE | cut -c 8-80`
echo "mv $NAME $NUMBER.$NAME"
end


--N.

--
Nathaniel Johnston  		 
Work: natej@mci.net		|  Internet Infrastructure Engineering 
Solaris System Administrator 	|  MCI Communications Co.




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: renaming with number prefix
  1998-01-23 15:54 renaming with number prefix Sven Guckes
                   ` (2 preceding siblings ...)
  1998-01-23 16:40 ` Nate Johnston
@ 1998-01-23 17:09 ` Stephen Marley
  1998-01-23 17:19 ` Bart Schaefer
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: Stephen Marley @ 1998-01-23 17:09 UTC (permalink / raw)
  To: zsh-users

Quoting Sven Guckes (guckes@math.fu-berlin.de):
> Problem:
> Rename all files within a directory such that their names
> get a numeral prefix in the default sort order.
> 
> Btw, "leading zeroes" would be a bonus.
> 

i=1; for f in *; do echo mv $f $(echo $i| awk '{ printf("%03d", $0)}').$f; ((i++)); done

remove the first echo to really move files.
-- 
stephen@memex.com


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: renaming with number prefix
  1998-01-23 15:54 renaming with number prefix Sven Guckes
                   ` (3 preceding siblings ...)
  1998-01-23 17:09 ` Stephen Marley
@ 1998-01-23 17:19 ` Bart Schaefer
  1998-01-28  1:21   ` nirva
  1998-01-23 17:28 ` Peter Williams
  1998-01-23 21:47 ` Ulrich Pfeifer
  6 siblings, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 1998-01-23 17:19 UTC (permalink / raw)
  To: Sven Guckes, ZShell Users

On Jan 23,  4:54pm, Sven Guckes wrote:
} Subject: renaming with number prefix
}
} 	$ ls
} 	abc bar baz foo zyxxy
} 	$ <command>
} 	$ ls
} 	1.abc 2.bar 3.baz 4.foo 5.zyxxy
} 
} So - what's that <command>?

    integer i=0; for f in *; do mv $f $[i+=1].$f; done

} Btw, "leading zeroes" would be a bonus.

How many leading zeros?  I'm guessing 3 digits total would be OK:

    typeset -Z 3 i=1 ; for f in *; do echo $f $i.$f; ((++i)); done

Too bad $[i+=1] doesn't respect the typeset flags of `i' when printing.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: renaming with number prefix
  1998-01-23 15:54 renaming with number prefix Sven Guckes
                   ` (4 preceding siblings ...)
  1998-01-23 17:19 ` Bart Schaefer
@ 1998-01-23 17:28 ` Peter Williams
  1998-01-23 21:47 ` Ulrich Pfeifer
  6 siblings, 0 replies; 9+ messages in thread
From: Peter Williams @ 1998-01-23 17:28 UTC (permalink / raw)
  To: ZShell Users

In message <19980123165404.53612@math.fu-berlin.de>, Sven Guckes writes:
 >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

I know I risk being branded a zsh heretic for posting two perl
solutions in one day, but here goes ;-)

usage: renumdir <directories>

renumdir () {
	perl -e '{
		foreach $dir (@ARGV) {
			opendir(DIR,$dir) || die "Cannot opendir $dir: $!";
			@files = sort grep(!/^\.\.?$/,readdir(DIR));
			$num = "0" x length(@files);
			chdir($dir);
			for (@files) {
				$file = $num++ . ".$_";
				if (-f $file || !rename($_,$file)) { 
					warn "Cannot rename $_ -> $file: $!\n";
				}
			}	
		}
	}' $*
}

Peter Williams <peter.williams@jpl.nasa.gov>

  "Just don't create a file called -rf.  :-)"
    -- Larry Wall in <11393@jpl-devvax.JPL.NASA.GOV>


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: renaming with number prefix
  1998-01-23 15:54 renaming with number prefix Sven Guckes
                   ` (5 preceding siblings ...)
  1998-01-23 17:28 ` Peter Williams
@ 1998-01-23 21:47 ` Ulrich Pfeifer
  6 siblings, 0 replies; 9+ messages in thread
From: Ulrich Pfeifer @ 1998-01-23 21:47 UTC (permalink / raw)
  To: ZShell Users

>>>>> "Sven" == Sven Guckes <guckes@math.fu-berlin.de> writes:

    Sven> Problem:
    Sven> Rename all files within a directory such that their names
    Sven> get a numeral prefix in the default sort order.

    Sven> Example:

    Sven> 	$ ls
    Sven> 	abc bar baz foo zyxxy
    Sven> 	$ <command>
    Sven> 	$ ls
    Sven> 	1.abc 2.bar 3.baz 4.foo 5.zyxxy

    Sven> So - what's that <command>?

perl -e 'map rename($_, ++$i.".".$_), @ARGV' *

    Sven> Btw, "leading zeroes" would be a bonus.

perl -e 'map rename($_, sprintf "%03d.%s",++$i,$_), @ARGV' *

Ulrich Pfeifer
--
OK, I could change the source, but I'd rather not, besides looking
    at it makes my head hurt
        --Richard Caley speaking about the freeWAIS-sf sources


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: renaming with number prefix
  1998-01-23 17:19 ` Bart Schaefer
@ 1998-01-28  1:21   ` nirva
  0 siblings, 0 replies; 9+ messages in thread
From: nirva @ 1998-01-28  1:21 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: guckes, zsh-users

Bart Schaefer stands accused of saying:
> On Jan 23,  4:54pm, Sven Guckes wrote:
> } Subject: renaming with number prefix
> }
> } 	$ ls
> } 	abc bar baz foo zyxxy
> } 	$ <command>
> } 	$ ls
> } 	1.abc 2.bar 3.baz 4.foo 5.zyxxy
> } 
> } So - what's that <command>?
> 
>     integer i=0; for f in *; do mv $f $[i+=1].$f; done
> 
> } Btw, "leading zeroes" would be a bonus.
> 
> How many leading zeros?  I'm guessing 3 digits total would be OK:
> 
>     typeset -Z 3 i=1 ; for f in *; do echo $f $i.$f; ((++i)); done
> 
> Too bad $[i+=1] doesn't respect the typeset flags of `i' when printing.

typeset -Z 3 i=1 ; for f in *; do echo $f $i.$f; typeset -Z 3 i=$((++i)); done

---------------------------------------------------------------------------
Danny Dulai                                           Feet. Pumice. Lotion.
http://www.ishiboo.com/~nirva/                            nirva@ishiboo.com
---------------------------------------------------------------------------


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~1998-01-28  1:22 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-01-23 15:54 renaming with number prefix Sven Guckes
1998-01-23 16:21 ` Thomas Koehler
1998-01-23 16:24 ` Bruce Stephens
1998-01-23 16:40 ` Nate Johnston
1998-01-23 17:09 ` Stephen Marley
1998-01-23 17:19 ` Bart Schaefer
1998-01-28  1:21   ` nirva
1998-01-23 17:28 ` Peter Williams
1998-01-23 21:47 ` Ulrich Pfeifer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).