Problem: I need to delete files and folder from a directory full of files and folders with exceptions of files that I don't want to be deleted.

First attempt:

rm -r !(c_50.tbl sim.sh pam.prp PROINT spy.inp s.bak sr.ccmg sr.drp s.echo s.mdl st.prob SUB.bsub ufile)

Unfortunately, this command delete everything inside the directory

Note: In brackets the files that I don't want delete. ufile is a folder.


There is a missing vertical line "|" (or pipe symbol, vbar, stick, etc.) in between the files listed in the parenthesis.

The command should be:

rm -r !(c_50.tbl|sim.sh|pam.prp|PROINT|spy.inp|s.bak|sr.ccmg|sr.drp|s.echo|s.mdl|st.prob|SUB.bsub|ufile)

What this command does will remove all the files and sub-directories in the current directory except for c_50.tbl or sim.sh or pam.prp etc.

  • 1
    Before the command I had to activate: shopt -s extglob – Davide Sciortino Jan 23 '19 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.