Friday, May 27, 2022

[SOLVED] RegEx search on linux filesystem based on filenames

Issue

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

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)


Answered By - Wiktor Stribiżew
Answer Checked By - Candace Johnson (WPSolving Volunteer)