Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last modified time of file in linux

I am working on a C program where I need to get the last modified time of the file. What the program does is a function loops through each file within a directory and when a particular file(s) is found it calls another function to check that the last modified times of the file.

Within the directory there is a mylog.txt.1, mylog.txt.2 and mylog.txt.3 etc. When I list the directory in linux using the ll command I can see that mylog.txt.1 and mylog.txt.2 were modified on the 4th May and mylog.txt.3 was modified on the 3rd May.

When the program checks each of these files however, it is always returning 3rd may. Below is the code that I am using.

void getFileCreationTime(char *filePath)
{
    struct stat attrib;
    stat(filePath, &attrib);
    char date[10];
    strftime(date, 10, "%d-%m-%y", gmtime(&(attrib.st_ctime)));
    printf("The file %s was last modified at %s\n", filePath, date);
    date[0] = 0;
}

I've tried all the different variations of st_ctime, i.e. st_mtime and st_atime but they all return 3rd may.

Thanks for any help you can provide.

like image 396
Boardy Avatar asked May 04 '12 09:05

Boardy


People also ask

How do you check when the last time a file was modified in Linux?

The syntax is pretty simple; just run the stat command followed by the file's name whose last modification date you want to know, as shown in the example below. As you can see, the output shows more information than previous commands.

How do I find the last modified date of a file?

The File. lastModified read-only property provides the last modified date of the file as the number of milliseconds since the Unix epoch (January 1, 1970 at midnight). Files without a known last modified date return the current date.

How do I find file creation and modification date time in Linux?

Using ls -l command The ls -l command is usually used for long listing - display additional information about a file such as file ownership and permissions, size and creation date. To list and display the last modified times, use the lt option as shown.


3 Answers

This worked fine for me:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>

void getFileCreationTime(char *path) {
    struct stat attr;
    stat(path, &attr);
    printf("Last modified time: %s", ctime(&attr.st_mtime));
}
like image 73
qwertz Avatar answered Oct 07 '22 01:10

qwertz


This is one of those cases where timezones matter. You're getting gmtime of the st_mtime. You should instead be using localtime viz.

strftime(date, 20, "%d-%m-%y", localtime(&(attrib.st_ctime)));

this is because ls uses your timezone information, and when you used gmtime as part of the display, it deliberately omitted any timezone information.

like image 21
Petesh Avatar answered Oct 06 '22 23:10

Petesh


Things to fix:

  • Use the proper field, i.e. st_ctime.
  • Check that stat() succeeds before using its result.
  • Use strftime(date, sizeof date, ... to remove the risk of using the wrong buffer size.

I first suspected that your filesystem simply didn't support tracking the last-modified time, but since you say that other tools manage to show it, I suspect the code is breaking for whatever reason.

Could it be that the filenames are not full path names, i.e. they don't include the proper directory prefix?

like image 26
unwind Avatar answered Oct 07 '22 00:10

unwind