Skip to content

Instantly share code, notes, and snippets.

@Da-Juan
Created June 6, 2018 08:39
Show Gist options
  • Save Da-Juan/a9cd992f1dd9700632f3c5e800c3acb5 to your computer and use it in GitHub Desktop.
Save Da-Juan/a9cd992f1dd9700632f3c5e800c3acb5 to your computer and use it in GitHub Desktop.
Remove all files except some
#!/bin/bash
# Enable Extended Globbing
shopt -s extglob
# Simple version
rm !(a|b|c)
# Scriptable version
function join_by {
local IFS="$1"
shift
echo "$*"
}
KEEP=( "a" "b" "c" )
rm !($(join_by "|" "${KEEP[@]}"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment