Bash: reverse (i.e. NOT) shell shell expansion expansion?

Is there a way to handle bash v4 shell inversions, i.e. to process all files NOT as a wildcard? I need rm all files that are not in the format of 'Folder-???' in this case, and wondered if there is a shorter (i.e. built-in) way, and then

 for file in * do [[ $i =~ \Folder-...\ ]] && rm '$i' done 

loops. (example does not work, btw ...)

Just from bash studying curiosity ...

+6
source share
3 answers
 shopt -s extglob rm -- !(Folder-???) 
+14
source

@Dimitre has the correct answer for bash. A portable solution is to use case :

 for file in *; do case "$file" in Folder-???) : ;; *) do stuff here ;; esac done 

Another bash solution is to use the pattern matching operator inside [[ ... ]] ( documentation )

 for file in *; do if [[ "$file" != Folder-??? ]]; then do stuff here fi done 
+3
source

If you're fine with find , you can invert find predicates with -not .

As in

 find -not -name 'Folder-???' -delete 

(note that this recursively matches other differences in the glob shell mapping, but I assume you know find pretty good)

+2
source

Source: https://habr.com/ru/post/897834/


All Articles