How to Modify Date and Timestamps Using touch on CentOS

In CentOS, the touch command is used to create new files or update the timestamp of existing files. The timestamp includes the date and time that the file was last accessed, modified, or changed.

In this guide, we will show you how to modify the date and timestamps using touch on CentOS.

Step 1: Check Current Timestamps

Before modifying the date and timestamps, it’s a good idea to check the current timestamps of the file. You can do this using the ls command with the -l flag. For example:

ls -l myfile.txt

This command will display the file’s current timestamps in the following format:

-rw-r--r-- 1 user group 100 Apr 13 10:30 myfile.txt

The first column shows the file’s permissions, the second column shows the number of hard links, the third and fourth columns show the owner and group, the fifth column shows the file size in bytes, and the sixth column shows the date and time that the file was last modified.

See also  How to List User Groups on CentOS

Step 2: Modify Access and Modification Timestamps

You can modify the access and modification timestamps of a file using the touch command. The -a flag is used to modify the access timestamp, and the -m flag is used to modify the modification timestamp. For example, to change the modification timestamp of myfile.txt to April 1, 2023, you can run the following command:

touch -m -t 202304010000 myfile.txt

The -t flag is used to specify the new timestamp in the format yyyymmddHHMM, where yyyy is the year, mm is the month, dd is the day, HH is the hour, and MM is the minute.

Step 3: Modify Change Timestamp

You can also modify the change timestamp of a file using touch. The -c flag is used to update the timestamp of a file only if it has been changed. For example, to update the change timestamp of myfile.txt, you can run the following command:

touch -c myfile.txt

This command will update the change timestamp of the file only if it has been modified.

See also  How to Download Ubuntu Server 11.10 ISO

Step 4: Check Modified Timestamps

After modifying the timestamps, you can use the ls command with the -l flag again to verify that the timestamps have been updated. For example:

ls -l myfile.txt

This command will display the file’s updated timestamps in the same format as before.

Examples

a)Change date to past time:

    [root@server ~]# touch -t 0101141400 newtestfile1
    [root@server ~]# ls -l --full-time newtestfile1
    -rw-r--r-- 1 root root 0 2001-01-14 14:00:00.000000000 +0800 newtestfile1
    

Description:
The –full-time option and long format listing of the ls command show that the file newtestfile1 now has a timestamp of (2.00PM, 14 January 2001)

b)Change date to future time:

    [root@server ~]# touch -t 1212261400 newtestfile1
    [root@server ~]# ls -l --full-time newtestfile1
    -rw-r--r-- 1 root root 0 2012-12-26 14:00:00.000000000 +0800 newtestfile1
    

Description:
The –full-time option and long format listing of the ls command show that the file newtestfile1 now has a timestamp of (2.00PM, 26 December 2012)

See also  How to Properly Shutdown Linux CentOS/RHEL Server

Commands Mentioned:

  • ls -l – Check current timestamps
  • touch -m -t – Modify modification timestamp
  • touch -c – Modify change timestamp

Conclusion

In this guide, we have shown you how to modify date and timestamps using the touch command on CentOS. By using the touch command with the appropriate flags, you can modify the access, modification, and change timestamps of a file. This can be useful when you need to backdate or update files for testing purposes or when dealing with files that have incorrect timestamps. We hope this guide has been helpful to you. If you have any comments or suggestions for improvements, please feel free to share them below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *