UNIX command line

Continuing my series of things I take for granted, over the course of my life I have used UNIX-derived operating systems: AIX, SunOS, Solaris, OpenBSD, FreeBSD, Linux, and now OS X (which is based on FreeBSD).  I first learned how to use UNIX via the command line, dialing in to systems and telneting to others.  (As an aside: at the same time, I also got to learn VAX a little.  Yay.)  Because I first used UNIX-like systems via the command line, the command line is very comfortable to me.  I know it isn’t comfortable to most people, and there are in fact very good GUI (graphical user interface) equivalents to most of what you can do via the command line.  The nice thing about the command line is that you can do some very powerful stuff quickly, without the mouse.  Here are a couple of things I take for granted on the command line.  This is probably most relevant for people using OS X, or people with cygwin installed on Windows.

  1. du -cks * | sort -n
    

    This one I use so much I create an alias for it.  ‘du’ looks through every entry in the current directory that ‘*’ matches, and prints out how much space in kilobytes that entry takes.  ’sort’ then sorts the entries numerically.  The output is a sorted list of stuff in the directory where the biggest stuff is at the bottom.  This command is useful for figuring out where all your hard drive space is going (in my case, my Aperture library).

  2. vi $!
    

    This one is particularly geeky and really only useful for people who already know vi (a text editor that you hit ‘ZZ’ to quit out of).  In bash, ‘$!’ means get the last argument from the previous line.  If you do ‘ls x’ and then ‘vi $!’, bash converts this to ‘vi x’.

  3. history

    Shows the commands you have typed previously.

  4. man <<command>>

    Goes to the manual entry for that command.  For example, ‘man cat’ tells you how the cat command works.

  5. pydoc -p 8080

    Runs a local web server at port 8080 (that you can access via http://localhost:8080) with python documentation for your installed modules.

  6. cp file{,-20100131}

    Another bash trick; bash will expand whatever’s in the {}s into a list.  In this example, bash converts this into “cp file file-20100131″.  I use this to take simple backups of files before I destroy them accidentally.

  7. <<command>> | sort | uniq -c | sort -n
    

    Take the outputs of command and send it to the sort command.  Unique removes duplicates from output and prints how many rows there were.  Effectively this prints out unique lines, sorted by how many times they occurred.

  8. command | cut -d' ' -f 2

    ‘cut’ will use the specified delimiter (-d) and print out the specified field (in this case, the second field).  For example, if the input were “Hello there fred”, this particular command would print out “there”.

  9. svn <<commands>>

    I should probably learn how to use a newer version control tool.  Subversion (svn) is a tool for version control; it lets you check out files, edit them, and check them in.  For someone like me who makes lots of mistakes, subversion and its ilk are a lifesaver because it helps me find out where things started going wrong.

  10. ifconfig -a

    Shows information about network interfaces.  Helpful for finding out your IP address(es).

  11. grep -rli 'something' .

    Very very helpful file-searching tool.  grep -rli searches all subdirectories for the string specified.  It is slow.

  12. find . -mtime -3 -ls

    Extremely helpful file-level search tool, in this example to find all files within the directory tree modified in the last three days.

  13. locate x

    Search the locate database of all files on the system for files that have a given string in them, in this case ‘x’.  Requires that you’re running a job to update the locate database from time to time.

Well, those are just a couple that come to mind.  The command line can be really helpful for working with your filesystem and for finding stuff.  Fortunately, there are now also tools such as Quicksilver and the OS built-in search tools that can do a lot of this same stuff, but if you do need to do a lot of messing around with files (especially groups of files), learning the command line can really pay off.

Leave a Reply