How do i display headers of all commands?

I have done few google searches but none of them answered my question.

Do we have any universal way to display headers of all commands in Ubuntu with out installing additional packages? commands like ps -ef & ls, du.

Example

ps -ef | egrep "PID"
UID PID PPID C STIME TTY TIME CMD
root 18208 21629 0 02:49 pts/0 00:00:00 egrep --color=auto PID

in the above example, I have used egrep to get the header the above command prints no header when other grep words are used

Example below

ps -ef | egrep "nrpe"
nobody 8262 1 0 01:49 ? 00:00:00 /usr/bin/nrpe -c /etc/nrpe.cfg -d
root 18225 21629 0 02:49 pts/0 00:00:00 egrep --color=auto nrpe

P.S : I know functions of egrep but that doesn't work everywhere to get the headers.

TIA

2

1 Answer

Use a double pipe command, with head

ps -el | head -1 && ps -el | grep bash
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 1001 2680 2671 0 80 0 - 7532 wait pts/0 00:00:00 bash
0 S 1001 3673 2671 0 80 0 - 7499 wait pts/1 00:00:00 bash
4 S 1000 3694 3681 0 80 0 - 7469 - pts/1 00:00:00 bash
0 S 1001 6825 2671 0 80 0 - 7499 wait pts/2 00:00:00 bash

Explanation :

  • ps -el is the command that you would get result
  • head -1 will print the 1rst line displayed by your command (here, ps -el), so it would be the header line; the number of the option -1 just say how many lines must be displayed, so if your header is 2 lines, use -2 instead
  • there is the grep of your command
  • && help you to execute 2 commands in 1 line :
    1. it show the header line thanks to head -1
    2. it display the ps result filtered by the grep command

Also, there is a contracted form (which is equivalent) :

ps -el | ( head -1 && grep bash )

NB : if you also want to have the header line at the end (due to some result), just add another && statement, to do a 3 commands in 1 line, like that :

ps -el | head -1 && ps -el | grep bash && ps -el | head -1

Result :

F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 1001 2680 2671 0 80 0 - 7532 wait pts/0 00:00:00 bash
0 S 1001 3673 2671 0 80 0 - 7499 wait pts/1 00:00:00 bash
4 S 1000 3694 3681 0 80 0 - 7469 - pts/1 00:00:00 bash
0 S 1001 6825 2671 0 80 0 - 7499 wait pts/2 00:00:00 bash
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like