How to kill all processes apart from specific one

Give a try to this command:

ps ax | grep "java" | egrep -v "eclipse" | cut -b1-06 | xargs -t kill

this will search for all processes containing java and execluding eclipse then kill them


Using awk

ps ax | awk '/java/ && !/eclipse/ {system("kill "$1)}'

The command kills all java processes, but not the process eclipse.


How about :

pgrep -af 'java' | grep -Po '^[^ ]+(?!.*eclipse)' | xargs kill
  • pgrep -af 'java' searches for java in the full command line of the processes and list them

  • grep -Po '^[^ ]+(?!.*eclipse)' looks among the searched processes and get the process IDs of the processes that do not have eclipse in their full command lines

  • xargs kill will kill the processes.