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
statcommand 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.
statcommand should be followed by the name(or the full path) of the file as shown below and it will produce the following output.
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
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
-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
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
-roption followed by the name of file will display the last modified date and time of the file.
date -r demofile.txt
-r option is used to display the last modification date of the file. Above command will produce following output
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.
date cannot be used without any option.
Method 3: Using ls command
lscommand is used for listing files and folders and its
-loption 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.
ls -l demofile.txt
Hit the clap if you liked the article