Yevgeny Simkin
Yevgeny Simkin

Reputation: 28349

What's the best way to use regex in searching for files named via some pattern in linux?

I have file names that are named thusly...

[phone_number]_[email]_[milliseconds].mp4

so:

[email protected]_1378447385902.mp4

find takes a regex pattern (supposedly) to look for files that start with 10 digits and end with mp4like this:

find ../media_pool -regex '^\d{10}.*mp4$'

However that returns nothing at all.

When I try it thusly: find ../media_pool -regex 'mp4$' it returns all the files that end with that extension... so, it *looks like it works with some subset of regex but not all.

Can someone point me to what the right way to get what I need would be? I'm happy not to use find if something else does the job better.

Upvotes: 1

Views: 233

Answers (3)

Ibrahim Najjar
Ibrahim Najjar

Reputation: 19423

I am not an expert in Linux utilities but it seems you can specify the type of the regex used to match the pattern, anyway it seems that \d is not supported, try the following:

find ../media_pool -regextype posix-extended -regex '^[0-9]{10}.*mp4$'

I don't know if you need to quote posix-extended, that's for you to figure out.

Edit: Sorry for that, there was another problem. You don't need to change the engine type, by default find uses Emacs engine and I was able to look at the syntax supported.

find ../media_pool -regex '.*/[0-9]\{10\}.*mp4$'

The key is escaping the { and } eg. \{10\} and adding a .*/ to the start to match on the full path which find returns.

Upvotes: 1

Hunter McMillen
Hunter McMillen

Reputation: 61512

The default regular expression engine for find is Emacs, you can change it to something else by using the -regextype option

Here is an example using sed:

find . -regextype sed -regex ".*/[0-9]\{10\}.*mp4$"

there are most likely other solutions since several engines are supported. Another important thing to note is the .*/ at the beginning of the regular expression, find matches the entire path of a file so this will catch that.

Upvotes: 0

PherricOxide
PherricOxide

Reputation: 15909

Took me a while to figure out that find matches the entire path, so you need the ".*/" at the beginning. The following is tested and works.

find . -regextype posix-extended -regex '.*/[0-9]{10}.*mp4$'

Upvotes: 1

Related Questions