A few Linux / OS X tricks



Process administration



How to kill all you running instances of a named application
    kill $(lsof -t -c NameOfAppToKill -u $(whoami) -a)

Directory and file operations/info


List current directory contents displaying the full path

    ls | sed -e s*^*`pwd`/* -e s*//*/*

Compare the contents of two directories
OS X:
    rsync -rlpgoDnc --delete dir1/ dir2/
Linux:
    rsync -ancO --del dir1/ dir2/

Note: Don't worry "deleting" does not delete anyting because of the -n option

Change owner and group with the same command
    chown newowner:newgroup thefilename

Mass renaming of files in a directory (here *.htm to *.html):
    ls -d *.htm | sed 's/\(.*\).htm$/mv & \1.html/' | sh

Recursive find in files in the current directory
    find . | xargs grep 'The text you want to find'

Recursive find in files in the current directory showing line numbers
    find . | xargs grep -n 'The text you want to find'

Bash command line



Don't type a long filename again but reuse the previous command
Instead of typing:
    cat verylongfilename.txt
    od -x verylongfilename.txt
You can use:
    cat verylongfilename.txt
    od -x !!:*

Don't retype commands
    1) Use Ctrl-R to do an incremental search and reuse/edit your commands
    2) Use arrow-up a few times to reuse/edit one of the last commands used

Automated editing



Add a leading string to the beginning of every line in a file
    sed -i -e 's/^/The string you want to add/' yourfile.txt

Add a trailing string to the end of every line in a file
    sed -i -e 's/$/The string you want to add/' yourfile.txt

Remove all lines containing a certain string from a file
    sed -i -e '/Lines containing this string will be deleted/d' yourfile.txt

Remove all lines NOT containing a certain string from a file
    sed -i -e '/Lines containing this string will not be deleted/!d' yourfile.txt

Delete the first line of a file

    sed -i -e 1d yourfile.txt

Delete the last line of a file
    sed -i -e '$d' yourfile.txt

Mass conversion of text/source code files (here python files) from Windows/DOS to Linux/Unix format:
    ls -d *.py | sed 's*^*dos2unix *' | sh

Note: dos2unix is not available on OS X

Remote sessions



Tunneling a VNC session to a remote VNC server via ssh port forwarding
It is assumed that you have a remote VNC server running and you can connect to it via VNC on port 5901, and port 22 is not blocked in a firewall and is redirected (eg. via NAT) to the host at the remote site.
Type the following at your remote client, where port 6666 is a randomly chosen unused port:

    ssh -fNCTl yourloginname -L6666:localhost:5901 ipAddressOfRemoteHost

Start your favorite VNC viewer (eg. Chicken of the VNC) and connect to localhost:6666

Note: A remote Windows client can be connected to your VNC server with port forwarding via Putty and eg. RealVNC