Manipulating File Timestamps with touch
The touch command allows you to not only create files but also update their timestamps. This is useful for testing or when you need to synchronize timestamps between files.
Updating All Timestamps
To update all timestamps (access, modification, and change) to the current time:
touch testfile.txt
Check the timestamps:
stat testfile.txt
Output:
File: testfile.txt
Size: 19 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 2101099 Links: 1
Access: (0640/-rw-r-----) Uid: (1000/labex) Gid: (1000/labex)
Access: 2023-05-15 14:45:10.000000000 +0000
Modify: 2023-05-15 14:45:10.000000000 +0000
Change: 2023-05-15 14:45:10.000000000 +0000
Birth: -
Updating Only Access Time
To update only the access time:
touch -a testfile.txt
Check the timestamps:
stat testfile.txt
Output:
File: testfile.txt
Size: 19 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 2101099 Links: 1
Access: (0640/-rw-r-----) Uid: (1000/labex) Gid: (1000/labex)
Access: 2023-05-15 14:46:15.000000000 +0000
Modify: 2023-05-15 14:45:10.000000000 +0000
Change: 2023-05-15 14:46:15.000000000 +0000
Birth: -
Note that the change time (ctime) is also updated because the file's metadata has changed.
Updating Only Modification Time
To update only the modification time:
touch -m testfile.txt
Check the timestamps:
stat testfile.txt
Output:
File: testfile.txt
Size: 19 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 2101099 Links: 1
Access: (0640/-rw-r-----) Uid: (1000/labex) Gid: (1000/labex)
Access: 2023-05-15 14:46:15.000000000 +0000
Modify: 2023-05-15 14:47:20.000000000 +0000
Change: 2023-05-15 14:47:20.000000000 +0000
Birth: -
Again, the change time is also updated.
Setting Specific Timestamps
You can set a specific timestamp using the -d option with touch:
touch -d "2023-01-01 12:00:00" testfile.txt
Check the timestamps:
stat testfile.txt
Output:
File: testfile.txt
Size: 19 Blocks: 8 IO Block: 4096 regular file
Device: 801h/2049d Inode: 2101099 Links: 1
Access: (0640/-rw-r-----) Uid: (1000/labex) Gid: (1000/labex)
Access: 2023-01-01 12:00:00.000000000 +0000
Modify: 2023-01-01 12:00:00.000000000 +0000
Change: 2023-05-15 14:48:30.000000000 +0000
Birth: -
This sets both the access and modification times to January 1, 2023, at 12:00 PM. The change time is still updated to the current time.
Creating a New File with Timestamps from an Existing File
You can also use the -r option to reference another file's timestamps:
## Create a new file
touch newfile.txt
## Set its timestamps based on testfile.txt
touch -r testfile.txt newfile.txt
Check the timestamps of the new file:
stat newfile.txt
Output:
File: newfile.txt
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 801h/2049d Inode: 2101100 Links: 1
Access: (0644/-rw-r--r--) Uid: (1000/labex) Gid: (1000/labex)
Access: 2023-01-01 12:00:00.000000000 +0000
Modify: 2023-01-01 12:00:00.000000000 +0000
Change: 2023-05-15 14:49:45.000000000 +0000
Birth: -
The access and modification times of newfile.txt now match those of testfile.txt.