Search code examples
bashsynology

Synology diskstation bash script: delete old files (leave only 10 newest files)


This works to delete files older then 14 days

find /path/to/share -type f -iname "*.*" -mtime +14 -exec rm -f {} \;

I need to modify, leave only 10 latest files, no matters how old are they


Solution

  • find /path/to/share -type f -iname "*.*" | sort | sed -n -e :a -e '1,10!{P;N;D;};N;ba' | xargs rm
    

    PS If you not need sort, remove it.