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.
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
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
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.
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
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.
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
Hit the clap if you liked the article