keep n recent item in folder
Sometimes you create a script that generate something but you wanted to only keep N last item. There is couple way to achive this. This version works both on OSX and Linux.
Basically it the (..)
will capture together the output
ls -t | head -n 5
will list files sort by time and get first 5 itemsThe second
ls
will list all file. Now it contains duplicate items for items we want to keepsort|uniq -u
will sort it, and remove those duplicate file in step 1. At this point it will contains only files that we want to delete (everything except files from step 1)xargs rm
will change the resulst into space separated value which is argument forrm
Previousjq extracting properties to arrays from json row lineNextopen last command in the editor with fc
Last updated