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

Re: renaming with number prefix



In message <19980123165404.53612@xxxxxxxxxxxxxxxxx>, 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@xxxxxxxxxxxx>

  "Just don't create a file called -rf.  :-)"
    -- Larry Wall in <11393@xxxxxxxxxxxxxxxxxxxxxxx>



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