how to remove all files except one in linux

C++
# delete all except the filename

$ rm -v !("filename")
# if you are using a loop

shopt -s extglob

rm -rf !("abc.txt" | "abc.log" )  # others will be removed except these two

shopt -u extglob 
$ rm -v !("filename1"|"filename2") 

Source

Also in C++: