The Computer Oracle

Get Last Modified Date of File in Linux

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: The Builders

--

Chapters
00:00 Question
00:23 Accepted answer (Score 250)
01:32 Answer 2 (Score 95)
01:44 Answer 3 (Score 86)
02:09 Answer 4 (Score 24)
02:30 Thank you

--

Full question
https://superuser.com/questions/737247/g...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#linux #commandline

#avk47



ACCEPTED ANSWER

Score 292


As mentioned by @edvinas.me, stat tells you various information about the file including the last modified date.

At first, I was confused with Modify and Change, just to clarify, stat output lists:

  • Access shows the time of last data access (e.g. read).
  • Modify shows the time of last data modification.
  • Change shows the time the file status last changed.

For example:

~ $ touch foo
~ $ stat foo
File: ‘foo’
Size: 0             Blocks: 0          IO Block: 4096   regular empty file
Device: fc01h/64513d    Inode: 410397      Links: 1
Access: (0644/-rw-r--r--)  Uid: (80972/ etomort)   Gid: (18429/  eem_tw)
Access: 2015-09-21 12:06:11.343616258 +0200
Modify: 2015-09-21 12:06:11.343616258 +0200
Change: 2015-09-21 12:06:11.343616258 +0200
Birth: -

~ $ echo "Added bar to foo file" >> foo
~ $ stat foo
File: ‘foo’
Size: 42            Blocks: 8          IO Block: 4096   regular file
Device: fc01h/64513d    Inode: 410654      Links: 1
Access: (0644/-rw-r--r--)  Uid: (80972/ etomort)   Gid: (18429/  eem_tw)
Access: 2015-09-21 12:09:31.298712951 +0200
Modify: 2015-09-21 12:09:31.298712951 +0200
Change: 2015-09-21 12:09:31.302713093 +0200
Birth: -

~ $ chmod 444 foo
~ $ stat foo
File: ‘foo’
Size: 42            Blocks: 8          IO Block: 4096   regular file
Device: fc01h/64513d    Inode: 410654      Links: 1
Access: (0444/-r--r--r--)  Uid: (80972/ etomort)   Gid: (18429/  eem_tw)
Access: 2015-09-21 12:09:31.298712951 +0200
Modify: 2015-09-21 12:09:31.298712951 +0200
Change: 2015-09-21 12:10:16.040310543 +0200
Birth: -



ANSWER 2

Score 108


Another way that is more flexible is using date -r. From man date:

-r, --reference=FILE
       display the last modification time of FILE

This has the advantage of allowing you to specify the output format, e.g.

$ date -r foo
Thu Aug 31 10:36:28 AEST 2017
$ date -r foo -R
Thu, 31 Aug 2017 10:36:28 +1000
$ date -r foo -u
Thu Aug 31 00:36:28 UTC 2017
$ date -r foo +%s
1504139788



ANSWER 3

Score 103


Use stat command for that:

$ stat file



ANSWER 4

Score 28


ls -l should do the work.

Example:

#> ls -l /home/TEST/
total 16

-rw-r--r--   1 rfmas1   nms          949 Nov 16 12:21 create_nd_lists.py

-rw-r--r--   1 rfmas1   nms            0 Nov 16 12:35 enb_list

-rw-r--r--   1 rfmas1   nms            0 Nov 16 12:35 nb_list

-rw-r--r--   1 rfmas1   nms            0 Nov 16 12:35 nodes_ip.txt

-rw-r--r--   1 rfmas1   nms            0 Nov 16 12:35 rnc_list