How to create/modify a file's timestamp

root's picture

In other words, how to change the modification time on a file.
Tricky, huh? But yet it's possible!

In this example, I had already a file but you can create one directly with touch if this is your goal.

Target file:

florian@florian:~$ echo 1234 > test.txt
florian@florian:~$ ls -la test.txt
-rw-rw-r--. 1 florian florian 5 Nov 14 13:41 test.txt
florian@florian:~$ cat test.txt
1234

Now, you have to use the "-t" option in touch command and the format is "Year, Month, Hour, Minute". If you do not put the year, the current one will be used.
For a better view, in our example we have:
09 = month
10 = day
12 = hour
45 = minute
and for the second one (in front of the others above):
01 or 2022 = year

Modify the timestamp in the past, same year:

florian@florian:~$ touch -t 09101245 test.txt 
florian@florian:~$ ls -la test.txt
-rw-rw-r--. 1 florian florian 5 Sep 10 12:45 test.txt

Modify the timestamp in the past, different year:

florian@florian:~$ touch -t 0109101245 test.txt 
florian@florian:~$ ls -la test.txt
-rw-rw-r--. 1 florian florian 5 Sep 10  2001 test.txt

Modify the timpestamp even in the future:

florian@florian:~$ touch -t 202209101245 test.txt 
florian@florian:~$ ls -la test.txt
-rw-rw-r--. 1 florian florian 5 Sep 10  2022 test.txt

PS: there is also the option -d on which you can use the time in "human" format:

florian@florian:~$ touch -d "10 Sep 2069 15:20" test.txt 
florian@florian:~$ ls -la test.txt
-rw-rw-r--. 1 florian florian 5 Sep 10  2069 test.txt
florian@florian:~$ touch -d "20 Sep 2030" test.txt 
florian@florian:~$ ls -la test.txt
-rw-rw-r--. 1 florian florian 5 Sep 20  2030 test.txt

How to copy the timestamp from one fie to another:

florian@florian:~$ touch -r IMG_1535.MOV IMG_1535-1.mov

How to do it for multiple files if the difference is similar:

florian@florian:~$ for i in $(ls *-1.mov | sed 's/-1.mov//g'); do touch -r ${i}.MOV ${i}-1.mov; done

Thou shalt not steal!

If you want to use this information on your own website, please remember: by doing copy/paste entirely it is always stealing and you should be ashamed of yourself! Have at least the decency to create your own text and comments and run the commands on your own servers and provide your output, not what I did!

Or at least link back to this website.

Recent content