Do the upper parts in all cadences move in contrary motion to the bass.if I write it like that, am I wrong? Or is it only for adjacent chords?
Making the upper parts move in contrary motion to the bass is a good "rule of thumb" to avoid writing consecutive 5ths and octaves, but it is not necessary in a cadence, or anywhere else.
In 'harmony exercise' land, contrary motion is good and can help in avoiding parallels. But other approaches can be more exciting!
Once you get beyond imitating hymn tunes, you'll find parallel motion, unisons etc. used a great deal. Sometimes music demands smooth, full harmony. Sometimes a concerted, exultant leap upwards is more appropriate.
(But in harmony exercises, obey the rules.)
You can have parallel movements, it is just more likely for consecutive octaves/fifths when you do it. So for newbies it is just easier to teach it like this.
You can, for instance, have your cadential 6/4 chord progressions where two voices stay on the same pitch and the other two move a step down in parallel.
Good voice leading is the main issue in these harmony questions, the form of your outer voices, while not unimportant cannot be good at the voice leading's expense.
In old counterpoint style, exemplified by Fux's species counterpoint, the cadence called a clausula vera did require contrary motion.
But the typical classical cadences do not have that requirement.
I think the simplest example that illustrates the point is the perfect authentic cadence using only treble and bass parts to show the essential voice leading:
...those two cadences can be identified with the treble parts using scale degrees:
^3 ^2 ^1 and
^1 ^7 ^1.
So the final tonic
G can be approached by either a step above or a step below in the treble.
The bass part moves from the dominant to the tonic.
I intentionally wrote the bass so that it moves in similar motion to the treble over the barline. When two parts move in similar motion to a perfect fifth or octave it is called direct (or hidden) fifths/octaves. Direct motion is avoided to varying degrees, but at cadences it is acceptable.