Home > Net >  RegEx search on linux filesystem based on filenames
RegEx search on linux filesystem based on filenames

Time:03-19

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?

CodePudding user response:

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)
  • Related