### What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

### Check These Out

Which processes are listening on a specific port (e.g. port 80)
swap out "80" for your port of interest. Can use port number or named ports e.g. "http"

Display the number of connections to a MySQL Database
Count the number of active connections to a MySQL database. The MySQL command "show processlist" gives a list of all the active clients. However, by using the processlist table, in the information_schema database, we can sort and count the results within MySQL.

Delete all non-printing characters from a file
tr has some predefined sets of characters that are more convenient to use than characters codes

share internet connection with only one network interface
the below command create a alias for share your internet connection with another. $ifconfig eth0:1 192.168.0.1/24 Its obviously necessary too activate the iptables post-routing and ip forwarding, as root:$modprobe iptable_nat $iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE$echo 1 > /proc/sys/net/ipv4/ip_forward Be sure that the alias 192.168.0.0/24 is not your active real ip range

Recursively grep for string and format output for vi(m)
This is a big time saver for me. I often grep source code and need to edit the findings. A single highlight of the mouse and middle mouse click (in gnome terminal) and I'm editing the exact line I just found. The color highlighting helps interpret the data.

Find the package that installed a command

Search recursively to find a word or phrase in certain file types, such as C code
I have a bash alias for this command line and find it useful for searching C code for error messages. The -H tells grep to print the filename. you can omit the -i to match the case exactly or keep the -i for case-insensitive matching. This find command find all .c and .h files

add the result of a command into vi
':r!ls -l' results in listing the files in the current directory and paste it into vi

escape any command aliases
e.g. if rm is aliased for 'rm -i', you can escape the alias by prepending a backslash: rm [file] # WILL prompt for confirmation per the alias \rm [file] # will NOT prompt for confirmation per the default behavior of the command

Display or use a random file from current directory via a small bash one-liner
An other way to run it ( playing a random file ending with avi, flv or mpeg ) from a specified dir and a specified type of extension : making MOVIE array with a glob : $MOVIE=( /PATH/TO/MY/FAVORITE/MOVIES/*.{avi,flv,mpeg} ) playing the random file from a random key from the array$ mplayer ${MOVIE[ RANDOM % (${#i[@]} + 1 ) ]]} I use only globs and a bash array. I use GNU bash, version 3.2.48