Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash: Inverse (ie. NOT) shell wildcard expansion?

Tags:

bash

glob

Is there a way of handling invers bash v4 shell expansion, ie. treat all files NOT like a wildcard? I need to rm all files that are not of the format 'Folder-???' in this case and was wondering if there is a shorter (ie. built-in) way then doing a

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

loop. (the example doesn't work, btw...)

Just out of bash learning curiosity...

like image 874
Christian Avatar asked Sep 22 '11 14:09

Christian


People also ask

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

What does if (- E mean in bash?

The “if –e” and “if –s” are such operators in Bash, used for testing the existence of a file. The difference between the two is that the former only tests the existence of a file, whereas the latter also checks if there are any contents in that file or not.

What is Globbing in bash?

The Bash shell feature that is used for matching or expanding specific types of patterns is called globbing. Globbing is mainly used to match filenames or searching for content in a file. Globbing uses wildcard characters to create the pattern.


1 Answers

shopt -s extglob
rm -- !(Folder-???)
like image 79
Dimitre Radoulov Avatar answered Oct 07 '22 20:10

Dimitre Radoulov