Home >System Tutorial >LINUX >How To Change File Timestamps To Specific Date And Time In Linux
This guide demonstrates how to manage file timestamps in Linux, a crucial task for various applications like testing, data analysis, and compliance. We'll cover changing and verifying timestamps using common Linux commands, along with practical use cases.
Table of Contents
ls
Commandstat
CommandModifying File Timestamps in Linux
The touch
command is key to creating empty files or updating existing file timestamps. The -t
option allows precise timestamp control.
Setting a Precise Date and Time
To set the timestamp of ostechnix.txt
to 12:30 PM on November 14th, 2024:
touch -t 202411141230 ostechnix.txt
20241114
: Represents November 14th, 2024.1230
: Represents 12:30 PM.This updates both access and modification timestamps.
Setting the Timestamp to Midnight
To set the timestamp to midnight on a specific date, omit the time:
touch -t 202411140000 ostechnix.txt
This sets the timestamp to 12:00 AM on November 14th, 2024.
Verifying File Timestamps
Use ls
and stat
to confirm timestamp changes.
Using the ls
Command
The ls -l
command displays files in a long listing format, including timestamps:
ls -l ostechnix.txt
Output (example):
<code>-rw-r--r-- 1 ostechnix ostechnix 158 Nov 14 12:30 ostechnix.txt</code>
"Nov 14 12:30" shows the last modification time.
Using the stat
Command
stat
provides detailed file information, including timestamps:
stat ostechnix.txt
Output (example):
<code> File: ostechnix.txt Size: 158 Blocks: 8 IO Block: 4096 regular file Device: 259,2 Inode: 1578889 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/ostechnix) Gid: ( 1000/ostechnix) Access: 2024-11-14 12:30:00.000000000 +0530 Modify: 2024-11-14 12:30:00.000000000 +0530 Change: 2024-11-14 17:47:19.624593116 +0530 Birth: 2024-10-11 15:39:59.515973804 +0530</code>
This shows Access (last access), Modify (last modification), Change (last metadata change), and Birth times.
ls
offers a quick overview, while stat
provides comprehensive timestamp details.
Practical Applications
Timestamp manipulation is valuable for:
Scenario Example
Simulate log files created on different days:
touch -t 202411141230 ostechnix.txt
ls -l
confirms the timestamps are correctly set.
Summary
Managing file timestamps in Linux is straightforward using touch
, ls
, and stat
. This control is essential for various tasks, from simple testing to complex compliance needs. This guide provides the tools and knowledge to effectively manage file timestamps.
Further Reading:
The above is the detailed content of How To Change File Timestamps To Specific Date And Time In Linux. For more information, please follow other related articles on the PHP Chinese website!