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
In this tutorial you will learn:
- How to check file access and modification time in Linux
|Requirements, Conventions or Software Version Used
|Any Linux distro
|Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – 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.
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).
- The Linux
statcommand 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: -
Birthfield (last line) is not supported on EXT file systems.
- Another tool we can use for the job is the
lscommand. This is already an extremely common command that all Linux users learn during their first day anyway. You will need to add the
-loption 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
- To see the access time for a file with
ls, append the
-uoption 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.
- Yet another tool we can use is the
datecommand. 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
- The nice thing about using the
datecommand 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
$ date -r example.txt +"%Y-%m-%d-%H-%M-%S" 2021-12-15-22-28-53
findcommand 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
findcommand 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
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.
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.