How to get last modified date of a file in linux

Many times you need to check the time a file or folder was last modified to ensure that you have the latest and required file in place.
There are many ways to get the last modified date time of a file in linux, unix or mac systems from command line terminal and this article will list them out with examples.

Method 1: Using stat command
stat command is used for checking a file status and returns file details such as its size, permissions, date created, date on which it was last accessed and the last modified date of the file.
stat command should be followed by the name(or the full path) of the file as shown below and it will produce the following output.

stat demofile.txt

stat demofile.txt
File: demofile.txt
Size: 4240 Blocks: 16 IO Block: 4096 regular file
Device: 806h/2054d Inode: 2236484 Links: 1
Access: (0755/-rwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2019-08-08 17:10:05.713608794 +0530
Modify: 2019-05-06 17:31:03.605066693 +0530
Change: 2019-05-06 17:31:03.605066693 +0530
Birth: –

You can see the last modified date in the output above.
If you do not want the detailed output and you are concerned with only the last modified data, then use the below command.

stat -c ‘%y’ demofile.txt

where -c flag is used to display data in custom format and %y is used to displaying time of last modification. Thus, this command will only output

2019-05-06 17:31:03.605066693 +0530

If last modified date of a folder needs to be checked, then the file name should be replaced by the name of the folder.
Note:
If you are not in the same directory as the location of file, then provide the full path of file instead of the file name.

Method 2: Using date command
date command with -r option followed by the name of file will display the last modified date and time of the file.
Example,

date -r demofile.txt

where -r option is used to display the last modification date of the file. Above command will produce following output

Mon May 6 17:31:03 IST 2019

which is the last modified date and time of the given file. date command can also be used to determine the last modified date of a directory.
Unlike stat command, date cannot be used without any option.

Method 3: Using ls command
ls command is used for listing files and folders and its -l option displays the file and directory names along with other details such as file and folder permissions, owner user, date modified etc.
When this command is issued,

  • without any argument, then it will list all the files and folders in the current directory.
  • with a file name, then it will list the details of that file only.
  • with a directory name, then it will list the details of all files in that particular directory.

Example,

ls -l demofile.txt

-rwxr-xr-x 1 root root 4240 May 6 17:31 demofile.txt

Hit the clap if you liked the article

Leave a Reply