You can use touch -r option to provide the timestamp details of another file in touch command. By default, touch command uses the current timestamp value.
Example:
Get the current system time
1 2 |
## Get current timestamp date +"%b %d %T" ## returns Feb 02 18:45:32 |
Create a new empty file
1 2 |
## Create an empty file touch myrefile |
Get the newly created file timestamp details
1 2 3 4 5 6 7 8 9 10 |
## Get the myrefile time details stat myrefile ## returns ## File: myrefile ## Size: 0 Blocks: 0 IO Block: 512 regular empty file ## Device: 2h/2d Inode: 147774362773100948 Links: 1 ## Access: (0644/-rw-r--r--) Uid: ( 1000/ debjeet) Gid: ( 1000/ debjeet) ## Access: 2022-02-02 18:46:57.876243100 +0530 ## Modify: 2022-02-02 18:46:57.876243100 +0530 ## Change: 2022-02-02 18:46:57.876243100 +0530 |
touch -r option example
1 2 |
## touch -r option touch -r myrefile myfile ## use reference file (myrefile) timestamp |
Get the newly created file timestamp details
1 2 3 4 5 6 7 8 9 10 11 12 |
## Get the myrefile time details stat myfile ## returns ## File: myfile ## Size: 0 Blocks: 0 IO Block: 512 regular empty file ## Device: 2h/2d Inode: 63050394783226292 Links: 1 ## Access: (0644/-rw-r--r--) Uid: ( 1000/ debjeet) Gid: ( 1000/ debjeet) ## Access: 2022-02-02 18:46:57.876243100 +0530 ## Modify: 2022-02-02 18:46:57.876243100 +0530 ## Change: 2022-02-02 18:48:06.286251300 +0530 rm my* |
Observe, both myrefile and myfile Access and Modify times are the same though they are created at two different timestamp.