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

Joseph Gleason clash at tasam.com
Tue May 27 07:34:40 PDT 2003


If someone knows how to extract the unix time that a file was modified in shell, it is easy.  I usually write a C++ program for that
step.

for file in *.jpg
do
   MODTIME="`getmodtime $file`"
   NEWNAME="`echo -n "$file"|cut -d "." -f 1`_`date -r $MODTIME +%Y%m%d%H%M%S`.jpg"
   mv $file $NEWNAME
done

Not sure if this code is right, but the -r for the date makes it use the next argument (which should be seconds from epoch) as the
time.  The +% stuff is formating how you want the output.

I have attached the code for getmodtime.

Does anyone know how to get the modification time_t from a file in shell script?  There is probably some command to do it that I
don't know about.


----- Original Message ----- 
From: "Joachim Dagerot" <joachim at dagerot.nu>
To: <freebsd-questions at freebsd.org>
Sent: 27 May, 2003 09:18
Subject: shell programming - how to write a script that renames files aftertheir last moddate?


> 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.
>
>
>
> 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
> _______________________________________________
> freebsd-questions at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe at freebsd.org"
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: getmodtime.cpp
Type: application/octet-stream
Size: 200 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-questions/attachments/20030527/5cabf8e0/getmodtime.obj


More information about the freebsd-questions mailing list