I\'m trying to delete some files and folders from a directory. The command (from here):
rm -rf !(file_i_don\'t_want_to_remove|other|other_one)
You need the extglob extension enabled for this syntax to work:
#!/bin/bash
shopt -s extglob
cd /path/to/dir # See the comment below the answer
rm -rv !(one|two)
PS: Do not use rm -f
in a script.
PS: If /path/to/dir
is coming from a variable, make sure that is is not empty before using it with cd
:
if [ -z "${path_to_delete}" ] ; then
"path_to_delete is empty! Aborting"
exit 1
fi
cd "${path_to_delete}"
rm -rv !(one|two)