There are a few tools for this, and I'm going to cover Shake and Defrag.
Shake is a defragmenter that runs in userspace, without the need of patching the kernel and while the system is used (for now, on GNU/Linux only).
It works by rewriting fragmented files. But it has some heuristics that could make it more efficient than other tools, including defrag and, maybe, xfs_fsr.
Defrag is a shell script. Download the latest version from HERE.
Using Defrag is very easy: copy the script in a folder you want to defragment. This can be just a normal folder or the top level directory of a partition.
http://launchpad.net/e2defrag.
Shake
There is a useful article but it's a few years old:
DEFRAGMENTING LINUX EXT3 FILESYSTEMS USING SHAKE AND DEFRAG