278

I'm new to Linux. I'm using the command-line. I'm trying to view the last modified date of a file. How do I do that in Linux from the Command Line?

4
  • 42
    ls -l also works...
    – Daniel Beck
    Commented Apr 3, 2014 at 12:48
  • 1
    Duplicate of superuser.com/questions/612099/…
    – Oldskool
    Commented Apr 3, 2014 at 13:39
  • 2
    @DanielBeck is the date of ls -l the modified date or the create date? Commented Mar 2, 2017 at 8:47
  • 2
    @BrunoBieri It's the modification date. See man ls. Typical Linux file systems don't even track creation date -- see the accepted answer for the kinds of dates kept track of.
    – Daniel Beck
    Commented Mar 2, 2017 at 10:52

8 Answers 8

271

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: -
1
  • «At first I was confused with Modify and Change, but it's obvious: Modify shows time of last MODIFICATION, at while Change shows time when lastly CHANGED». Ok, but I'm still confused... Commented Aug 6, 2023 at 3:50
99

Use stat command for that:

$ stat file
3
  • 44
    If you want just the last modified date (in human-readable form), use stat -c '%y' file Commented Feb 19, 2015 at 14:53
  • 1
    In addition to @AdamTaylor, date '+%F %T' -d "@$( stat -c '%Y' "$filepath"; )".
    – Artfaith
    Commented Jun 4, 2022 at 6:09
  • @ek9 I think it worths modifying your answer to include Adam Taylor's comment.
    – Marinos An
    Commented Aug 1, 2023 at 17:26
96

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
2
  • 4
    Yes, very helpful, thanks. Here is a bash function that will rename a file to be prefixed by the modified time: function mvfilestime() { if [ x"${1}" = "x" ] ; then echo "mvfilestime: Missing argument of file to mv" else f=$(date +"%Y-%m-%d-%H-%M" -r ${1})-${1} echo mv ${1} ${f} mv ${1} ${f} fi }
    – Traveler
    Commented Apr 17, 2018 at 19:39
  • 3
    iso-8601: date -r foo -u +"%Y-%m-%dT%H:%M:%SZ" (works on Linux and OSX)
    – Gerry
    Commented Jul 3, 2021 at 13:51
26

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
0
11

Building off of @Adam Taylor 's comment in @phoops 's answer and @Sparhawk 's answer.

To specifically just get the date (using October 3, 2019 for examples because it was my last birthday)

  • stat -c %y file | cut -d' ' -f1 will give you 2019-10-03
  • date +%F -r file will also give you 2019-10-03
  • date +%D -r file will give you 10/03/19
  • date +%x -r file will probably give either 10/03/2019, or 10/03/19 if you're in the U.S. and either 03/10/2019, or 03/10/19 if you're in the U.K., just to name a couple examples (of course there are more possibilities)

These date format options are, to my understanding, combinations of other format options. Here are some explanations from the man page:

%b locale's abbreviated month name (e.g., Jan)
%B locale's full month name (e.g., January)
...
%d day of month (e.g, 01)
%D date; same as %m/%d/%y
%e day of month, space padded; same as %_d
%F full date; same as %Y-%m-%d
...
%m month (01..12)
...
%x locale's date representation (e.g., 12/31/99)
...
%y last two digits of year (00..99)
%Y year
...
By default, date pads numeric fields with zeroes.
The following optional flags may follow `%':

- (hyphen) do not pad the field
_ (underscore) pad with spaces
0 (zero) pad with zeros
^ use upper case if possible

use opposite case if possible

N.B.: These flags don't work on the "combo formats" like %F, %D and %x. They are for the "singular field formats".

Apparently this last flag (#) does not work as I'd expect (e.g., if date +%b gives Oct, date +%#b gives OCT as opposed to oCT) I guess this would be useless, but I'd think a lower case option would be more useful. date +%#pdoes turn date +%p which might give PM or AM into pm or am, respectively. So I guess it's not a 'per-character' case switch but sets the case of all the characters in the string to the opposite case of the majority of the characters? Also date +%P gives pm or am, but neither date +%^P nor date +%#P change its output. My guess for this case is that %P is just an alias for %#p, and it seems that whenever you add more than one flag, the behavior is undefined/unpredictable ( e.g., date +%0-e gives the same as date +%-e: 3 and date +%-0e gives the same as date +%0e: 03, which makes you think that only the flag next to the letter works or that it goes left to right, but both date +%#^p and date +%^#p give pm or am, [depending on the time of course] ) unless there's some hidden order of operations? Sorry for digressing...

Also, if you run the command locale -k LC_TIME | grep ^d_fmt, you can see the combo for the specific locale of your system (e.g., d_fmt="%m/%d/%Y").

And you can make your own combo. For example,

  • date +%^b\ %-e\ %Y -r file will give you OCT 3 2019
5

1) List Files directory with Last Modified Date/Time

To list files and shows the last modified files at top, we will use -lt options with ls command.

$ ls -lt /run
output
total 24
-rw-rw-r--.  1 root utmp 2304 Sep  8 14:58 utmp
-rw-r--r--.  1 root root    4 Sep  8 12:41 dhclient-eth0.pid
drwxr-xr-x.  4 root root  100 Sep  8 03:31 lock
drwxr-xr-x.  3 root root   60 Sep  7 23:11 user
drwxr-xr-x.  7 root root  160 Aug 26 14:59 udev
drwxr-xr-x.  2 root root   60 Aug 21 13:18 tuned

https://linoxide.com/linux-how-to/how-sort-files-date-using-ls-command-linux/

2

If the file is on another webserver, I like httpie (docs).

Installation

pip install httpie --user

Usage

The -h command gives only the header. The pattern is

http -h [url] | grep 'Last-Modified\|Date'

Example:

$ http -h https://martin-thoma.com/author/martin-thoma/ | grep 'Last-Modified\|Date'
Date: Fri, 06 Jan 2017 10:06:43 GMT
Last-Modified: Fri, 06 Jan 2017 07:42:34 GMT

The Date is important as this reports the server time, not your local time. Also, not every server sends Last-Modified (e.g. superuser seems not to do it).

1

ls -l shows the file's "modification time" (mtime), the time of last modification of the data in the file.

ls -lc shows file's "change time" (ctime), the time of the last change to the file's status information (name, path, owner, permissions).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .