Bash Find with ignore

I need to find files and ignore files like "^02" (it is regex). If "^02" is directory, then I need to ignore every files, which are inside directory. I don't know how to do it. I tried to use something like.

find . -type f -not -regex "^9" -o -prune

But it doesn't works.

Upvotes: 0

Views: 110

Answers (1)

choroba
choroba

Reputation: 241758

Note that the regex doesn't use ^ and $ as it always has to match the whole string. Moreover, the path starts with ./ if the first argument to find is ., so you need to include it, too.

find -type f -not -regex '\./02.*'

If you want to exclude even subdirectories, use .*/02.* for the regex.

If you want to only exclude the directories matching the pattern, but you want to keep the files, you need to use prune only for directories matching the regex, and -false to remove the directories from the list:

find . -type d -regex '\./02.*' -prune -false -or -type f

Also, you can use patterns instead of regexes for simple cases. That way, you can use -name to include subdirectories:

find . -name '02*' -prune -false -or -type f

Upvotes: 2

Related Questions