Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rename file based on last mod datetime?

Status
Not open for further replies.

BPetro

Programmer
Oct 1, 2002
59
US
Has anyone done this? I know how to rename a file using today's datetime. But how about if I want to use the files last modified date/time, represent that as a nice file-name string like maybe 071003-1412 (that's flexible, but keep it nice for a file name) and then rename the file based on that. Any ideas? Is there a command to return that for a specific filename or would you have to cut some form of ls output to snag it? If the latter anyone have a "best" form?

THANKS!
 
Hi

Code:
[blue]master #[/blue] ls -ls
total 12
   4 -rw-r--r--    1 master  master      172 2007-07-06 16:10 File1
   4 -rw-r--r--    1 master  master       85 2006-10-29 12:39 file1.txt
   4 -rw-r--r--    1 master  master      799 2006-09-13 17:31 first

[blue]master #[/blue] find . -type f -printf 'mv "%P" "%P-%Ty%Tm%Td-%TH%TM"\n' | sh

[blue]master #[/blue] ls -ls
total 12
   4 -rw-r--r--    1 master  master      172 2007-07-06 16:10 File1-070706-1610
   4 -rw-r--r--    1 master  master       85 2006-10-29 12:39 file1.txt-061029-1239
   4 -rw-r--r--    1 master  master      799 2006-09-13 17:31 first-060913-1731
Tested with GNU [tt]find[/tt].

Feherke.
 
Excellent - that's better than I had hoped. I was afraid it would require some cut magic which would be a fairly ugly hack. Thanks a bunch.

Hmm... to take it a step further, here's the perfect final solution for what I needed if anyone else wants:

[tt]find . -type f -iname "*.log" -printf 'mv "%P" "%P-%Ty%Tm%Td-%TH%TM"\n' | sh [/tt]

This is what feherke suggested plus elimination of all files that do not match a "*.log" name skeleton.
 
If you rename a file, doesn't the mod time become the time you renamed it?
 
If you rename a file, doesn't the mod time become the time you renamed it?
No.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top