keep N recent item in folder

2016-05-26 :: ( 1 minutes reading )

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.

$ (ls -t|head -n 5;ls)|sort|uniq -u|xargs rm

Basically it the (..) will capture together the output

  1. ls -t | head -n 5 will list files sort by time and get first 5 items
  2. The second ls will list all file. Now it contains duplicate items for items we want to keep
  3. sort|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)
  4. xargs rm will change the resulst into space separated value which is argument for rm
comments powered by Disqus

Archives

English

  • My Desktop With i3, polybar, conky, and rofi
  • Convert Intellij Live Template to vscode Snippet
  • Why for-range behaves differently depending on the size of the element (A peek into go compiler optimization)
  • Testing go 1.5 cross compilation on raspberry pi
  • osx-push-to-talk App
  • Tracking origin of bugs with git bisect
  • Stubbing Time.Now() in golang
  • My account just got hacked by Romanian (Possibly)
  • berks upload core dump
  • Safely sharing credentials with PGP

Indonesia

  • Ruby Fiber apaan sih ?
  • Scale MongoDB dengan Sharding
  • Telepon murah ke Indonesia dengan voip

Today I Learned

  • Intellij as git diff and mergetool
  • Checksum a File From a URL
  • Global Gitignore File
  • View kubernetes secret
  • Push Only Current Branch
  • Convert PDF to Text Using OCR
  • Checkout Last Branch
  • Replacing Last Command and Execute It
  • Open Last Command in the Editor With Fc
  • Regex for Validating Password
Labs.Yulrizka.com
twitter github feed
    • Left Panel
    • No Panel
    • Right Panel
  • Home
  • EN
  • ID
  • Today I Learned

© Ahmy Yulrizka 2019. Made with hugo source