Ви можете використовувати findзамість своєї оболонки:
find . -mindepth 1 -maxdepth 1 ! -name "*.dmg" -delete
Від man find:
! expr True if expr is false. This character will also usually need
protection from interpretation by the shell.
-name pattern
Base of file name (the path with the leading directories removed)
matches shell pattern pattern.
-delete
Delete files; true if removal succeeded. If the removal failed,
an error message is issued. If -delete fails, find's exit status
will be nonzero (when it eventually exits). Use of -delete
automatically turns on the -depth option.
Якщо ви не можете скористатися findз будь-якої причини, ось спосіб зробити це zsh(або іншими оболонками). zshБудучи zsh, мабуть, є простіший спосіб зробити це, але оскільки я bashхлопець, це я придумав:
for file in *; do if [[ ! "$file" == *.dmg ]]; then rm $file; fi; done
rm -r secrets/!(directory)він постійно проситьnumber expectedабо іноді це дає меніevent not found: directory