Linux or unix find: shortcut for matching partial name

567 views Asked by At

What I usually type:

find . -name "*pattern*"

what I want to type (for example. mnemonic = "all"):

afind . -name "pattern"

or

find . -aname "pattern"

I was just thinking there could be a shortcut like in the style of find . -iname "blah" or rgrep instead of grep -r

Even though, like the grep example, it only saves a couple of characters, they are shifted characters not as easy to type, and it is a commonly used search, probably.

3

There are 3 answers

0
L. Scott Johnson On BEST ANSWER

You'd have to roll your own. e.g.:

$ afind () { find "$1" -name "*$2*"; }

$ afind . partial  
./dir/apartialmatch

You can add the definition to your .profile or whatever so that it's always available.

1
Camden Cheek On

What you're looking for is a sort of wrapper function for find.

This script should do what you're looking for, and maintain all the other functionality of find.

declare -A params
wild_next=false
for i in $(seq 1 $#); do
    if [[ ${!i} == "-aname" ]]; then
        params[$i]='-name'
        wild_next=true
    else
        if $wild_next; then
            params[$i]="'*${!i}*'"
        else
            params[$i]=${!i}
        fi
        wild_next=false
    fi
done

eval "find ${params[@]}"

It just replaces any instance of the -aname option with -name and puts asterisks around its argument.

0
Erik Bennett On

If this is something that you do a lot of, locate would be faster. It's based on the Berkeley Fast Find and is included in the GNU "findutils" package. Note that it gets its data from a database, so its only as current as the last run of updatedb, which is also in the "findutils" package.