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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


`touch $filename`;
oostwijkAuthor Commented:
ok, that's on thing, but by using this the script doesn't know what date it should get..
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
#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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.