• 0 Posts
  • 1 Comment
Joined 1 year ago
cake
Cake day: June 26th, 2023

help-circle
  • The first command (ls -1 /backup/*.dump) just creates a list of files in the backup folder that have the extension .dump. the output of the prior command is then sent to the next command (head -n -2) this cuts the list down to everything except the last 2 items in the list this is then sent to the final command which takes the list and runs the final (rm -f) command with the items in the list as the targets to delete.

    heres a solution based on this post https://stackoverflow.com/questions/25785/delete-all-but-the-most-recent-x-files-in-bash

    ls -tp /backup/*.dump | grep -v ‘/$’ | tail -n +4 | tr ‘\n’ ‘\0’ | xargs -0 rm -f

    There is an explanation on that post that explains it in better detail but in simple terms it deletes all files but the most recent 3 files in the directory that have the .dump extension