I've opened a file in Emacs. I'd like to view information about that file such as creation date, size, etc. within Emacs. Is there a key shortcut or any Emacs command for that? Thanks.
2 Answers
There doesn't seem to be a command to get this info interactively, although you can use dired as @jrm suggests.
You can get this information with the following elisp code:
(file-attributes (buffer-file-name))
This will return a list like this:
(nil 1 "lh" "users"
(20614 64019 50040 152000)
(20000 23 0 0)
(20614 64555 902289 872000)
122295 "-rw-rw-rw-"
t (5888 2 . 43978)
(15479 . 46724))
The first line gives you the UID "lh" and GID "users".
The next three lines are the last access, modification and status change times, formatted as lists. You can convert those to human-readable strings with current-time-string
:
(current-time-string '(20614 64019 50040 152000))
In this case, the return value is "Tue Oct 23 16:12:03 2012". With that, you can build your own function to extract whichever bits your interested in. If you just want to see the meta data in the minibuffer, this will display it for you:
(defun file-metadata ()
(interactive)
(let* ((fname (buffer-file-name))
(data (file-attributes fname))
(access (current-time-string (nth 4 data)))
(mod (current-time-string (nth 5 data)))
(change (current-time-string (nth 6 data)))
(size (nth 7 data))
(mode (nth 8 data)))
(message
"%s:
Accessed: %s
Modified: %s
Changed: %s
Size: %s bytes
Mode: %s"
fname access mod change size mode)))
Calling this interactively produces:
/home/tws/org/hk.org:
Accessed: Mon May 30 09:38:29 2016
Modified: Thu Apr 21 11:59:06 2016
Changed: Wed May 18 16:09:31 2016
Size: 7508 bytes
Mode: -rwxr-xr-x
More details in the manual (elisp) File Attributes.

- 2,695
- 1
- 18
- 31

- 22,234
- 1
- 54
- 94
-
Thank you for the answer. But I don't know or couldn't manage to enter the first code: (file-attributes (buffer-file-name)) I guess it should be entered inside the minibuffer but how do I switch to minibuffer? I know C-g to switch off but don't know how to switch in. – Terry May 31 '16 at 19:12
-
To run elisp code in the minibuffer, enter
M-:
. You'll seeEval:
in the minibuffer, and then you can type in the code. For the defun,(defun file-metadata...
, you can try that out by pasting it into the scratch buffer, and with point inside the function typingM-C-x
. Then you can call the function withM-x file-metadata
. – Tyler May 31 '16 at 20:11 -
Ops, yeah that worked out. Hey that's useful. As you pointed out it lists file attributes in native form and with converter functions it's possible to extract some more detailed info about the file. I'd select this answer as (very) helpful. Thanks! – Terry May 31 '16 at 21:29
Here is a command that uses dired
with verbose file flags for the ls
command. You may want to customize those flags depending on your operating system.
(defun file-info ()
(interactive)
(let ((dired-listing-switches "-alh"))
(dired-other-window buffer-file-name)))
Create a keybinding to your liking.
(global-set-key (kbd "C-c d") 'file-info)
-
1Thank you for all comments and answers. This one worked out without any problem. I've copied it into ~/.emacs and it started to work. – Terry May 31 '16 at 18:36
C-x d RET
will opendired
in the directory of visited file. It won't show information specifically about the visited file, but all files in the directory. – jrm May 31 '16 at 16:21M-x eval-expression RET (dired-other-window buffer-file-name) RET
The command switches for "ls" that dired uses is customizable. – lawlist May 31 '16 at 16:28(global-set-key (kbd "C-c d") 'file-info)`
– jrm May 31 '16 at 16:40dired-listing-switches
so that the OP can display any file attribute that the applicable version of "ls" supports. – lawlist May 31 '16 at 17:11