Change date time stamp of existing file

I have a file hello.txt which was created today (today's date timestamp)

I wish to change its date timestamp (access, modified, created) to 1 week old i.e one week from now.

uname -a
SunOS mymac 5.11 11.2 sun4v sparc sun4v

Can you please suggest a easy way to do that ?

Try touch .

I know that touch has a solution but i tried the below command to change the date timestamp to one week back (old) and it did not work.

Considering today is Thursday.

touch -d "previous Thursday" hello.txt
touch: bad time specification

Can you help me with the accurate command. I am not concerned about the time ... its just the date that I wish to change to one week back.

Today is 28 July 2016, below command will change the access time and modification time of file hello.txt to 21 July 2016 13:00 hrs

touch -t 1607211300 hello.txt

There is no creation time, therefore it cannot be changed :slight_smile:

I m not looking for a static but a dynamic solution.

The date should change one week prior for any given file on any given day.

Hello mohtashims,

If you have GNU date in your system then following may help you in same.
Let's say we have following Input_file(as an example).

ls -ld   Input_file
-rw-rw-r-- 1 Singh_is_King   Singh_is_King  100 Jul 27 10:51  Input_file

Then following is the command for changing the timings.

touch -t $(date -d"1 week ago" +%y%m%d%H%M)   Input_file

Now after executing above command, when you check the Input_file details they will be as follows.

ls -ld   Input_file
-rw-rw-r-- 1 Singh_is_King   Singh_is_King  100 Jul 21 06:07  Input_file

Thanks,
R. Singh

Does not work.

Please see the output:

touch -t date -d"1 week ago" +%y%m%d%H%m hello.txt
touch: bad time specification

You have not followed the suggestion, so I'm not surprised.

As a quick test, can you do the following?:-

date -d"1 week ago"

If you get an error then we will know you haven't got that function available.

Robin

Hello mohtashims,

Seems to be that previous GNU date command is not working in Sun/Solaris OS, could you please try following and let me know if this works for you.

touch -t $(perl -e '@T=localtime(time-86400);printf("%02d%02d%02d%02d%02d\n",$T[5]+1900,$T[4]+1,$T[3]-6,$T[2],$T[1])')  Input_file
OR
touch -t $(perl -e '@T=localtime(time);printf("%02d%02d%02d%02d%02d\n",$T[5]+1900,$T[4]+1,$T[3]-7,$T[2],$T[1])')    Input_file
 

Thanks,
R. Singh

1 Like

Thank you but i don't use perl.

I need a non-perl solution.

If we do not have a non-perl solution for my operating system do let me know, so i dont invest more time with this.

I note that when Ravinder suggested using:

+%y%m%d%H%M

as a date format when using the GNU date utility, you not only decided to ignore the fact that you would have to specify the pathname for the GNU date utility, you also decided to change that format to:

+%y%m%d%H%m

and, of course, it didn't work.

There is no way for us to know whether or not you have installed the GNU core utilities on your system or not, and if so, where you installed them. You might see if gdate would work with your default path or if /usr/gnu/bin/date would work.

Or, if you use ksh93 as your shell on a Solaris 11 system, the following should work for you:

touch -t $(printf '%(%Y%m%d%H%M)T' "7 days ago") hello.txt

If you choose to change the date format I specified, I don't want to hear about how you didn't get the right time.

If you choose to change 7 days ago to 1 week ago , I don't want to hear about how you got Monday a week ago instead of the current day a week ago.

If you choose to try the above command using some shell other than ksh93 to run that command, I don't want to hear about how it didn't work for you.

1 Like

This worked !!

touch -t $(printf '%(%Y%m%d%H%M)T' "7 days ago") hello.txt

@Don Cragun -> Not to take anything away from anyone but you are amazing !!

If that is a new request, please be aware that

  • it is usually preferred to open a new thread of its own with new questions
  • the better (i.e. more precise/detailed) the specification, the better the results or proposals

What did you try to adapt the solutions presented before to solve your request?