Search code examples
bashxargsrm

rm except last 5 files bash


I found 5 last core files.

I need to delete all core files except these 5 files.

ls -t /u01/1/bin/core.siebprocmw.* |head -n 5

command to find 5 last files by time.

ls -t /u01/1/bin/core.siebprocmw.* |head -n 5 |xargs rm -r

command remove found last 5 files.

I need to delete all files except these last 5 files. Any ideas?


Solution

  • You could use sed to exclude the first five newest files, then delete the rest:

    ls -t /u01/1/bin/core.siebprocmw.* | sed '1,5d' | xargs rm -r