I was trying to use negation to exclude directories from globbing, but directories still appear in pattern match:
bash-4.3$ ls
file_1.txt testdir
bash-4.3$ shopt extglob
extglob on
bash-4.3$ echo !(*/)
file_1.txt testdir
bash-4.3$
What exactly am I doing wrong ?
Note:I know I can use for
loop with [
or find
command, but I'm trying to figure out extglob
specifically.
You can't have a /
in the @(...)
, !(...)
, *(...)
...
The /
can only appear between globs, even a[x/y]b
is treated as @(a\[x)/@(y\]b)
. globs are first split on /
and each part matched against the content of a directory. When there are x(...)
ksh glob extensions, however, there's no splitting on the /
that are inside the (...)
, but each glob part is still matched against file names. In !(*/*)
, */*
is matched against each file name in the current directory. Obviously, no file name may ever contain a /
, so it matches nothing, so !(*/*)
matches every file.
Here, you'd want to use zsh
and its glob qualifiers:
echo *(^/)
For the files of any type except directory. Or to be the opposite of bash
's */
(which is any file of type directory after symlink resolution):
echo *(-^/)
(files that are neither directories nor symlinks to directories).
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加