Wednesday, October 3, 2012

Create symlink to the most recent directory

Recently I needed to find a way to create a symlink to the most recent directory so it can be synced up to some other place. Here is the command in linux which can do that:


find /home/ -maxdepth 1 -mindepth 1 -type d -printf '%TY-%Tm-%Td %TT %p\n' | sort -r | cut -d ' ' -f 3 | head -n 1 | awk -F" " '{print "ln -s "$1" latest-dir"}'| bash

The command above creates a symlink to the most recent directory under /home/. As usual, first try it without "| bash" at the end, so you can test what does it print out.

No comments:

Post a Comment