Linear codes have been an interesting topic in both theory and practice for many years. In this paper, a class of $q$-ary linear codes with few weights are presented and their weight distributions are determined using Gauss periods. Some of the linear codes obtained are optimal or almost optimal with respect to the Griesmer bound. As s applications, these linear codes can be used to construct secret sharing schemes with nice access structures.