Commands.page Logo

How to Update File Access and Modification Time in Ubuntu

This article demonstrates how to change the access and modification timestamps of a file to the current time on Ubuntu. It covers the essential command line tool needed to update these attributes without modifying the actual file content.

Using the Touch Command

The primary tool for changing file timestamps in Ubuntu is the touch command. When used with a filename that already exists, it updates both the access time and the modification time to the current system time.

To update a specific file, open your terminal and run the following command:

touch filename.txt

Replace filename.txt with the name of your target file. If the file exists, its timestamps will update immediately. If the file does not exist, touch will create a new empty file by default.

Preventing File Creation

If you want to update the timestamps only if the file exists and avoid creating a new file if it is missing, use the -c flag. This ensures you do not accidentally generate empty files in your directory.

touch -c filename.txt

Verifying the Changes

You can confirm that the timestamps have been updated to the current time by using the stat command or listing the file details with ls.

To view detailed timestamp information, run:

stat filename.txt

Look for the “Access” and “Modify” fields in the output to verify they match the current time. Alternatively, use ls -l to see the modification time quickly.

ls -l filename.txt