Linux document timestamp viewing and modification--stat

Source: Internet
Author: User
Tags touch command

View file Timestamp command: Stat awk.txt
File: ' Awk.txt '
Size:20 blocks:8 IO block:4096 Regular file
device:801h/2049d inode:380730 links:1
Access: (0644/-rw-r--r--) Uid: (0/root) Gid: (0/root)
Access:2008-04-26 01:50:44.000000000 +0800
Modify:2008-04-26 01:48:18.000000000 +0800
Change:2008-04-26 01:48:18.000000000 +0800

Description: Access time for access. Modify modification time. Change state changes time. Can stat * View the status of all files in this directory.
Now, to filter out the timestamp of the modify and save it in YYYYMMDDHHSS format, you can use this command:
Stat Awk.txt | Grep-i Modify | Awk-f. ' {print $} ' | awk ' {print $2$3} ' | awk-f-' {print $1$2$3} ' | Awk-f: ' {print $1$2$3} '
Output results: 20080426014818

To modify the file time and create a new file: Touch

When introducing the LS command, it is mentioned that each file will have 3 major modification times recorded under Linux:
modification Time (mtime, modified): This is updated when the "content data" of the file is changed. Content data refers to the contents of a file, not the attributes of a file.
Status Time (CTime, status time): When the status of the file changes, it will be updated, for example, if the permissions and attributes are changed, the time will be updated.
access Time (atime): This read time is updated when "Fetch file contents" is used. For example, using cat to read ~/.BASHRC, the atime is updated.
For example, let's look at the time of the/etc/man.config file.

[Email protected] ~]# ls-l/etc/man.config
-rw-r--r--1 root root 4506 Apr 8 19:11/etc/man.config
[Email protected] ~]# ls-l--time=atime/etc/man.config
-rw-r--r--1 root root 4506 Jul 17:53/etc/man.config
[Email protected] ~]# ls-l--time=ctime/etc/man.config
-rw-r--r--1 root root 4506 June 08:28/etc/man.config

Did you see it? By default, LS displays the file's Mtime, which is the time the content of this file was last changed. My system was installed in 6/25, so the time the file was generated but the state changed back to that point in time. Because this file is used in the example just now, its atime will become the time it was just used.

The time of the file is important because it can cause some programs not to run smoothly if the file time is misjudged. Well, in case I find a file from the future (many times there will be this problem.) When we install it, the GMT time is what it means, how do you make the file's time "now"? Very simple, with touch commands.

[[email protected] ~]# touch [-ACDMT] File
Parameters:
-A: Modify access time only.
-C: Modify the time only, not the file.
-D: Date can be followed or--date= "date or time" can be used
-M: Modifies only mtime.
-T: can be followed by time, in the format [YYMMDDHHMM]
Example:
Example one: Create a new empty file.
[Email protected] ~]# cd/tmp
[email protected] tmp]# Touch Testtouch
[Email protected] tmp]# ls-l Testtouch
-rw-r--r--1 root root 0 Jul 20:49 Testtouch
Note that the size of this file is 0. In the default state, if the touch is followed by a file,
# 3 time (atime/ctime/mtime) of the file will be updated to the current time. If the file does not exist,
# will be active to create a new empty file. For example, the above example.
Example two: Copy the ~/.BASHRC to BASHRC, assuming that the full attribute is copied, check its date.
[email protected] tmp]# CP ~/.BASHRC BASHRC
[email protected] tmp]# ll BASHRC; ll--time=atime BASHRC; ll--time=ctime BASHRC
-rwxr-xr-x 1 root root 395 Jul 4 11:45 BASHRC <== this is mtime.
-rwxr-xr-x 1 root root 395 Jul 20:44 BASHRC <== this is atime.
-rwxr-xr-x 1 root root 395 Jul 20:53 BASHRC <== this is CTime.
# In this case, we used; The command delimiter, which we will refer to in the bash shell.
In addition, LL is Ls-l's command alias, which is also mentioned again in the bash shell,
# Now you can simply think of it, LL is the shorthand for ls-l. As for is to issue two commands at the same time,
# and let two commands be executed "in order" meaning. As you can see in the above results, the date of the file change
# Jul 4 11:45, but Atime is not the same as CTime.
Example three: Modify case two of the BASHRC file, adjust the date to two days ago.
[[email protected] tmp]# touch-d "2 days Ago" BASHRC
[email protected] tmp]# ll BASHRC; ll--time=atime BASHRC; ll--time=ctime BASHRC
-rwxr-xr-x 1 root root 395 Jul 21:02 BASHRC
-rwxr-xr-x 1 root root 395 Jul 21:02 BASHRC
-rwxr-xr-x 1 root root 395 Jul 21:02 BASHRC
# compared to the previous example, it was 19th that it became 17th (atime/mtime).
# But the CTime didn't change.
Example four: Change the BASHRC date of the last example to 2005/07/15 2:02.
[Email protected] tmp]# touch-t 0507150202 BASHRC
[email protected] tmp]# ll BASHRC; ll--time=atime BASHRC; ll--time=ctime BASHRC
-rwxr-xr-x 1 root root 395 Jul 02:02 BASHRC
-rwxr-xr-x 1 root root 395 Jul 02:02 BASHRC
-rwxr-xr-x 1 root root 395 Jul 21:05 BASHRC
Note that the date is changed in both Atime and Mtime, but CTime is the current time recorded.

The touch command makes it easy to modify the date and time of a file. Also, an empty file can be created. However, it is important to note that even if you copy a file, all properties are copied, but there is no way to copy the CTime property. CTime can record the time the file has changed state (status) recently. Anyway, still want to explain, we usually look at the file attributes, the more important or mtime. We are often concerned about when the "content" of this file was changed.
The most common use of the Touch command is:
• Create an empty file.
• Modify a file date to the current date (Mtime and Atime).

Several times related to the file:

1, Access time, read the contents of this file, this time will be updated. For example, use the more command for this file. The LS, stat command does not modify the file access time.

2, modify the time, the contents of the file modification once, this time will be updated. For example: VI after saving files. The time that ls-l lists is this time.

3, state change time. Change the file properties once with the chmod command, and the time will be updated. To view the detailed status of the file, the exact modification time, etc., you can use the stat command file name.

Linux document timestamp viewing and modification--stat

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.