In one of my programs I call a bash command to remove all files in the directory, but some:bash -cshopt -s extglobshopt -s nullglobrm /path/to/directory/!(file1 | file2)Which for what I understand mean Use bash on this command, use the extended pattern, remove all files but thoseIt works good, but deletes every file but the ones put in the command.What I would like is to remove all .mp4 and .zip in the directory but the ones I put in the command.I think I'll have to add *.mp4 and *.zip somewhere in the command, but I have no idea where.EDIT: For clarity, what I need isDelete all .mp4 and .zip files, but keep thoses
Remove all *.mp4 and *.zip but some files
bash;shell;rm
The pattern !(file1 | file2) matches any file except file1 or file2 where  is a space. The way you quoted it is misleading: it's equivalent to !(file1|file2) or !(file1|file2)`.There's no way in bash to combine a negative pattern with a positive pattern to say all of these except for those. In zsh, you can writesetopt extended_globrm /path/to/directory/*.(mp4|zip)~(keep*|1234.zip)to remove this.mp4 and that.zip but not keep-this.mp4 or 1234.zip.In bash, you can set the GLOBIGNORE variable to exclude certain patterns from all matches.shopt -s extglobGLOBIGNORE='keep*:1234.zip'rm /path/to/directory/*.(mp4|zip)unset GLOBIGNOREAlternatively, you can use find, which lets you build arbitrarily complex boolean expressions.find /path/to/directory \( -name '*.mp4' -o -name '*.zip' \) ! -name 'keep*' ! -name '1234.zip' -deleteNote that find recurses into subdirectories. If this isn't desired, add -maxdepth 1 after /path/to/directory.If your find doesn't understand -maxdepth (it's common but not standard), use -type d -prune -o instead. If your find doesn't understand -delete (same remark), use -exec rm {} + instead.