Skip to main content
1 of 6
mcp
  • 857
  • 1
  • 7
  • 19

I made a modular alias for this, based on a compilation of findings:

rmexcept()
{
    files=()
    for pattern in "$@"
    do
        files+=(`find . -type f -not -iname "$pattern"`)
    done

    printf "%s\n" ${files[@]} | sort | uniq -d | xargs rm
}

It is designed to work with multiple pattern arguments:

rmexcept *.tex *.pdf

The way I put the find results in an array might not be best practice. See this thread for more details.

mcp
  • 857
  • 1
  • 7
  • 19