Thursday, March 28, 2019

Tuesday, March 19, 2019

To move the five most recent files to a subdirectory

ls -rt will show you the files most recent first.

To move the most recent five to a subdirectory:

mkdir -p recent && find -maxdepth 1 -type f -printf '%T@ %P\n' | sort -rn | cut -d' ' -f2- | head -n 5 | xargs -I{} mv {} recent

Monday, March 4, 2019

To Find the Most Recent Version of a File

Occasionally it so happens that you want to find the most recent version of thing.

Suppose that you're trying to find your most recent timesheet.

Then:

find ~ -type f -name "*timesheet*" -printf "%T@ %t %p\n" | sort -n 

does the business.

Output looks something like:

1519655146.5096155360 Mon Feb 26 14:25:46.5096155360 2018 /home/john/data/.~/timesheet.py.~86~
1519925511.1565392570 Thu Mar  1 17:31:51.1565392570 2018 /home/john/data/.~/timesheet.py.~87~
1520000595.5182763930 Fri Mar  2 14:23:15.5182763930 2018 /home/john/data/timesheet.py

Where the first column is a sortable version of the last modification time "%T@", and the second "%t" is the human readable form.

The last thing printed will be the most recent file in your home directory tree with timesheet in the name.