Shell – CLI equivalent of Gnome’s recent files feature

directoryfilesshell-script

In Gnome's File manager (Nautilus), there's a feature called "Recent Files" which presents itself like a "virtual directory" of sorts, listing the most recently created/modified files in the user's home directory.

I am looking for something equivalent on the CLI. i.e. a virtual folder which can be navigated to, but which presents dynamic results based on the output of say, the find command.

My need arises from the fact that I use emacs for email and one needs to specify a path to each file attachment, hence sending file attachments from different folders is a pain. Life would be nicer if there was a single virtual directory which I knew would have all the recently created/modified files.

If there isn't a ready tool for this, I'd write a script to run the find command searching for the most recent files in the $HOME directory, and create a virtual folder containing symlinks to the files output by find; and run that as a cron or use inotify.

However, it would be lovely if there is already a tool to do this job.

Best Answer

This will take the recently-used files referenced in ~/.local/share/recently-used.xbel (or rather, ${XDG_DATA_HOME}/recently-used.xbel), and link them all into a directory called ~/recent:

#!/bin/sh
set -e
mkdir -p ~/recent
rm -f ~/recent/*       # Make sure you don’t have anything you care about here
xmlstarlet sel -t -m '/xbel/bookmark[starts-with(@href, "file://")]' \
    -v 'substring(@href, 8)' -n ${XDG_DATA_HOME:-~/.local/share}/recently-used.xbel |
python -c "import sys, urllib as ul;
sys.stdout.write(ul.unquote(sys.stdin.read().replace('\n', '\0')));" |
xargs -0 ln -st ~/recent

This uses XMLStarlet to extract the file URIs from the list of recently-used documents (ignoring other URIs), feeds them to a Python script which replaces newlines with nul characters and then unquotes the escaped URIs (e.g. + or %20 instead of space), and finally feeds that to xargs which splits all the file names and feeds them to ln (the GNU variant) to create symbolic links.

Note that links will be created regardless of whether the target file still exists; it often happens that the list of recently-used files includes temporary files which have since been deleted.

Related Question