1. Overview

When we work with directories in the Linux command line, we sometimes need to know the last modification time of a directory.

In this tutorial, we’ll learn how to get a directory’s last modification time.

Moreover, we’ll discuss what changes to a directory will affect the modification time.

2. Getting the Last Modification Time of a Directory

In Linux, sometimes we use the short form “mtime” to indicate the last modification/change time of a file.

In this tutorial, we’ll address two commands to read the last modification time of a directory: the stat command and the date command.

Next, let’s see them in action.

2.1. Using the stat Command

The stat command is a great utility to report file or file system status. Using this command on a file is pretty straightforward:

$ stat myDir
  File: myDir
  Size: 40            Blocks: 0          IO Block: 4096   directory
Device: 23h/35d    Inode: 87601       Links: 2
Access: (0755/drwxr-xr-x)  Uid: ( 1000/    kent)   Gid: ( 1000/    kent)
Access: 2021-08-31 20:59:05.895057894 +0200
Modify: 2021-08-31 20:59:05.895057894 +0200
Change: 2021-08-31 20:59:05.895057894 +0200
 Birth: -

As the output above shows, the stat command displays the status of the myDir directory.

The last modification time is shown in the line:

Modify: 2021-08-31 20:59:05.895057894 +0200

Using the stat command, we can also control the output by the -c FORMAT option. 

There are two formats to display the mtime:

  • %y – displays time of last data modification in a human-readable format
  • %Y – displays time of last data modification in number of seconds since Epoch

Let’s test the two options with the myDir directory:

$ stat -c %y myDir
2021-08-31 20:59:05.895057894 +0200

$ stat -c %Y myDir
1630436345

2.2. Using the date Command

The date command from the Coreutils package supports the -r option to extract the last modification time of the given file. 

Also, we can control the output format using date +FORMATS.

Now, let’s see a couple of examples of using date to print the last modification time of the myDir directory:

$ date -r myDir +"%F %T.%3N" 
2021-08-31 20:59:05.895

$ date -r myDir +"%s"
1630436345

As the example above shows, we can print the mtime of myDir using the date command in human-readable and in seconds since Epoch formats.

As we can see, no matter whether we use the stat or the date command, it’s pretty straightforward to get the last modification time of a file or directory.

Next, let’s move to an interesting topic: What changes will affect the last modification time of a directory?

3. What Will Affect the Last Modification Time of a Directory

The “last modification time”, as the name implies, records the time of the last modification to the directory. However, not all modifications to the directory will affect the last modification time.

Sometimes, this may not be very clear.

First, let’s create a directory as our example and put some files and subdirectories under it:

$  tree myDir 
myDir
├── file1.txt
├── file2.txt
├── file3.txt
├── subDir1
│   ├── sub1_file1.txt
│   └── sub1_file2.txt
└── subDir2
    ├── sub2_file1.txt
    └── sub2_file2.txt

2 directories, 7 files

Next, let’s take a closer look at it.

3.1. Renaming the Directory

Probably, we think if we’ve renamed the directory, of course, we’ve modified it. So, the last modification time should be updated.

Now, let’s see if it works in this way:

$ stat -c %Y myDir
1630440905
$ mv myDir myDirRenamed
$ stat -c %Y myDirRenamed 
1630440905

As the test output shows, the last modification time is the same before and after our renaming operation.

Therefore, renaming a directory will not change its last modification time.

3.2. Changing the Permission of the Directory

We may think that if renaming the directory won’t update the last modification time, then changing its permission should affect its mtime.

Next, let’s do a test:

$ stat -c %Y myDir
1630440905
$ ls -ld myDir
drwxr-xr-x 4 kent kent 140 Aug 31 22:15 myDir/
$ chmod +777 myDir
$ ls -ld myDir
drwxrwxrwx 4 kent kent 140 Aug 31 22:15 myDir/
$ stat -c %Y myDir
1630440905

It turns out that changing the directory’s permission will not affect its last modification time.

3.3. Changing the Content of a File Under the Directory

Next, let’s test if changing the content of a file under the directory will update its mtime:

$ stat -c %Y myDir
1630440905
$ echo "Adding some new data" >> myDir/file1.txt
$ stat -c %Y myDir                              
1630440905

In the test, we’ve edited a file under myDir. However, myDir‘s mtime is not updated.

Therefore, changing the content of a file under the directory will not change the directory’s last modification time.

We should note that sometimes we use the find command with the -mtime option to search files that haven’t been modified for some time.

For example, we can list directories that haven’t been modified in 30 days:

find directory -type d -mtime +30

However, we should be careful when we use the result for further processing. This is because all directories in the result could contain files that have been changed in the last 30 days.

3.4. Adding New Files or Subdirectories

Next, we’ll test if adding a new file or a new subdirectory to the directory will change its mtime.

First, let’s test the adding new files scenario:

$ stat -c %Y myDir
1630440905
$ touch myDir/newFile.txt
$ stat -c %Y myDir
1630442785

As the test shows, myDir‘s last modification time has been changed after adding a new file under it!

Next, let’s add a new subdirectory:

$ stat -c %Y myDir
1630442785
$ mkdir myDir/newSub
$ stat -c %Y myDir
1630442998

From these two tests, we know now that adding files or subdirectories to the directory will change its last modification time.

3.5. Removing Files or Subdirectories

Next, let’s test whether removing a file or subdirectory from myDir will update its mtime.

First, let’s remove the file we just created:

$ stat -c %Y myDir
1630442998
$ rm myDir/newFile.txt
$ stat -c %Y myDir
1630443387

We can see that myDir‘s mtime has been changed.

Next, let’s remove a subdirectory:

$ stat -c %Y myDir
1630443387
$ rm -r myDir/newSub
$ stat -c %Y myDir
1630443522

Therefore, removing files or subdirectories from the directory will update its last modification time.

3.6. Renaming Files or Subdirectories Under a Directory

We can also do a similar test and see that renaming files or subdirectories under the directory will change its last modification time, too:

$ stat -c %Y myDir
1630443522
$ mv myDir/file1.txt newName1.txt
$ stat -c %Y myDir
1630443643

$ stat -c %Y myDir
1630443643
$ mv myDir/subDir1 myDir/subDirNew
$ stat -c %Y myDir
1630443809

4. Conclusion

In this article, we’ve learned how to get the last modification time of a directory under the Linux command line. Both the date and the stat commands are pretty convenient for this task.

Further, we’ve discussed what operations will change a directory’s last modification time. Let’s summarize it in a table for better comparison:

Operation

Updates directory (myDir)’s mtime

Renaming myDir

No

Changing myDir’s Permission

No

Changing the content of files under myDir

No

Adding, removing, or renaming files or subdirectories under myDir

Yes