Skip to content

Instantly share code, notes, and snippets.

@obsergiu
Created April 1, 2019 15:31
Show Gist options
  • Save obsergiu/eef848b7fa27f9877b7020c056a039c9 to your computer and use it in GitHub Desktop.
Save obsergiu/eef848b7fa27f9877b7020c056a039c9 to your computer and use it in GitHub Desktop.
Bash - Remove files/directories ignoring the one from list
#!/usr/bin/env bash
# Old approach - working but
# the bash approach offers
# greater mobility
#GLOBIGNORE=$(paste -s -d : ./config/ignore.list)
#rm -Rf -- {*,.*}
# new line is mandatory as allows
# to skip substrings
newline='
'
# load the project default files
ignore_list="$newline$(cat ./config/ignore.list)$newline"
# the ignore.list file would have the files one on a line, eg:
# file1.ext
# dir1
# file2.ext
# ...
# echoes the files/directory on first level
# without going recursively
# and removes them
for x in * .[!.]* ..?*; do
case "$x" in *"$newline"*) continue;; esac # sanity check: if the file name contains a newline, leave it alone
case "$ignore_list" in
*"$newline$x$newline"*) echo "Skipping $x";;
*) echo "Delete: $x" && rm -rf -- "$x";
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment