'RegEx search on linux filesystem based on filenames

I have tried following command find . | egrep -v '.*/[A-Z]{3}-[0-9]{8}-.' to recursively search for files (not folders) that are not in the pattern. This also displays folders! What am I missing?



Solution 1:[1]

You can use find directly with -not option:

find . -type f -regextype posix-egrep -not -regex '.*/[A-Z]{3}-[0-9]{8}-[^/]*$' -exec basename {} \;

With GNU find, you may use

find . -type f -regextype posix-egrep -not -regex '.*/[A-Z]{3}-[0-9]{8}-[^/]*$' -printf "%f\n"

Details:

  • -type f - return only file paths
  • -regextype posix-egrep sets the regex flavor to POSIX ERE
  • -not reverses the regex result
  • .*/[A-Z]{3}-[0-9]{8}-[^/]*$ - matches paths where file names start with three uppercase letters, -, eight digits, - and then can have any text other than / till the end of the string
  • -exec basename {} \; / -printf "%f\n" only prints the file names without folders (see Have Find print just the filenames, not full paths)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1