40GB is the thicker 2-platter hard drive, so it won't fit (unless you get the thick iPod back casing). 30GB can be either one-platter (like your 20GB hard drive) or two-platter. The older 30GB hard drive is the thick type. The newer 30GB hard drive is thin. So you need to fine out the specifics from the supplier. You can put any thin iPod hard drive of the same type (not the more recent type with "ZIF" connecter used in 5th gen iPod and later) in your 20GB 4th gen iPod - They are all the same physical size starting with the 5GB hard drive in the original iPod. The largest capacity (that is thin) is 30GB.
I have a 64GB compact flash card on an adapter in my 4th gen iPod (thin model with monochrome screen). It mostly works great, except that it occasionally starts skipping through songs continuously. Haven't figure that out yet. As a test, I let it play by itself all day on shuffle until the battery ran low, with no problems. I may be pushing the hardware; 32GB CF card would have been a more logical choice, except I wanted to fit my entire iTunes music library which is about 50GB currently. Or it may the particular brand or model if CF card that I chose. It is reliable enough for my use.

Another option is the smaller SD card, which is usually cheaper for a given GB size. But you need to put an adapter on an adapter, because there is no adapter that goes directly from SD card to iPod hard drive connector for the older "classic" iPods. And some of those SD to CF adapters are too thick for the thin iPod model, when you stick it on the CF to iPod adapter. Haven't tried it, but seems to work for some folks who write customer reviews.
What I just tried a few days ago works REALLY well on an iPod mini (not nano). iPod mini uses an IBM MicroDrive, which is a really tiny spinning hard drive. It is the same form-factor as the thicker compact flash card type. That adapter for SD to CF is exactly the same size, with the same connector. Along with a 64GB SD card, my 4GB iPod mini is now a 64GB iPod mini. Restored smoothly, and synced to my entire iTunes music library. I did not try it with my 64GB CF card, because that one's in my 4th gen iPod right now. Still testing it out, but this mod seems to be ultra-reliable.
If you want the more details about specific products I used, please post back.