0

I want to run top so that it opens as if you hit the m key, what I really want is this:

How to run ''top'' command 1 time and exit?

But I want to

a) have the 'm' key hit b) sort by memory (shift-m)

Can this be done from the command line?

Thanks, Kevin

update - showing what i am seeking from command line without having to hit any key:

enter image description here

Just an update to clarify i need BOTH the sort by AND the memory showing, in a single command line, the given solution only solves the sort request. Thanks for any help.

user1130176
  • 203
  • 3
  • 12
  • If you want those as the *defaults*, consider using a .toprc file - see for example [How to set memory usage sort in top command as default?](https://askubuntu.com/questions/1268933/how-to-set-memory-usage-sort-in-top-command-as-default) – steeldriver Nov 23 '22 at 14:37
  • 'man top' may contain the answer. Could be the -o option. – vanadium Nov 23 '22 at 14:39
  • @vanadium it is ;-) – Rinzwind Nov 23 '22 at 14:40

1 Answers1

1

from command line you can sort with -o. On MEM this would be

top -o +%MEM

if from high to low.

See man top:

-o :Override-sort-field as: -o fieldname Specifies the name of the field on which tasks will be sorted, independent of what is reflected in the configuration file. You can prepend a + or - to the field name to also override the sort direction. A leading + will force sorting high to low, whereas a - will ensure a low to high ordering.

This option exists primarily to support automated/scripted batch mode operation.

Rinzwind
  • 293,910
  • 41
  • 570
  • 710