*nix command line

 

Overview

need to do some useful work with linux/*nix? here are some 1-liners and sets of commands that do commonly-needed stuff for Administrating a computer, whether it's virus removal, etc.

get list of PIDs of process named sh

just so you know, the starting $ or # is the prompt. you don't type that in.

get list of PIDs of process named sh. ps puts a leading space on the numbers, so you have to trim it with cut.

$ ps -C sh -o pid=|cut -f 2 -d ' '
2057
2060
2201

comma-separated-list of PIDs as required by top -p

comma-separated-list of PIDs as required by top -p

$ ps -C sh -o pid=|cut -f 2 -d " "|paste -s -d ','
1731,2057,2060,2115,2175,2201,2547,2637

run top on a particular process name sh: -s means serial and -d is delimiter.

run top on a particular process name sh: -s means serial and -d is delimiter.

$ top -H -p`ps -C sh -o pid=|cut -f 2 -d " "|paste -s -d ','`
top - 15:01:36 up 33 min,  4 users,  load average: 0.05, 0.08, 0.13
Threads:   3 total,   0 running,   3 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.2 us,  0.2 sy,  0.0 ni, 99.4 id,  0.2 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:  12586952 total,  1302340 used, 11284612 free,    65896 buffers
KiB Swap: 12868604 total,        0 used, 12868604 free.   575888 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
 2057 jim       20   0    4444    644    548 S  0.0  0.0   0:00.00 sh
 2060 jim       20   0    4444    644    548 S  0.0  0.0   0:00.00 sh
 2201 jim       20   0    4444    636    536 S  0.0  0.0   0:00.00 sh

grep recursively from any dir

grep recursively from any dir, in this case ~/test-17799. grep -r uses the current dir. pushd pushes the current dir onto a built-in stack, and popd pops it off the stack and cd's into it.

$ pushd .;cd ~/test-17799;grep -r alloc *;popd
~ ~
a/s.h:alloc
a/s:alloc
a.h/s.h:alloc
a.h/s:alloc
s:alloc
s.h:alloc
~

find the string 'alloc' in all files and show the lines containing it. useful for source code

find the string 'alloc' in all files and show the lines containing it. useful for source code.
this is equivalent to below, you can tack on a -print afterwards or beforehand to put in the filepath.
the -s tells grep to not complain about directories. this is a misfeature/bug I wish they would fix, it's annoying and gets in the way of work, I don't think anybody wants the error messages about it finding a directory.

$ find -O3 ~/test-17799 -print -exec grep -s 'alloc' {} \;

tips

tip: never ever remove gcc from linux, it will hose your system.
apparently the ubuntu synaptic pakage manager removes dependent packages along with it, like DVD software, etc!
it also removes the synaptic package manager. so basically you are in for a reinstall of linux.