System Administration

Rename a file with its modification date

 
SOLVED
Go to solution
whairst
Occasional Advisor

Rename a file with its modification date

I'd like to routinely rename a bunch of files, using their modification date as part of the filename. This process might or might not involve other commands on the files as well (like gzip). Ideally it would start with something to narrow down the files, like:

find . -name "*.abc" -mtime +1 -type f

then rename the file to a timestamp format. yyyy-mm-dd-hh-mm.abc would work, but some flexibility would be nice.

HPUX 10.20, no option of upgrading or adding additional software, so no "stat" or "date -r".

After much 'net searching, I can't seem to come up with a way of getting a modification date into a script, given the OS constraints. Any ideas?

 

6 REPLIES 6
Steven Schweda
Honored Contributor

Re: Rename a file with its modification date

> HPUX 10.20, no option of upgrading or adding additional software, so
> no "stat" or "date -r".

   That restriction might be too strict.  HP-UX 10.20 is too old/lame.

   At the shell level, you could extract some kind of date+time from
"ls -l" output, but the sub-genius design of "ls" doesn't make it easy
to get the same format over a wide date range.

   I might try to get a GNU "ls", and use "--time-style".  Failing that,
I might write a short C program using stat() and strftime(), or, if
strftime() isn't there, something like localtime() to get a struct tm,
and format it myself.

   I suspect that (long ago) someone wrote something suitable as
freeware, but that, too, would be "additional software".

> [...] given the OS constraints.

   And your own?

whairst
Occasional Advisor

Re: Rename a file with its modification date

That restriction might be too strict.  HP-UX 10.20 is too old/lame.

This is for industry, not hobby; I can't control the change restrictions that are in place.

I already considered ll (ls -l), but as you mentioned, the formats vary depending on how old the file is. Adding GNU ls is, as previously stated, not an option. Source code of old freeware would probably be fine (can clearly verify the lack of side effects). Looks like I also have Perl 4.0 to play with, if that helps.

Steven Schweda
Honored Contributor

Re: Rename a file with its modification date

> [...] Source code of old freeware would probably be fine [...]

   See "I might write a short C program [...]", above.

> [...] Looks like I also have Perl 4.0 to play with, if that helps.

   I don't do enough with Perl to know anything, but I seem to recall
seeing some relevant Perl suggestions when I tried a Web search for
terms like:

      file modification time

Steven Schweda
Honored Contributor
Solution

Re: Rename a file with its modification date

> [...] I might write a short C program [...]

   The closest I have to HP-UX 10.20 is 11.11, but I did use the (lame)
bundled C compiler (hence the conditional prototype), which is about as
handicapped as I can get easily.

dy $ cc -o mdt -DPROTO mdt.c
(Bundled) cc: "mdt.c", line 10: error 1705: Function prototypes are an ANSI feature.

dy $ cc -o mdt mdt.c
dy $

dy $ cp -p mdt.c mdt_gmt_` ./mdt mdt.c `.c

dy $ ls -l mdt.c mdt_gmt_*
-rw-rw-rw-   1 sms        users          796 May  7 22:07 mdt.c
-rw-rw-rw-   1 sms        users          796 May  7 22:07 mdt_gmt_2022-05-08_03-07-50.c

dy $ cat mdt.c
/* Print mod date+time of file specified as parameter. */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>

#ifdef PROTO
int main( int argc, char **argv)
#else
int main( argc, argv)
 int argc;
 char **argv;
#endif
{
  int sts;
  struct stat stat_buf;
  struct tm *mtm;

  if (argc != 2)
  {
    sts = EINVAL;
  }
  else
  {
    sts = stat( argv[ 1], &stat_buf);
    if (sts != 0)
    {
      sts = errno;
    }
    else
    {
      mtm = gmtime( &stat_buf.st_mtime);
      if (mtm != NULL)
      {
        fprintf( stdout, "%04d-%02d-%02d_%02d-%02d-%02d",
         (1900+ mtm-> tm_year),
         (1+ mtm-> tm_mon),
         mtm-> tm_mday,
         mtm-> tm_hour,
         mtm-> tm_min,
         mtm-> tm_sec);
      }
    }
  }
  exit( sts);
}

 

whairst
Occasional Advisor

Re: Rename a file with its modification date

That worked a treat. My only modification was to change it from gmtime to localtime (now that I know those functions exist!) so it returns the local-time timestamp instead of the GMT one.

 

Thank you very much.

Steven Schweda
Honored Contributor

Re: Rename a file with its modification date

> [...] My only modification was to change it from gmtime to localtime
> [...]

   I started with localtime(), but changed it after I rediscovered the
name of gmtime().  (It's been a while since I had used either.)  An
advantage of gmtime() is that its result is independent of time zone,
daylight saving, and so on, because UTC is what's used in the file
system (in most places).  But otherwise they're pretty easily swapped,
as you've seen.  And, of course, you can change the formatting in the
fprintf() format string.

   Glad to hear that it worked for you.