0

part 1

ps -e | sort -k 1 -nr 

What is the interpretation of the above command? I want to understand the role of -nr. It is not clearly given the man pages of sort.

part 2

When you open the man page of head, one of the options states:

-c, --bytes=[-]K

print the first K bytes of each file; with the leading `-', print all but the last K bytes of each file

How to interpret the -c, --bytes=[-]K?

  • 2
    When you have two single letter options (-n and -r) that do not take arguments, usually you can squish them together like -nr. Check the man page for the individual options. An example: ls -ltr or ls -l -t -r – glenn jackman Sep 24 '15 at 13:46
  • related, but not dupplicate : http://unix.stackexchange.com/questions/193815/how-do-i-use-man-pages-to-learn-how-to-use-commands/193816#193816 – Archemar Sep 24 '15 at 14:44
  • The K in the manpage is schematic for a number. You can have -c10 or --bytes=10 to print the first 10 bytes of the file. Or -c-10/--bytes=-10 to print all but the last 10 bytes of the file. – dubiousjim Sep 25 '15 at 06:31

3 Answers3

2

Part 1

  1. ps is a command that reports a snapshot of the current processes, and the -e argument specifies ps to report all processes.
  2. | is a pipe. This allows you to redirect the output of the previous command into another command. In this example, you are redirecting the output of ps -e to sort.
  3. sort is a command that allows you to sort the lines from stdin. The -k 1 argument sorts via a key, in this case 1. This means that you are going to sort with respect to the first column of ps -e, the PID numbers. The -nr argument sorts the input numerically in reverse (descending) order.
  4. NOTE: The -n argument is actually redundant in this implementation. You do not need it because you are already specifying sort to sort by the first column. ps -e | grep -rk 1 will work just the same.

Part 2

  1. The -c or --bytes=[-]K argument allows you to print out the first or all but the last X kilobytes of a file, where X is how kilobytes you want to print. To print the first X kilobytes, just put a number after -c. To print all but the last kilobytes, add a - in front of the X kilobytes.

  2. Also note that, with -c, you do not need to specify a number followed by K. This syntax is only needed in the --bytes= argument.

Peschke
  • 4,148
  • The --bytes=... option also only takes a number argument, not an argument followed by a literal K. The K in the manpage is meant to stand in for a number, schematically. – dubiousjim Sep 25 '15 at 06:32
0

Part 1: -n is for numerical sorting, -r is for reverse sorting, so -nr combined sorts in descending order numerically.

Part 2: you can use either -c (short option) or --bytes= (long option, more typing but no difference). So say --bytes=-1024 for the last 1024 bytes for instance.

proycon
  • 314
  • 1
  • 6
0

Additionally, the items between the brackets [] can be interpreted as 'optional'. So --bytes=[-]K means you can use a 'dash' for 'the last of...' and without it to mean 'the beginning of ...'. Since the 'K' is not within the brackets, it means that it is required for the option --bytes