Two-dimensional open-cavity flows for free stream Mach number of 0.1 to 1.6 are investigated with direct numerical simulations for cavities of aspect ratio 2 and 6 to characterize the flow stability. We determine the neutral stability curve over a wide range of Mach numbers and Reynolds numbers. In particular, we examine the effects of compressibility on the flow in transonic regime at low Reynolds numbers. Rossiter modes are identified by spectral analysis. The corresponding spatial structures associated with the dominant and subdominant Rossiter modes are extracted with dynamic mode decomposition. The nature of these structures provides us insight to their contribution to instability of the flow. The interaction between the shock waves and the shear layer modes for the transonic flows are investigated.