shell programming - how to write a script that renames files after their last moddate?

Eduardo Viruena Silva mrspock at esfm.ipn.mx
Tue May 27 19:45:26 PDT 2003


On Tue, 27 May 2003, Vince Hoffman wrote:

>
> > This is certainly not freeBSD specific and probably I'm annoying
> > someone for being off-topic but please be patient and hint me on where
> > to find good resources in shell-programming.
> >
> http://www.shelldorado.com/
> isnt bad. otherwise comp.unix.shell is always worth a look.
>
>
>
> >
> > I could use some help in writing a script that renames all files in a
> > directory tree to the files last modified date, example usage:
> >
> > > daterename "Img_" *.jpg
> >
> > the command above will rename all *.jpg files to "Imag_<date>".jpg


=============================
#!/bin/tcsh

set prefix=$1
shift

while ( "$1" != "" )
    set ext=`expr "$1" : ".*\(\..*\)"`
    set newname=$prefix`stat -f "%Sm" -t "%F_%H:%M:%S"`
    echo moving  $1  to  $newname$ext
    mv $1 $newname$.ext
    shift
end
===========================

BUGS:

I'm not sure "stat" works in 4.x

If two [or more] files are created at exactly the same time,
the one with the last name  --in lexicographic order--
will overwrite the others.


Hope it helps.


More information about the freebsd-questions mailing list