0

Can I know how to remove all *.sln file but exclude "work.sln" in the same folder I try run rm *.sln !("work.sln") it return /bin/bash: eval: line 128: syntax error near unexpected token ('`

Thank you

Emily
  • 103
  • 8

1 Answers1

-1

Assuming you are already inside the folder that contains the files you need, this is one way to do it (also assuming you are looking for a one-line version, given your example)

for file in $(ls);do if [[ ! $file == work.sln ]]; then rm $file; fi; done

EDIT: as @tripleee pointed out, the ls can be avoided and re-written like this

for file in *; do if [[ ! $file == work.sln ]]; then rm $file; fi; done
Revje
  • 66
  • 1
  • 6
  • 1
    That's a [useless `ls`](https://www.iki.fi/era/unix/award.html#ls); you want simply `for file in *` (or for robustness `for file in ./*` but then you have to update the rest of the code to include the `./` everywhere). – tripleee Aug 27 '22 at 15:35
  • Thanks! I didn't know it could be written like that – Revje Aug 27 '22 at 15:37