Lazy Linux: 10 Essential tricks for admins

Lazy Linux: 10 Essential tricks for admins is an awesome list of cool things you can do with Linux. I learned about trick #3 (collaboration with screen) from an admin at the datacenter where one of my servers is hosted. I remember being thrilled sitting there watching him do stuff on my server while sharing the keyboard to type messages back and forth in vi (think Remote Desktop or VNC, but on the console). Trick #5 (SSH back door) is something I've been using for years at work for remote diagnostics. It is an invaluable trick for getting around firewalls. Very cool stuff!

Write a Comment

Comment

  1. Here’s one for you that’s directly related to email hosting:

    A lot of the time, a domain will have a default, “catch all” mailbox for unroutable mail, bounce backs, et cetera. Nobody ever checks the things, it would appear, so they tend to get huge … like several hundred thousand messages huge. On boxes that use maildir, that causes the directory descriptor for new/cur for that account to become huge in kind, which affects the standard filesystem utilities (any time a file is read, written, or stat’d, that descriptor must be read pretty much in full).

    Basically, it means that any operation on a file in those directories takes forever (a simple ls takes about 45 minutes before it even starts to output anything on my rather beefy workstation). Different file systems perform differently, but they all get bogged down by this kind of noise.

    So, yeah, here’s a much faster way to count the number of files in such a directory:

    (bash)
    cd /path/to/dir ; count=0 ; for i in * ; do ((count=count+1)) ; done ; echo $count

    Since rm does an lstat on any file that you try to remove, it’s going to suffer from the same problem. Here’s a quick way to remove files (and only files) in a directory like this:

    perl -e ‘unlink(glob(“*”))’

    Granted, it’s still slow, but it’s typically an order of magnitude quicker than rm -rf. Also, it’s not really favorable if you want to cherry-pick files, but with the amount of time that ls takes to begin displaying results, it’s a pretty safe bet that you’re never going to want to see any of the messages in a mailbox that’s affected by this problem.

    After the unlink runs, you’ll want to nuke and re-create the directory that you’ve cleaned out so that an update to the directory descriptor is forced.

  2. I always wondered in what situation using unlink() would make more sense than using rm. Also, the man page for lstat gives me some cool stuff to explore while I continue learning C. (I’m fairly certain I’ll be using lstat in the follow-up Systems Programming class.)

    Very cool! Thanks! 🙂