1

Is there a way to refer to a directory by its modification date, like cd ~/$newest/subdirectory? I know that I could (and shouldn't?) parse ls, and I currently just use a hacky script that generates symlinks. Short of writing a filesystem is there any way to do this?

For clarity I would like it to act like an actual folder so I can use it in paths in various contexts, i.e. outside bash.

6
  • Can't you adjust the script/program that creates the directory to also update the symlink, that is how programs like py.test give you a link to the latest test-run, while keeping N older runs available.
    – Anthon
    Feb 24, 2016 at 17:12
  • Well that's what I currently do, for a symlink called latest. I could even call it latest0 and then iterate over latest* and increment each. Unfortunately not all the folders are created by the script anymore but some are copied in from elsewhere. Ideally I'd get a general purpose solution, or a strong claim that it's impossible.
    – Sean D
    Feb 24, 2016 at 17:20
  • 1
    There is no creation date, unless your vendor has added something to the traditional stat(2) call (e.g. on Mac OS X) or otherwise stored that information somewhere in the filesystem (ext attributes?).
    – thrig
    Feb 24, 2016 at 17:47
  • 2
    @thrig: ext4 has crtime - aka birth-time , (unless, for some reason, such as fs being <512 MiB, the inode size is old size (of 128 bytes)). stat, at least on Linux, does however not populate this value and one need root privileges to access it, (by e.g. debugfs). Status? IDK superuser.com/a/703927/189803 - but it has been 5 years :P
    – Runium
    Feb 24, 2016 at 18:54
  • 1
    Two options I see: 1. A custom FS (probably FUSE) that does the lookup for you. Difficult, and probably overkill. 2. A background script that watches the directory via inotify and updates links automatically. Probably the best option.
    – Tom Hunt
    Feb 24, 2016 at 19:34

1 Answer 1

2

Here's a skeleton of a shell script:

while inotifywait $some_dir -e create; do
    read _ fname < <(find $some_dir -maxdepth 1 -type d -printf '%T@ %p\n' | sort -n -k1,1 | tail -n1)
    ln -sf $fname $some_dir/latest
done

(Requires GNU find; there's probably some POSIX way to do it, or you could parse ls, or...)

Set this to running in the background, and whenever a directory is created, the symlink latest will be updated to point to the newest mtime directory.

You must log in to answer this question.

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