Way to access the “Recent Items” list from the terminal

finderterminal

The Apple menu "Recent Items" entry is handy, but at times I would like to be able to access it via command line, e.g. to change to a directory I was just working in with a GUI app. Is there a way to get access to at least the last open file or folder from Desktop to terminal?

Failing that, is there a way to access a smart folder from the command line (for example, if I have one that consists of files modified in the past 2 days).

Best Answer

You can read the recent items plist file.

    defaults read $HOME/Library/Preferences/com.apple.recentitems

There's a lot of "noise" in the output. You can filter the "noise" with grep and list the last entry with tail which should be the last file opened (but not always the case).

    defaults read $HOME/Library/Preferences/com.apple.recentitems | grep "Name" | tail -n 1