Monday, November 26, 2012

Sorting "find" results by date

I have been meaning to look this up for a while now. Sometimes I save a file, but I'm not sure in which folder. I use "ls -ltrh" all the time to sort contents of a folder by reverse time so the most recently modified items appear at the bottom (very useful example is ls -ltrh /var/log to find most recently modified log files).

The following command will use the results of  the find command to do the same thing over multiple folders. Its too complex to type all the time, so I put it in a script in my personal script folder. Another good idea would be to alias it.


    find . -type f -printf '%T@ %p\n' | sort -k 1nr | sed 's/^[^ ]* //' | head -n 10

This example shows only the 10 most recent files (head -n 10) and doesn't filter filenames in any way. It also only examines normal files, not folders or other.

No comments:

Post a Comment