moominboy
moominboy

Reputation: 23

How to find files whose name starts with "a"?

I know I'm missing something obvious and I'm certain it's to do with my -name part.

And it is homework, but I think I'm pretty close to the correct answer. Better ways of writing are always appreciated too!

find /home/caine/thecopy -user caine -size -10240c -name ^a.* | wc 

This gives a wc of 0 0 0, removing the name expression gives oodles of counts.

I've tried ^a , '^a' , '^a.*' and all come up with 0 results. TIA folks. :)

Upvotes: 2

Views: 514

Answers (2)

fedorqui
fedorqui

Reputation: 290025

You need to wrap the file name:

find /home/caine/thecopy -user caine -size -10240c -name "a.*" | wc
#                                                        ^   ^

Upvotes: 4

Keith Thompson
Keith Thompson

Reputation: 263497

The argument to -name is a shell pattern, not a regular expression. The ^ and . characters will match literal ^ and . characters, not the beginning of the name and any character, respectively, as they would if it were a regular expression.

If you want to match file names starting with 'a', this should work:

find /home/caine/thecopy -user caine -size -10240c -name 'a*' | wc 

The quotation marks around a* (either single or double) are important; without them, the shell will expand the pattern before find sees it.

If you really want to match a regular expression, you can replace -name with -regex; that's a GNU-specific extension. But in this case, a shell pattern is probably good enough.

Note: I'm assuming that you want to match files whose names start with a; you didn't actually say so.

Upvotes: 2

Related Questions