Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using wildcards to exclude files with a certain suffix

I am experimenting with wildcards in bash and tried to list all the files that start with "xyz" but does not end with ".TXT" but getting incorrect results.

Here is the command that I tried:

$ ls -l xyz*[!\.TXT]

It is not listing the files with names "xyz" and "xyzTXT" that I have in my directory. However, it lists "xyz1", "xyz123".

It seems like adding [!\.TXT] after "xyz*" made the shell look for something that start with "xyz" and has at least one character after it.

Any ideas why it is happening and how to correct this command? I know it can be achieved using other commands but I am especially interested in knowing why it is failing and if it can done just using wildcards.

like image 800
Ramesh Samane Avatar asked Jul 11 '12 16:07

Ramesh Samane


1 Answers

These commands will do what you want

shopt -s extglob
ls -l xyz!(*.TXT)
shopt -u extglob

The reason why your command doesn't work is beacause xyz*[!\.TXT] which is equivalent to xyz*[!\.TX] means xyz followed by any sequence of character (*) and finally a character in set {!,\,.,T,X} so matches 'xyzwhateveryouwant!' 'xyzwhateveryouwant\' 'xyzwhateveryouwant.' 'xyzwhateveryouwantT' 'xyzwhateveryouwantX'

EDIT: where whateveryouwant does not contain any of !\.TX

like image 76
Nahuel Fouilleul Avatar answered Oct 13 '22 00:10

Nahuel Fouilleul