Bash Script to find the most recently modified file Bash Script to find the most recently modified file bash bash

Bash Script to find the most recently modified file


I prefer this for finding the most recently modified file:

find . -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort


NEWEST=for f in /Volumes/A/*.bkf /Volumes/B/*.bkfdo    if [ -z "$NEWEST" ]    then         NEWEST=$f    elif [ "$f" -nt "$NEWEST" ]     then        NEWEST=$f    fidone


Goes through some twists just to make sure to handle filenames with odd characters well, which mouviciel's doesn't:

NEWEST=$(find /Volumes/A /Volumes/B -name '*.bkf' -printf '%T@ %p\0' | \    sort -rnz | xargs -0n1 2>/dev/null | head -n1 | cut -d' ' -f2-)[[ -n "$NEWEST" ]] && cp -v "$NEWEST" /other-location

Actually, since these files are coming from Windows and are thus pretty much guaranteed not to have odd characters in their names (like embedded newlines),

NEWEST=$(find /Volumes/A /Volumes/B -name '*.bkf' -printf '%T@ %p\n' | \    sort -rn | head -n1 | cut -d' ' -f2-)[[ -n "$NEWEST" ]] && cp -v "$NEWEST" /other-location