Check file access and modification time in Linux

The purpose of this tutorial is to show show several command line methods you can use to check file access and modification time on a Linux system.

Check the examples below as we cover several tools for the job such as ls, date, stat, and find.

In this tutorial you will learn:

  • How to check file access and modification time in Linux
Viewing the access and modification time for a file in Linux
Viewing the access and modification time for a file in Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Check file access and modification time in Linux examples




The Linux command line comes with several tools we can use to check file access and modification time. It will not be necessary to install any extra tools. Check out some of the examples below for various ways to check file access and modification time on Linux.

DID YOU KNOW?
The access time is when a file was last accessed (read or modified). The modified time is when a file last had its contents modified. And the change time is when a file last had its metadata changed (such as file permissions or the name of the file).
  1. The Linux stat command will show us the access time, modification time, and change time of a file. Just specify the path to a file in your command.
    $ stat example.txt
    

    The output will look something like this. The bolded lines contain the relevant information.

      File: example.txt
      Size: 13367     	Blocks: 32         IO Block: 4096   regular file
    Device: 805h/2053d	Inode: 787524      Links: 1
    Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
    Access: 2021-12-15 22:28:53.480000095 -0500
    Modify: 2021-12-15 22:28:53.480000095 -0500
    Change: 2022-02-20 19:48:33.288001148 -0500
     Birth: -
    

    Note the Birth field (last line) is not supported on EXT file systems.

  2. Another tool we can use for the job is the ls command. This is already an extremely common command that all Linux users learn during their first day anyway. You will need to add the -l option to the command in order to see the modification time.
    $ ls -l example.txt
    -rw------- 1 root root 13367 Dec 15 22:28 example.txt
    
  3. To see the access time for a file with ls, append the -u option in your command.
    $ ls -u example.txt
    -rw------- 1 root root 13367 Dec 15 22:28 example.txt
    

    In this case, our access time is the same as the file’s modified time, which is normal for files that have not been accessed since they were last saved.

  4. Yet another tool we can use is the date command. It is not necessary to specify any extra options, as the modified time will be output in very human readable format, but we will show you why you might want to use extra options in the next example.
    $ date -r example.txt
    Wed 15 Dec 2021 10:28:53 PM EST
    
  5. The nice thing about using the date command is that we can choose the format that we want our modified date to be output in. This makes it very friendly to use inside a Bash script or other type of automation. For example, here we get the modified date in the format of YYYY-MM-DD-HH-MM-SS.


    $ date -r example.txt +"%Y-%m-%d-%H-%M-%S"
    2021-12-15-22-28-53
    
  6. The find command comes in handy if we need to see all files that contain a certain modification time. For example, this command will display all files that have been modified in the last seven days.
    $ find . -mtime -7
    

    Or files modified within the last hour:

    $ find . -mmin 60
    
  7. The find command can also be used to search for files with a particular access time. For example, this command will search for files accessed within the last 20 minutes.
    $ find ~ -amin 20
    

    Or we can search for files with an access time later than 20 minutes ago by changing to a plus sign.

    $ find ~ +amin 20
    

Closing Thoughts

In this tutorial, we saw how to check the access and modification time for a file in Linux. There are several tools that can do the job, but each of them have their own pros and cons, depending on exactly what you would like to do.




The stat command gives us all relevant information, but ls is probably a command you are more used to utilizing every day. Then again, the date command may work better in Bash scripts or in situations where you need the output formatted a certain way. Finally, the find command works best when searching for files within a certain modified or access date range.



Comments and Discussions
Linux Forum