This is an old revision of the document!


Three lines tip

Do you have a tip/hack that's max. three lines long??

How do I capture the output of “top” to a file?

The command top is a very useful tool to capture information about process running on Linux. Many times this information may need to be captured to a file. This can be done with the following command:

  top -b -n1 > /tmp/top.txt

This will run top once write the output to a file and exit.

The command top can also be run so that it will give multiple reports. To run top 5 times and wait 5 seconds between each output the command would be:

 top -b -n5 -d5 > /tmp/top.txt

Removing whitespaces from filename

 # i in *.[jJ][pP][gG]; do mv "$i" $(echo "$i" | sed "s/ /_/g"); done

ali

 # rename 's/ /_/' *.[jJ][pP][gG]

:)

How to get Webserver Version

 # printf "HEAD / HTTP/1.0nn" | nc $server 80 | grep Server

Spliting .tgz file on the fly

 tar -czf /dev/stdout $(DIRECTORY_OR_FILE_TO_COMPRESS) | split -d -b $(CHUNK_SIZE_IN_BYTES) - $(FILE_NAME_PREFIX)

putting them together agian

 cat $(FILE_NAME_PREFIX)* >> /dev/stdout | tar -xzf /dev/stdin

Compile on SMP machines

export MAKEFLAGS=“-j3”

Patch & Diff

 $ diff -c prog.c.old prog.c > prog.patch
 $ patch < prog.patch

Printing network traffic

 % sudo tcpdump -Atq -s 0 -i en1

-i en1 will display traffic on your airport card. Use en0 (or nothing, for most systems) for built-in ethernettage. You can add a host line to limit output to a particular host

 % sudo tcpdump -Atq -s 0 -i en1 host foobar.com

Couting openfiles per user

 # lsof | grep ' root ' | awk '{print $NF}' | sort | wc -l

Of course, if you want to drop the count and show the actual processes, you can run:

 # lsof | grep ' root '

How to check is service is tcpwrapper enable

 # ldd `which sshd` | grep -i libwrap

or

 # strings `which sshd` | grep -i libwrap

Comparing files & taking some action based on outcome

Method 1

cmp -s file1 file2 || {
  # do something
}

Method 2

cmp -s file1 file2
if [ $? = 1 ]
then
  # do something
fi

finding a string in a "live" stream

tail -f filename |grep –line-buffered STRING

tips/threelinestip.1227686690.txt.gz · Last modified: 2009/05/25 00:34 (external edit)
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0 ipv6 ready