The Beginner’s Guide to Command Line Magic

by Devanshu Mehta Mar 08, 2007

Many of you may have heard of the supposed power of Mac OS X that it gains from Unix-y underpinnings. Like one of those proclamations that make you seem smart, but you’re not sure why (“Red wine is good for you because it has antioxidants,” you say with an air of superiority), the BSD-like innards of OS X are something that Mac enthusiasts like to cite, but rarely get their hands dirty in.

So for those of you who have always wondered how to make your life a little easier or more powerful or impressive with command line voodoo, I am putting together a little beginner’s guide here. This is truly a beginner’s guide, so those of you who know a little bit about this stuff may want to wait until I get to part 3 or 4 of this series.

Why?
You may be wondering why you would possibly need to enter commands on a text prompt when OS X has all this wonderful GUI-ness. Everyone has their own reasons—Matrix-y coolness being only one of them. There are a few good reasons, in no particular order:

  • GUIs can get bogged down. Who will let you debug and/or fix that situation without a reboot? Terminal.app!
  • GUIs are no good for repetitive small tasks.
  • GUIs are no good for chaining small tasks together. You can take the output of one command and feed it into the next effortlessly. Say you have one command that lists the currently running programs (ps) and another that searches for lines with a particular string (grep). Now, in one single line, you can put them together to search for all running applications that contain the string “Microsoft”—ps | grep Microsoft. That’s it.

Basics
To run anything on the command line in standard OS X boxes, you need to launch Terminal.app. When you get better at this stuff, go get iTerm and use it instead. If you’re a really keen newbie, you will keep Terminal.app in your Dock.

Each command can have “arguments” and “options.” Arguments are usually the object you want the command to act on. Options usually specify how you want it to perform a specific task. For example, ls lists contents of the current directory. If you give it the argument /Users/ by saying ls /Users/ it will list the contents of the /Users directory. If you give it the option ‘-l’ by saying ls -l /Users it will give you a lot more information about the files it lists. You can usually chain together many options, and for many commands, you can chain together many arguments as well.

man
Simple Usage: man

This command allows you to find out how to use a particular command. Try man ps to find out more about the command I mentioned above. For that matter, try man man to find out all about the man command! “man” stands for manual.

pwd
Simple Usage: pwd
The first time you start Termnal.app, you will be “in” your home directory. This is /Users/(username). You can find out what your current directory is by typing pwd at the prompt. If you ever get lost in some strange directory hierarchy, use this command to find out where you are. Your “home” directory is also represented by the ~ character. This means that ~/Documents is the same as /Users/(username)/Documents.

cd
Simple Usage: cd

Which brings us to “cd.” Typing cd /Users/(your-username)/Documents will take you to that directory. If you get lost, simply typing cd will bring you back to your home directory.

ps
Simple Usage: ps aux
As I mentioned briefly above, ps will simply list all the currently running programs. This includes ones that you have explicitly launched and ones that OS X has launched itself in the background. It helps you to find out if there are processes you did not know were running or if you need to know which processes are a resource hog. The “aux” options I added to the basic command make sure it gives you processes launched by any user and also provides you with many more interesting facts about the processes such as start time, the user that launched the process, and so forth. Again, try man ps to find out a lot more.

ls
Simple Usage: ls
ls simply lists the contents of your current directory (which you can find out using pwd). You can also make ls list the contents of any directory by supplying it the directory path in its argument. For example, ls /Users/(your-username)/Documents. You can also get a lot more information about the files by supplying the option -l (ls -l), which will provide you with the user who created and “owns” the file, when it was modified, and so on.

more
more is a nifty command that works best with other commands. For example, say you tried the ls command above and the results gave you multiple pages of results. You would love for it to show it to you one page at a time so you can scroll through at your own pace, right? Try ls | more. It will wait for you to hit the space bar before it shows you the next page of information. Hit “q” (or scroll until the end) to quit from it.

This brings us to the pipe—the character “|.” This allows us to pipe the output of one command to the input of another. Above, when we did ls | more, the output of ls is provided to the more command which allows you to scroll through it gradually. I will deal with this command to show you complex hybrid commands in future articles.

clear
At any point in time, type clear to clear the screen.

That’s all for this time, but I will give you a lot more in subsequent articles. Here are a few harmless commands to try on your own—em>cat, top, diff and df. As always, remember man and enjoy!

Comments

  • Thanks a lot for this! I’m looking forward to the next one!

    mattyg had this to say on Mar 08, 2007 Posts: 1
  • xwiredtva had this to say on Mar 08, 2007 Posts: 172
  • When I open Terminal I get this line:

    <i>Welcome to Darwin!
    tcsh: /sw/bin/init.csh: No such file or directory.</i?

    Don’t no how to fix this, or even if I need to. Help?

    Artisticulated had this to say on Mar 08, 2007 Posts: 2
  • When I open Terminal I get this line:

    tcsh: /sw/bin/init.csh: No such file or directory.

    Don’t no how to fix this, or even if I need to. Help?

    Preview? we don’t need no steenkin’ preview!

    Artisticulated had this to say on Mar 08, 2007 Posts: 2
  • @Artisticulated- do you have Fink installed? What have you installed using it so far? Have you tried installing tcsh by any chance? If so, edit the file /Users/username/.tcshrc and remove the line source /sw/bin/init.csh

    Devanshu Mehta had this to say on Mar 08, 2007 Posts: 108
  • Page 1 of 1 pages
You need log in, or register, in order to comment