2020-04-01-prune.md 1.2 KB


layout: post title: prunefiles subtitle: An easy way to keep your repositories clean tags:

  • prune
  • git
  • repositories
  • fedora ---

Overview

Here's a simple script/function to keep n number of the latest files that match your argument(s) in the current directory. I wrote this in order to periodically clean rpm and deb repositories. It will create file lists by matching one or more input arguments and keep only the latest file from each list. The number of files to be kept for each matching list can be set using -k. The file can safely be sourced as a function.

Code

prunefiles:

{% insert_git_code https://git.bryanroessler.com/bryan/scripts/raw/master/prunefiles %}

Example

$ ls
Package-25-1.rpm
Package-25-2.rpm
Package-25-3.rpm
Package-25-4.rpm
Package-26-1.rpm
Package-26-2.rpm
Package-27-1.rpm
Package-27-2.rpm
$ prunefiles Package-25 Package-27
$ ls
Package-25-4.rpm
Package-26-1.rpm
Package-26-2.rpm
Package-27-2.rpm

Conclusion

I have provided a prunefiles script/function that keeps n number of latest files in the current directory that match any number of input argument(s).