Is it necessary to defrag drives in Ubuntu? If so, how do I do it and how often should it be done?
Defragment is (or was) recommended under Windows because it had a poor filesystem implementation. Simple techniques such as allocating blocks for files in groups rather than one by one keep fragmentation down under Linux. Typical Linux filesystems only gain significantly from defragmentation on a nearly-full filesystem or with unusual write patterns. Most users don't need it, though heavy file sharers could benefit from it (filling a file in little bits in the middle is not the case ext3 was optimized for; if you're concerned about fragmentation and your bittorrent or other file sharing client offers that option, tell it to preallocate all files before starting to download).
At the moment, there is no production-ready defragmentation tool for the common filesystems on Linux (ext3 and ext4). If you installed Ubuntu 9.10 or newer, or converted an existing installation, you have an ext4 filesystem, which supports extents, further reducing fragmentation.
For those cases where fragmentation does arise, an ext4 defragmentation tool is in the works, but it's not ready yet.
Note that in general, the Linux philosophy and especially the Ubuntu philosophy is that common maintenance tasks should happen automatically without your needing to intervene.
This is the best explanation (not only of file fragmentation but of how EXT2/3/4 is more resistant to it than FAT or NTFS) I have found and I still refer people to it: http://geekblog.oneandoneis2.org/index.php/2006/08/17/why_doesn_t_linux_need_defragmenting
If you're using ext, here is a good quote from Theodore Ts'o (he was heavily involved in the development of the file system):
If you set the reserved block count to zero, it won't affect performance much except if you run for long periods of time (with lots of file creates and deletes) while the filesystem is almost full (i.e., say above 95%), at which point you'll be subject to fragmentation problems. Ext4's multi-block allocator is much more fragmentation resistant, because it tries much harder to find contiguous blocks, so even if you don't enable the other ext4 features, you'll see better results simply mounting an ext3 filesystem using ext4 before the filesystem gets completely full.
So really, the answer is 'no'. Only in extreme cases will you start to see adverse fragmentation (with ext).