changing date of file

Hi there,
Can someone help me out ?
I want to make a script which modifies the date of a certain file on the server. Can someone tell me the command to modify the date ?
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

maneshrConnect With a Mentor Commented:
i am a bit confused.

so you have a file on your server let's say


you want to modify the date of this file (for which you can use the piece of code i had given earlier).

The touch command updates the access and modification times of the specified file. If you do not specify a value for the Time variable, the touch command uses the current time.

However if you want to use any other time you can use the foll command
touch -t YYMMDDhhmm filename

YY=last 2 digit of year, MM= month (01-12) DD= day (01-31) hh=hour and mm=minutes

touch -t 9901011212 /tmp/thisfile.htm

NOTE: you should be the owner of this file to do the above.

`touch $filename`;
oostwijkAuthor Commented:
ok, that's on thing, but by using this the script doesn't know what date it should get..
#include <time.h>
#include <sys/types.h>
#include <utime.h>
#include <errno.h>

int main(int argc, char **argv)
  int date  = 21;
  int month = 10;
  int year  = 2001;
  char *filename = "name_of_file";

  time_t t;
  struct tm mktimeb;
  struct utimbuf utimeb;

  if(argc == 2) filename = argv[1];

  mktimeb.tm_sec  = 0;
  mktimeb.tm_min  = 0;
  mktimeb.tm_hour = 0;
  mktimeb.tm_mday = date;
  mktimeb.tm_mon  = month - 1;
  mktimeb.tm_year = year - 1900;
  mktimeb.tm_isdst = -1;

  t = mktime(&mktimeb);

//  utimeb.actime  = t;   /* access time */
  utimeb.modtime = t;   /* modification time */

  if( utime(filename, &utimeb) )
      case (EACCES):
        printf("Write to file \'%s\' is denied", filename);
      case (ENOENT):
        printf("File \'%s\' does not exist", filename);

  exit (0);
oostwijkAuthor Commented:
Well done, keep up the good work
All Courses

From novice to tech pro — start learning today.