bash: list newest files filtered by date and file name

I thought this was pretty nifty so I figured I’d put it in a post.

The problem: you want to copy some files, but only the newest and of a certain type, such as, only .txt files.

The solution: The trick is to use ls -lrt to sort by and show the date and count the number of files. Then you just pass that number to tail, and it only grabs those files! Furthermore, you can add a grep at the end to only select the files types you want.


ls -rt directory/*.txt' | tail -n `ls -lrt directory/*.txt' | grep "Nov 30" | wc -l`

Note: If you grep for a date, those files have to be the most recent in the list for this to work.


About this entry