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.
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.
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 fin 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
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 f7zipito 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