2

I'm both a regex & SE Unix and Linux newbie, so I hope I'm doing everything right.

I'm looking for files that have a name inside of the filename, and could have different extensions. I based my initial trials on this answer (the linux part) and my basic understanding of find and regexes, but with limited success.

I tried this regex: .*filename[^/].*\.(jpg|xcf|png|jpeg), which inside find looked like this find . -iregex '.*filename.*\.\(jpg\|gif\|png\|jpeg\)$'. The idea was to exclude the appearance of "filename" inside directory names, hence the "without forward slashes" [^/] part of the regex, but it excludes the most basic (and necessary) format, e.g. filename.jpg

I also tried find . -type f -iregex '.*filename.*\.\(jpg\|gif\|png\|jpeg\)$', but I have the feeling that using regexes might bypass the -type flag, because I still get all the directories in my results.

Could you share some light?

  • Bit confused here - you mention [^/] but then don't seem to use it; I suspect what you want is [^/]* (not [^/].*) – steeldriver Jun 18 '19 at 18:00
  • @steeldriver nice double find, thank you. Would you mind explaining the difference? I thought that the [^/] would prevent the regex to find patterns that had a slash in it, but then how does the simple quantifier alone allow for other characters to be there? Or the quantifier in this case means "find any char but a /" (which would then be equivalent to the ., but for the exception)? – Giampaolo Ferradini Jun 18 '19 at 19:59
  • [^/]* is a (possibly empty) sequence of characters none of which is / whereas [^/].* means a single character that is not / followed by a (possibly empty) sequence of any characters (which may include /) – steeldriver Jun 18 '19 at 22:02
  • Perfect explanation, thanks a lot. I think I was confused by the implicit assertion: I keep thinking in terms of "not /" (to which I was trying to add an "every character" notation, as I would in normal programming, say in python or JS), and forget that the [^/] actually means "every kind of character but /". – Giampaolo Ferradini Jun 19 '19 at 00:20
  • @steeldriver You should really have made it an answer, otherwise the question shows up as unanswerd, in fact i've started writing an answer, and then noticed that basically i was saying what you have said already. – v010dya Feb 03 '24 at 07:14

0 Answers0