SiegeX
SiegeX

Reputation: 140377

Bash Bug? Can't use negate extglob !(*/) to filter out directories?

If the glob */ only matches directories, then logically the extglob !(*/) should match non-directories; but this doesn't work. Is this a bug or am I missing something? Does this work on any shell?

Test 1 to prove that */ works

$ cd /tmp; ls -ld */
drwxr-xr-x  2 seand users 4096 Jan  1 15:59 test1//
drwxr-xr-x  2 seand users 4096 Jan  1 15:59 test2//
drwxr-xr-x  2 seand users 4096 Jan  1 15:59 test3//

Test 2 to show potential bug with !(*/)

$ cd /tmp; shopt -s extglob; ls -ld !(*/)
/bin/ls: cannot access !(*/): No such file or directory

Upvotes: 3

Views: 684

Answers (2)

Mattias Ahnberg
Mattias Ahnberg

Reputation: 2870

The answer to the specific question has already been given; and I am not sure if you really wanted another solution or if you were just interested to analyze the behavior, but one way to list all non-directories in the current folder is to use find:

find . ! -type d -maxdepth 1

Upvotes: 0

ephemient
ephemient

Reputation: 204778

In Bash, !() (like *, ?, *(), and @()) only applies to one path component. Thus, !(anything containing a / slash) doesn't work.

If you switch to zsh, you can use *(^/) to match all non-directories, or *(.) to match all plain files.

Upvotes: 5

Related Questions