commands for viewing and modifying file times in Linux

Source: Internet
Author: User
Tags current time parse string touch touch command file permissions

There are three main types of file time under Linux:

1.1 Modification Time (mtime)

File modification time, that is, when the contents of the file changes, update this time, does not include file permissions and property modification. Using Ls-l view, the default display time is Mtime

$ ls-l Uconv.h
-rw-rw-r--1 Work work 1808 June 2013 Uconv.h

1.2 Status Time (CTime)

The time when file status status is modified, such as file permissions and property modifications. Use LS--time=ctime to view

$ ls-l--time=ctime uconv.h
-rw-rw-r--1 Work work 1808 June 2013 Uconv.h


1, stat View file time

[ROOT@WEB10 ~]# Stat Install.log
File: "Install.log"
size:33386 blocks:80 IO block:4096 general files
device:fd00h/64768d inode:7692962 links:1
Access: (0644/-rw-r--r--) Uid: (0/root) Gid: (0/root)
ACCESS:2012-07-13 16:02:34.000000000 +0800
modify:2011-11-29 16:03:06.000000000 +0800
change:2011-11-29 16:03:08.000000000 +0800
Description: Access access time. Modify modification time. Change state changes time. You can stat * View the status of all files in this directory.

We want to see a specific time in a file for three of times, and save it in the form of minutes and seconds. We can use the following command:

[ROOT@WEB10 ~]# stat install.log|grep-i Modify | Awk-f. ' {print $} ' | awk ' {print $2$3} ' | awk-f-' {print $1$2$3} ' | Awk-f: ' {print $1$2$3} '
20111129160306

1.3 Access Time (Atime)

File access time, update this time when the contents of the file are fetched. Use LS--time=actime to view

$ ls-l--time=atime uconv.h
-rw-rw-r--1 Work Work 1808 Dec 2013 Uconv.h

The corresponding through LS view also has three time:

modification Time (mtime, modified): This time is updated when the content data for that file changes. Content data refers to the contents of the file, not to the properties of the file.
status Time (CTime): When the status of the file changes, this time is updated, for example, if permissions and attributes are changed, this time is updated.
access Time (atime, access times): This read time is updated when "file content is taken". For example, using cat to read ~/.BASHRC will update the atime.

[Root@web10 ~]# ls-l--time=ctime Install.log
-rw-r--r--1 root root 33386 2011-11-29 Install.log
[Root@web10 ~]# ls-l--time=atime Install.log
-rw-r--r--1 root root 33386 07-13 16:02 Install.log

NOTE: LS parameters do not--mtime This parameter, because we default through the Ls-l check to see the time is mtime.

2. Time to modify files

If you need to modify the above three times, use the touch command to modify it. Touch filename, if the file does not exist, create a new file.

$ Touch--help

Usage:touch [OPTION] ... FILE ...
Update the access and modification times of all FILE to the current time.

-A change is the access time
Modify Access Time
-C,--no-create do don't create any files
Modify the file three times, do not exist do not create
-D,--date=string parse STRING and use it instead of
Specify a time to replace the current time
-F (ignored)
-M modification Time
Modify Mtime
-R,--reference=file Use this FILE ' s times instead of
-T STAMP use [[CC]YY]MMDDHHMM[.SS] instead of
Specify modified Time
For example:

$ touch-d "2 days Ago" uconv.h
$ ll uconv.h; ll--time=atime Uconv.h; ll--time=ctime Uconv.h;
-rw-rw-r--1 Work work 1808 June 18:17 Uconv.h
-rw-rw-r--1 Work work 1808 June 18:17 Uconv.h
-rw-rw-r--1 Work work 1808 June 18:17 Uconv.h

The Mtime and Atime were modified to two days ago, and CTime did not change.

$ touch-t 201406142020 uconv.h

$ ll uconv.h; ll--time=atime Uconv.h; ll--time=ctime Uconv.h;
-rw-rw-r--1 Work work 1808 June 20:20 Uconv.h
-rw-rw-r--1 Work work 1808 June 20:20 Uconv.h
-rw-rw-r--1 Work work 1808 June 18:23 Uconv.h
Atime and Mtime have changed, but CTime has become the current time.

Using the CP command,-a retains the original attribute.

$ cp-a uconv.h uconv.h1

$ ll uconv.h1; ll--time=atime Uconv.h1; ll--time=ctime Uconv.h1;
-rw-rw-r--1 Work work 1808 June 20:20 Uconv.h1
-rw-rw-r--1 Work work 1808 June 18:25 Uconv.h1
-rw-rw-r--1 Work work 1808 June 18:27 Uconv.h1

Mtime and Atime keep the original file unchanged, but CTime becomes the current time

Note : If the touch is followed by an existing file, the file's 3 time (Atime/ctime/mtime) will be updated to the current time. If the file does not exist, it will actively create a new empty file.

[Root@web10 ~]# Touch Install.log
[ROOT@WEB10 ~]# Stat Install.log
File: "Install.log"
size:33386 blocks:80 IO block:4096 general files
device:fd00h/64768d inode:7692962 links:1
Access: (0644/-rw-r--r--) Uid: (0/root) Gid: (0/root)
ACCESS:2012-07-13 16:21:50.000000000 +0800
MODIFY:2012-07-13 16:21:50.000000000 +0800
CHANGE:2012-07-13 16:21:50.000000000 +0800

Similarly, using LS, the same results are found.

[Root@web10 ~]# ls-l--time=ctime Install.log
-rw-r--r--1 root root 33386 07-13 16:21 Install.log
[Root@web10 ~]# ls-l--time=atime Install.log
-rw-r--r--1 root root 33386 07-13 16:21 Install.log
[Root@web10 ~]# ls-l Install.log
-rw-r--r--1 root root 33386 07-13 16:21 Install.log

Let's look at an example that doesn't relate to touch:

[Root@web10 ~]# cp/etc/profile.; ll--time=atime profile. ll--time=ctime profile
CP: Do you want to overwrite "./profile"? Y
-rw-r--r--1 root root 1344 07-13 16:24 profile
-rw-r--r--1 root root 1344 07-13 16:25 profile
Because I have run this command once before, so there will be coverage, but this coverage is good, just let us see the atime and CTime time difference.

We'll go back to the touch. Modify file time by:

1. Modify the file at the same time and access time
touch-d "2010-05-31 08:10:30" Install.log
2. Modify the file modification time only
Touch-m-D "2010-05-31 08:10:30" Install.log
3. Only modify the access time of the file
Touch-a-D "2010-05-31 08:10:30" Install.log

Below give a rootkit Trojan commonly used trick. is to change the time of the latter file to the same as the previous one.

Touch-acmr/bin/ls/etc/sh.conf
In addition, the touch also supports modifying the file time like the date command:

[Root@web10 ~]# touch-d "2 days Ago" Install.log; ll Install.log
-rw-r--r--1 root root 33386 07-11 16:35 Install.log
Finally, the paper summarizes the relationship between the common file operation and time:

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

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

3, the state change time. The time is updated by changing the file properties once with the chmod command. To view the detailed status of the file, the exact modification time, and so on, you can use the stat command file name.

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.