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.

  • C-x d RET will open dired 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:21
  • 1
    How about?: M-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
  • You could also put that in a function and add a keybinding. (defun file-info () (interactive) (dired-other-window buffer-file-name)) (global-set-key (kbd "C-c d") 'file-info) – jrm May 31 '16 at 16:40
  • 1
    I would encourage @jrm to post an answer similar to the above comment that includes the let-bound variable dired-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
  • Thank you for all comments and efforts. The one suggested by jrm worked just out of the box. – Terry May 31 '16 at 18:40

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 ()
  (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)
  • 1
    Thank 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

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 ()
  (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)))
  Accessed: %s
  Modified: %s
  Changed: %s
  Size: %s bytes
  Mode: %s"
     fname access mod change size mode)))

Calling this interactively produces:

  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.

  • 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 see Eval: 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 typing M-C-x. Then you can call the function with M-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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.