Let’s go ahead and talk about file timestamps. Every file on Linux has three timestamps: 1
the access timestamp or atime, the modified timestamp or mtime, and the changed timestamp ctime. Linux 2
timestamps hold an integer number rather than a date and time. 3
This number is the number of seconds since the unique epoch, which was midnight on January 1st, 1970 4
UTC. 5
When Linux needs to display the date, it translates that number of seconds into a date and time. 6
This makes it easier for humans to understand. To view these timestamps, use the statement or ls 7
with options. 8
The stat command is showing statistics about the file including the timestamps. You can see 9
the access time, the modification time and the changed time. EXT4, which is the default file 10
system on most Linux distributions, also allocates space for a file creation timestamp in its internal 11
file system structures. 12
But in most cases, this has not been implemented yet. In this example, file creation or birth hasn't 13
been implemented yet. 14
That's why we see a hyphen instead. 15
Let's discuss what these timestamps mean. 16
The access time or atime is the last time the file was read. 17
This means the time when someone used a program to display the contents of the file, it doesn't 18
matter if the file contains were changed or not; the modification timestamp or mtime signifies the last 19
time the contents of the file was modified. 20
If someone used a text editor and changed something inside, the file mtime was updated. 21
And finally, the change time or ctime is the time when some metadata related to the file, not to the 22
file contents, was changed. 23
For example, changing the files permissions or the files owner will update the changed time. 24
To see the timestamps with the ls command run ls -l, or ls -lt if you want to see the modification 25
time, ls -lu to see the access time and ls -lc to see the changed time. 26
Sometimes the ls-l is not displaying the timestamp with a maximum precision. 27
In this example, we only know the month, the day, the hour and the minute when the file was last 28
modified. To see the entire timestamp that was saved, this means including the year and the second, use 29
the --full-time option like this: ls -l --full-time and /etc/ or the directory. 30
And we are seeing the entire timestamp. 31
Let's talk about changing the file timestamp. This is useful when you want a back up program to include 32
or exclude some files or when you simply do not want other users to know that you've read or modified 33
the file; you can change the time stamp on a file using the touch 34
command. 35
The touch command is also used to create an empty file. 36
So if the file, which is the argument of the touch command, does not exist, touch will create it. 37
But if it exists, touch will update the file timestamps to the computer's current time. 38
This is the contents of the current directory, which is the user's home directory. 39
And if I run touch linux.txt it will create the file since the file does not already exist. 40
The file is there. 41
Let's see the modification time. 42
And stat linux.txt ; these are the timestamps of the file. By the way, in Linux, 43
it's not recommended to use spaces or other special characters in filenames; separate the multiple 44
words with an underscore or a hyphen. 45
But if for some reason you want to include a whitespace in the filename, then enclose the file name 46
in a pair of double quotes, like this: 47
The file name contains a whitespace. 48
Now, if I run touch linux.txt again, it will update all three timestamps of the file to the current time. 49
touch linux.txt and stat linux.txt these are the new timestamps. 50
Take a look! 51
They are updated. 52
54 14:29 and the now 14:30. If you want to change only the access time to the 53
current time, use the -a option. 54
So touch -a linux.txt; and stat a.txt 55
Note that the access time was updated to the current time, the time was also updated because metadata 56
information of the file like atime was changed. 57
This is normal behavior. 58
To change the modification timestamp, you can use the same -m option : touch -m and the file 59
and stat linux.txt; 60
See how both mtime and chnge time were modified. If you want to set the access time or modification 61
time to a specific time, not the system current time, you can use -d option and a datetime. 62
Let's change the modification 63
time to a date in the past: touch -m and the date, let's say 2018 64
12 the month 65
30 the day, the hour, the minute, and dot the second. 66
And the file; the format of the new timestamp is year 67
month 68
day, hour and .seconds 69
I'm sorry, I've forgotten the -t option. 70
This is very important. 71
And the modification time was changed. 72
If you want to change simultaneously both access and modification time, use the -d like 73
this touch -d between double quotes a date, in this format, 74
and the file. 75
Both atime and mtime were changed. 76
Note that -t and -d options accept different data formats. 77
You can also use the -r option if you want to set the timestamps of one file to the timestamp values 78
of another one. 79
For example, I'm creating a new file ubuntu.txt 80
These are its timestamps and I'm setting the time stamps of linux.txt to the timestamps of 81
ubuntu.txt 82
So touch linux.txt -r ubuntu.txt; ubuntu.txt is the reference file and stat linux.txt 83
They have the same timestamps. 84
Note that it's not possible to modify only the change time to a specific timestamp, but I'll tell 85
you a trick. 86
If you want to modify only the change time to a specific time, then you set the system date and time 87
to the desired change time run the command touch and the name of the file to change all the timestamps 88
to the system current time and then change back the access and modification times to the initial values. 89
Change time will remain at the desired value. 90
You can do this as homework. 91
Use the date command to both display the current date and time and set the system date and time. 92
This is the date and time using the local time zone, East European time and the date-u is 93
displaying the date and time using the coordinated Universal Time or UTC ; check its man page to see the 94
format of the new date if you want to change it.