The pattern rules for glob are not regular expressions. Instead, they follow standard Unix path expansion rules. There are only a few special characters: two different wild-cards, and character ranges are supported [from pymotw: glob – Filename pattern matching].
So you can exclude some files with patterns.
For example to exclude manifests files (files starting with _) with glob, you can use:
files = glob.glob('files_path/[!_]*')
Answer from Kenly on Stack OverflowThe pattern rules for glob are not regular expressions. Instead, they follow standard Unix path expansion rules. There are only a few special characters: two different wild-cards, and character ranges are supported [from pymotw: glob – Filename pattern matching].
So you can exclude some files with patterns.
For example to exclude manifests files (files starting with _) with glob, you can use:
files = glob.glob('files_path/[!_]*')
You can deduct sets and cast it back as a list:
list(set(glob("*")) - set(glob("eph*")))
How to exclude folders when include follows a glob pattern?
New in v2.1: exclude files with globs
[SOLVED] How to exclude using glob?
node.js - Glob matching, exclude all JS files - Stack Overflow
shopt -s extglob
echo rm foo.!(org)
This matches foo. followed by anything NOT org
To restore normal glob behavior afterwards, use:
shopt -u extglob
Reference: The GNU Bash reference manual
In bash, you can also use GLOBIGNORE="*.org"; rm -i foo*.
And unset GLOBIGNORE when done.
It's not really better than shopt -s extglob, but I find it easier to remember.
I think the ** means "include everything". Below I was hoping excluding __tests__ would take precedence over this broad match in include but it did not. How would you modify tsconfig to include everything in src except for folders and files inside __tests__ ?
I have a directory with backups. Each file increments by day of month from 01 to 31. I want to delete all of the files except the 01 file. Of course I can do this with multiple commands, but it seems like there should be a one line solution. I'm looking for the equivalent of:
rm 0[2-9]; rm [1,2,3]?
edit: I'm interested in the globbing or file expansion solution.
if you are using bash
#!/bin/bash
shopt -s extglob
ls !(fubar).log
or without extglob
shopt -u extglob
for file in !(fubar).log
do
echo "$file"
done
or
for file in *log
do
case "$file" in
fubar* ) continue;;
* ) echo "do your stuff with $file";;
esac
done
Why don't you use grep? For example:
ls |grep -v fubar|while read line; do echo "reading $line"; done;
And here is the output:
reading barbaz.log reading fubaz.log reading text.txt