To display the top 15 processes sorted by memory use in descending order, do:
# top -b -o +%MEM | head -n 22
As opposed to the previous tip, here you have to use +%MEM (note the plus sign) to sort the output in descending order.
From the command above, the option:
- -b : runs top in batch mode
- -o : used to specify fields for sorting processes
- head utility displays the first few lines of a file and
- the -n option is used to specify the number of lines to be displayed.
Redirect or Save ‘top’ Output to File in Linux
Additionally, using top in batch mode allows you to redirect the output to a file for later inspection:
# top -b -o +%MEM | head -n 22 > topreport.txt