Ali Ashar
Ali Ashar

Reputation: 3

How do I use the find statement in bash shell with multiple conditions for the filename

I need to display all of the files in the "final" directory and sub directories that start with 2 underscore, followed by any number of any characters, ending in a number, with the “.c” extension.

Can I get some help here? I can't figure it out that how do I use multiple conditions that would fulfill the requirements.

#!/bin/bash
fine . -type f -iname "__*"
find . -type f -iname "*.c"

Upvotes: 1

Views: 307

Answers (2)

Gordon Davisson
Gordon Davisson

Reputation: 125708

If I'm understanding the desired filename pattern correctly, this should work:

find . -type f -iname "__*[0-9].c"

This is pretty much a literal translation of your description into glob pattern syntax:

"__" (start with 2 underscore)
"*" (any number of any characters)
"[0-9]" (a number)
".c" (with the “.c” extension)

The only thing that doesn't quite correspond to your description is that this only looks for a single digit before the ".c", rather than a potentially-multi-digit number. But if there's more than one digit, the "*" will match all but the last, so it's not a problem.

Upvotes: 1

nemequ
nemequ

Reputation: 17482

If either condition is true:

find . -type f -iname '__*' -o -iname '*.c'

If both have to be true, use -a instead of -o:

find . -type f -iname '__*' -a -iname '*.c'

Of course, in that case it would be much more simple to just do

find . -type f -iname '__*.c'

Upvotes: 0

Related Questions