Question: How can I make `find` apply my shell's defined functions and aliases inside its *exec* parameter?

For example I have defined a function analogous to **bzip2** but using **7z**:

> 
> function 7zip() { for f in $@; do ls -alF "$f"; 7za a -t7z -m0=lzma
>  -mx=9 -mfb=64 -md=64m -ms=on "$f.7z" "$f" && touch -r "$f" "$f.7z" && rm -fv "$f" && ls -alF "$f.7z"; done; }
> 
> 
> 

When I find files older than 7 days to compress:

```
find . -mtime +7  -name "G*.html"   -execdir  7zip {}  + 

```

Rather than expanding **7zip** it errors Command Not Found.

This is all inside a shell script.

Answer: 
All four of these command works just fine with the function call. Adjust your find specs as need be.. They all cater for spaces in file names. Personally, I can't see the point of shelling out to another bash instance, but I've included two versions which call bash. 

```
IFS=$'\n'; f=($(find /tmp -maxdepth 1 -name "$USER.*")); f7zipi "${f[@]}"

IFS=; find /tmp -maxdepth 1 -name "$USER.*" | while read -r f ;do f7zipi "$f"; done 

IFS=$'\n'; bash -c 'IFS=; f7zipi "$@"' 0 $(find /tmp -maxdepth 1 -name "$USER.*")  

find /tmp -maxdepth 1 -name "$USER.*" -exec bash -c 'IFS=; f7zipi "$@"' 0 {} +;   

```

---

What follows is how I've set up the function, using GNU bash 4.1.5 in Ubuntu 10.04 

BTW. You should use `local f` in your function, so that it **does not clash** with the calling script's variable of the same name. 

This is exactly what I added to my ~/.bashrc 

```
function f7zipi() { 
    local f
    for f in $@; do 
        ls -alF "$f"
        7za a -si -t7z -m0=lzma -mx=9 -mfb=64 \
        -md=64m -ms=on "$f.7z" < "$f" && 
            touch -r "$f" "$f.7z" && 
            rm -fv "$f" && 
            ls -alF "$f.7z"
    done
}
export -f f7zipi

```

When I only assign the above function into a terminal's bash command line, a script running from that command line fails when it calls the function... If I further apply `export -f f7zipi` to that same command line.. then the script succeeds... However the scipt only works for that particular commandline session. 

When the function and export are included into `~/bashrc`, the script works every time, in any bash session.. 

This is the test script 

```
#!/bin/bash
f=/tmp/$USER.abc
g=/tmp/$USER.lmn
rm -fv "$f" "$f".7z
rm -fv "$g" "$g".7z
printf 'abcdefg'>"$f"
printf 'lmnopqr'>"$g"
IFS=$'\n'; f=($(find /tmp -maxdepth 1 -name "$USER.*")); f7zipi "${f[@]}"
exit

```
Seems that not every `find` will accept a function as an argument for `--execdir`. It did not work for me either in the original form or using `export -f`.

However, if your make a script out of your function, it will work

```
find . -mtime +7 -name "G*.html" -execdir  /path/to/script_7zipi {} +

```