To totally unlock this section you need to Log-in
Login
Another quick Linux how to today, here we learn how to change the date on a file in Linux. First we will create the file and list its details below, we create the file with the following syntax:
touch testfile.txt
We now use the ls -l command to see the details of this file:
We can see that the file was create june 26 at 09:12, we can modify the date stamp of a Linux file with the Touch command with the "-t" flag like so:
touch -t 09091020 testfile.txt
In the above we change the testfile.txt to have a date of the 9th of september at 10:20 am.
To see this has actually worked we use the "ls -l" command again to list the file information:
Use the time stamp of another File
The following touch command with -r option (i.e., reference), will update the timestamp of file meena with the timestamp of leena file. So, both the file holds the same time stamp:
# touch -r leena meena
Explicitly Set the Access and Modification times
You can explicitly set the time using -c and -t option with touch command. The format would be as follows:
# touch -c -t YYDDHHMM leena
For example the following command sets the access and modification date and time to a file leena as 17:30 (17:30 p.m.) December 10 of the current year (2012):
# touch -c -t 12101730 leena
Create a File using a specified time
If you would like to create a file with specified time other than the current time, then the format should be.
# touch -t YYMMDDHHMM.SS tecmint
For example the below command touch command with -t option will gives the tecmint file a timestamp of 18:30:55 p.m. on December 10, 2012:
# touch -t 201212101830.55 tecmint
We have almost covered all the options available in the touch command for more options use man touch. If we’ve still missed any options and you would like to include in this list, please update us via comment box.
Change File Access and Modification Time
To change or update the last access and modification times of a file called leena, use the -a option as follows. The following command sets the current time and date on a file. If the leena file does not exist, it will create the new empty file with the name.
# touch -a leena
Partial date and time on file
The -d and -t options allow the user to add a specific last access time. The former is followed by a string (i.e., sequence of characters) in the date, month, year, minute:second format, and the latter uses a [[CC]YY]MMDDhhmm[.ss] format. For example, to change the last access time of file8 to 10:22 a.m. May 1, 2005, 1 May 2005 10:22 would be enclosed in single quotes and used as follows:
touch -d '1 May 2005 10:22' file8
Partial date-time strings can be used. For example, only the date need be provided, as shown for file9 below (in which case the time is automatically set to 0:00):
touch -d '14 May' file9
Just providing the time, as shown below, automatically changes the date to the current date:
touch -d '14:24' file9
Stat about timestamps and version of touch command
The complete timestamps for any file or directory can be viewed by using the stat command. For example, the following would show the timestamps for a file named file11:
stat file11
The --help option displays a basic list of options, and the --version option returns the version of the currently installed touch program.