- Community Home
- >
- Servers and Operating Systems
- >
- Operating System - HP-UX
- >
- System Administration
- >
- Re: Rename a file with its modification date
-
-
Forums
- Products
- Servers and Operating Systems
- Storage
- Software
- Services
- HPE GreenLake
- Company
- Events
- Webinars
- Partner Solutions and Certifications
- Local Language
- China - 简体中文
- Japan - 日本語
- Korea - 한국어
- Taiwan - 繁體中文
-
- Advancing Life & Work
- Advantage EX
- Alliances
- Around the Storage Block
- HPE Ezmeral: Uncut
- OEM Solutions
- Servers & Systems: The Right Compute
- Tech Insights
- The Cloud Experience Everywhere
- HPE Blog, Austria, Germany & Switzerland
- Blog HPE, France
- HPE Blog, Italy
- HPE Blog, Japan
- HPE Blog, Middle East
- HPE Blog, Latin America
- HPE Blog, Russia
- HPE Blog, Saudi Arabia
- HPE Blog, South Africa
- HPE Blog, UK & Ireland
- HPE Blog, Poland
-
Blogs
- Advancing Life & Work
- Advantage EX
- Alliances
- Around the Storage Block
- HPE Blog, Latin America
- HPE Blog, Middle East
- HPE Blog, Saudi Arabia
- HPE Blog, South Africa
- HPE Blog, UK & Ireland
- HPE Ezmeral: Uncut
- OEM Solutions
- Servers & Systems: The Right Compute
- Tech Insights
- The Cloud Experience Everywhere
-
Information
- Community
- Welcome
- Getting Started
- FAQ
- Ranking Overview
- Rules of Participation
- Tips and Tricks
- Resources
- Announcements
- Email us
- Feedback
- Information Libraries
- Integrated Systems
- Networking
- Servers
- Storage
- Other HPE Sites
- Support Center
- Aruba Airheads Community
- Enterprise.nxt
- HPE Dev Community
- Cloud28+ Community
- Marketplace
-
Forums
-
Forums
-
Blogs
-
Information
-
English
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
05-05-2022 12:02 PM - last edited on 05-09-2022 08:16 AM by support_s
05-05-2022 12:02 PM - last edited on 05-09-2022 08:16 AM by support_s
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?
Solved! Go to Solution.
- Tags:
- Operating System
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
05-05-2022 02:03 PM
05-05-2022 02:03 PM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
05-06-2022 04:44 AM
05-06-2022 04:44 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
05-07-2022 06:29 AM
05-07-2022 06:29 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
05-07-2022 08:35 PM
05-07-2022 08:35 PM
Solution> [...] 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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
05-09-2022 09:32 AM
05-09-2022 09:32 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
05-09-2022 10:55 AM
05-09-2022 10:55 AM
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.
Hewlett Packard Enterprise International
- Communities
- HPE Blogs and Forum
© Copyright 2022 Hewlett Packard Enterprise Development LP