Home > Admin stuff > Shell perls

Shell perls

January 29th, 2012

As I always forget them, I just put them down right now right here (they were sitting for too long in my Drafts folder). And I might update them as I go.

Replace multiple chars

sed "s/=ABC=//g"

Grep for multiple words

I know, I know. This shouldn’t be here, but I kept on forgetting it…

egrep "FULL|bytes|speedup" snapback.log

Calculate size of directories

du --max-depth=1 -h .

Not sure what I did with this, but it seems pretty important…

NUMBER=$[ ( $RANDOM % 100 )  + 1 ]
cat servers | while read line; do ./collect_authorized_keys.sh $line done; done
awk -F":" '{ print $1 }' /etc/passwd
cat tt | while read line; do export NUMBER=$[$NUMBER + $line]; echo $NUMBER total size > s1; done;

Replacing newlines

My first thought about replacing newlines in files would have been sed, but it is not. tr is the way to go:

cat file | tr 'n' ','

To not only replace line endings, but also text before or after them, give this a try:

sed -n '1h;2,$H;${g;s/n/,/g;p}' <file>

Simulate system load

dd if=/dev/zero bs=100M | gzip | gzip -d | gzip | gzip -d | gzip | gzip -d > /dev/null &amp;

Howto kill all child processes of a (bash) process

Killing a single process is easy; killing multiple processes also. But only if you know all of the PIDs. If you need to kill a process including all children (sub-processes), try this one:

ps -o pid= --ppid $PID_TO_KILL | xargs kill

(Note that there are a gazillion solutions out there, but they all seem to be a bit over-complicated. Or I’m missing something..)

Check if a process is running

kill -0 pid 2>/dev/null
echo $?
Categories: Admin stuff
Comments are closed.