I am running an SSH server and have this user that I want to delete. I cannot delete this user because he is currently running a few processes that I need to kill first.

This is the pipeline I am using currently using to find out all the process ids of the user I am currently using:

ps -u user | awk '{print $1;}'

The output looks like this:


I want to pipe this to kill -9 to kill all processes so I can delete this stupid user like this:

ps -u user | awk '{print $1;}' | sudo xargs kill -9

But this does not work because of the PID header:

kill: failed to parse argument: 'PID'

The question

I am thinking that there has to be a simple Unix command to remove the first line of input.

I am aware that I can use tail for this but I don't want to count how many lines the input contains to figure out exactly how many I want to display.

I am looking for something like head or tail but inverted (instead of displaying only the first/last part of the stream it displays everything but the start/end of stream).


I managed to solve this issue I had by simply adding | grep [[:digit:]] after my awk command but I am still looking for a way to delete first line of a file as I think would be quite useful in other scenarios.

  • Since you're already using awk, you could have excluded the header record (line) using NR e.g. awk 'NR>1 {print $1;}'. Better yet - use pgrep -u or pkill -u – steeldriver Dec 31 '15 at 14:30
  • @steeldriver Interesting! Make an answer and explain how it (the awk command) works. – wefwefa3 Dec 31 '15 at 14:33

NOTE: if your system already has pgrep/pkill then you are re-inventing the wheel here. If your system doesn't have these utilities, then you should be able to format the output of ps to get the unencumbered PID list directly e.g. ps -u user -opid=

If you are already using awk, there is no need to pipe through an additional process in order to remove the first line (record): simply add a condition on the record number NR

ps -u user | awk 'NR>1{print $1;}'

Since you mention head and tail, the formula you probably want in this case is tail -n +2

  • 4
    tail -n +2 was exactly what I was looking for! – wefwefa3 Dec 31 '15 at 15:33
  • +1 for telling ps not to print a header with -opid=, as that prevents the output from ever showing up. If a future user ever decides to sort the ps output in the middle of the pipeline, the awk/sed/tail assumption may break. – Jeff Schaller Dec 31 '15 at 16:05

As I was writing this question I figured out a solution using sed.

Delete one line at beginning of input

To delete one line use sed 1d.

Delete multiple lines at beginning of input

To delete N number of lines use sed 1,Nd

  • To delete the first 5 lines use sed 1,5d

  • To delete the first 10 lines use sed 1,10d

  • To delete the first 25 lines use sed 1,25d

The pipeline

So the pipeline becomes in the scenario becomes:

ps -u user | awk '{print $1;}' | sed 1d | sudo xargs kill -9
                                     ^ removes the PID header

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.