简体   繁体   中英

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"

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.

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM