Knocking on wood, or to touch wood, refers to the apotropaic tradition in western folklore of literally touching, tapping, or knocking on wood, or merely stating that you are doing or intend same, in order to avoid "tempting fate" after making a favourable observation, a boast, or declaration concerning one's own death.
In old English folklore, "knocking on wood" also referred to when people spoke of secrets – they went into the isolated woods to talk privately and "knocked" on the trees when they were talking to hide their communication from evil spirits who would be unable to hear when they knocked. Another version holds that the act of knocking was to perk up the spirits to make them work in the requester's favor. Yet another version holds that a sect of Monks who wore large wooden crosses around their necks would tap or "knock" on them to ward away evil.
In Romania, there is also a superstition that one can avoid bad things aforementioned by literally knocking on wood ("a bate în lemn"). Wood tables are exempted. One of the possible reasons could be that there is a monastery practice to call people to pray by playing / knocking the simantron.
In Bulgaria the superstition of "knock on wood" (чукам на дърво) is reserved for protection against the evil, and is not typically used for attracting good luck. Usually people engage in the practice in reaction to bad news, actual or merely imagined. In most cases the nearest wooden object is used (in some areas, however, tables are exempt); if there are no such objects within immediate reach, a common tongue-in-cheek practice is to knock on one's head. Knocking on wood is often followed by lightly pulling one's earlobe with the same hand. Common phrases to accompany the ritual are " God guard us" (Бог да ни пази) and "May the Devil not hear" (Да не чуе Дяволът).
In Serbia there is also the habit of knocking on wood when saying something positive or affirmative about someone or something and not wanting that to change. Frequently the movement of knocking on nearby wood is followed by да куцнем у дрво (I will knock on wood), or sometimes by да не урекнем (I don't want to jinx it). 
In Poland, as well as in Russia, there is a habit of knocking on (unpainted) wood (which may be preceded by saying odpukać w niemalowane drewno or simply odpukać, literally meaning to knock on unpainted wood) when saying something negative - to prevent it from happening - or, more rarely, something positive - in order not to "spoil it".
On an episode of the American TV show The Rifleman, first aired 11/05/1962, an Irish female character stated 'knocking on wood' was done to alert Leprechauns that a good deed was needed by the person doing the knocking.