grep "^>" input.fa | sed "-es/^>//" > labels.txt
With FASTQ format, there is no simple way to do this using Linux commands because the Q score sequence can start with the same character(s) as a read label.
Example
usearch -fastx_getlabels reads.fq -output labels.txt