view linux file change time, modify time and access time, use linux stat or ls

How does Linux view change time, modify time and access time? That is linux file ctime, mtime and atime.

Use the linux stat command or the linux ls command.

First, let’s look at the meaning of the three time attributes of linux.

  • atime: access time, which shows the last time the content in the file was accessed. The ls and stat commands do not modify the access time of the file.
  • mtime: modify time, which shows the last time when the content of the file was changed.
  • ctime: change time, which shows the permissions of the file, the owner, the group to which it belongs, and the time when the number of links changes.Of course, when the content changes, it also changes.

Linux stat

Create a file.

➜ date
Tue Jun 11 22:16:42 CST 2019
➜ touch file
➜ stat -x file
  File: "file"
  Size: 0            FileType: Regular File
  Mode: (0644/-rw-r--r--)         Uid: (  501/ylspirit)  Gid: (   20/   staff)
Device: 1,4   Inode: 8597466429    Links: 1
Access: Tue Jun 11 22:16:50 2019
Modify: Tue Jun 11 22:16:50 2019
Change: Tue Jun 11 22:16:50 2019

Write content to a file.

➜ echo "hello world" > file
➜ stat -x file
  File: "file"
  Size: 12           FileType: Regular File
  Mode: (0644/-rw-r--r--)         Uid: (  501/ylspirit)  Gid: (   20/   staff)
Device: 1,4   Inode: 8597466429    Links: 1
Access: Tue Jun 11 22:22:19 2019
Modify: Tue Jun 11 22:22:17 2019
Change: Tue Jun 11 22:22:17 2019

Access files.

➜ cat file
hello world
➜ stat -x file
  File: "file"
  Size: 12           FileType: Regular File
  Mode: (0644/-rw-r--r--)         Uid: (  501/ylspirit)  Gid: (   20/   staff)
Device: 1,4   Inode: 8597466429    Links: 1
Access: Tue Jun 11 22:24:52 2019
Modify: Tue Jun 11 22:22:17 2019
Change: Tue Jun 11 22:22:17 2019
➜ date
Tue Jun 11 22:24:59 CST 2019

Modify file permissions.

➜ chmod +x file
➜ stat -x file
  File: "file"
  Size: 12           FileType: Regular File
  Mode: (0755/-rwxr-xr-x)         Uid: (  501/ylspirit)  Gid: (   20/   staff)
Device: 1,4   Inode: 8597466429    Links: 1
Access: Tue Jun 11 22:24:52 2019
Modify: Tue Jun 11 22:22:17 2019
Change: Tue Jun 11 22:27:30 2019
➜ date
Tue Jun 11 22:27:36 CST 2019

Linux stat options: (man stat)

 -F      As in ls(1), display a slash (`/') immediately after each pathname that is a directory, an asterisk (`*') after each that is exe-cutable, an at sign (`@') after each symbolic link, a percent sign (`%') after each whiteout, an equal sign (`=') after each          socket, and a vertical bar (`|') after each that is a FIFO.  The use of -F implies -l.  
-f format  Display information using the specified format.  See the FORMATS section for a description of valid formats.  
-L      Use stat(2) instead of lstat(2).  The information reported by stat will refer to the target of file, if file is a symbolic link, and not to file itself.  
-l      Display output in ls -lT format.  
-n      Do not force a newline to appear at the end of each piece of output.  
-q      Suppress failure messages if calls to stat(2) or lstat(2) fail.  When run as readlink, error messages are automatically suppressed.  
-r      Display raw information.  That is, for all the fields in the stat structure, display the raw, numerical value (for example, times          in seconds since the epoch, etc.).  
-s      Display information in ``shell output'', suitable for initializing variables.  
-t timefmt          Display timestamps using the specified format.  This format is passed directly to strftime(3).  
-x      Display information in a more verbose way as known from some Linux distributions.

Linux ls

file atime

➜ ls -lu
total 8
-rwxr-xr-x  1 ylspirit  staff  12 Jun 11 22:24 file

file mtime

➜ ls -l
total 8
-rwxr-xr-x  1 ylspirit  staff  12 Jun 11 22:22 file

file ctime

➜ ls -lc
total 8
-rwxr-xr-x  1 ylspirit  staff  12 Jun 11 22:27 file

Linux ls options: (man ls)

-u      Use time of last access, instead of last modification of the file for sorting (-t) or long printing (-l).  

-c      Use time when file status was last changed for sorting (-t) or long printing (-l).  

-l      (The lowercase letter ``ell''.)  List in long format.  (See below.)  If the output is to a terminal, a total sum for all the file sizes is output on a line before the long listing. 

Usually we can use these three times to sort files or folders, as Linux ls sort.

Add a Comment

Your email address will not be published. Required fields are marked *