Okay, last time we didn’t cover a lot, true. In fact, we didn’t cover much more than launching the Terminal. But the journey of 1,000 lines of code begins with a single prompt, or something like that.
By the way, side note… check out In The Beginning was the Command Line, a fascinating article written by Neil Stephenson in 1999, about the inevitable death of paid operating systems in the face of the rising free software movement. You probably are thinking to yourself, you paid for the operating system you’re using right now. Well, so did I, and so did Neil. He admits he got a lot wrong. But the article is just as interesting for what it got wrong as what it got right. And it’s been posted online for free. We now return you to your lesson.
Okay, let’s try some more commands. Launch the Terminal, and type the following:
And press return. Assuming you haven’t done anything else, and just launched the Terminal, you will be in your home folder, and when you type ls, you should see something similar to this:
Desktop Downloads Library Music Public Documents Movies Pictures Sites
Figure out what ls does? That’s the list command. It will list the items in your home folder. Yours might differ slightly; You may have a Dropbox folder, or some other custom folders, but basically, you will see a list of the folders and files in your home folder.
Take a look at your home folder in the Finder. You know, the graphical user interface (GUI) you use every day. If you’re using Lion, you won’t see your Library folder- its invisible in the finder, but its right there in the command line.
Okay, so you can see whats in your home folder, super. Whats the big deal, you might be thinking? Well the power of command line commands is in all the ways they can be modified. ls is just the tip of the iceberg. Try this next:
And press return. That’s “lima sierra space minus lima” if you’re having trouble reading the font. What happens this time? Probably something like this:
drwx------+ 3 demo staff 102 May 16 16:23 Desktop drwx------+ 4 demo staff 136 May 16 16:23 Documents drwx------+ 4 demo staff 136 May 16 16:23 Downloads drwx------+ 25 demo staff 850 May 16 16:23 Library drwx------+ 3 demo staff 102 May 16 16:23 Movies drwx------+ 3 demo staff 102 May 16 16:23 Music drwx------+ 4 demo staff 136 May 16 16:23 Pictures drwxr-xr-x+ 5 demo staff 170 May 16 16:23 Public drwxr-xr-x+ 3 demo staff 102 May 16 16:23 Sites
This also lists the contents of your home folder, but in a different format, and with more detail. The “-l” is called an option or a modifier to the ls command. It changes the way the command ordinarily works. “-l” is short for “long format”. From left to right, we see file permissions, number of items in each folder, the owner, the group, the size, the date modified, time modified, and name.
Here’s another way to modify the ls command:
And press return. Can we all agree to press return after we type a command? Okay, cool. So what happened?
. .bash_history Downloads Public .. .cups Library Sites .CFUserTextEncoding .dropbox Movies .DS_Store Desktop Music .Trash Documents Pictures
Woah, what’s all that extra stuff? Like .Trash and .cups and .bash_history? Well, those are items that are important to the working of your Mac, but things that could freak people out, or cause confusion to new users. Worse, your Mac could be messed up if they’re deleted. So Apple, in Their Infinite Wisdom, decided to hide them. But the -a option shows all. -l for Long format, -a for All. Get it?
About those hidden items. Notice something they all have in common? Yup, they all start with a period. But didn’t I say earlier that the Library folder was hidden? That doesn’t start with a period! What the heck is going on?!?
Don’t panic. There are two ways to make items invisible on a Mac. The traditional way that Unix has supported forever and ever is to simply start a file name with a dot. Those items are hidden in the graphical user interface (GUI), and in the command line, unless you use the -a option. But some items can’t just have their name changed without screwing up thousands of other lines of code. Apple couldn’t just change the name of Library to .Library without causing a lot of trouble. So Apple came up with another method of hiding items, called a hidden flag. We’ll see more about flags in a later lesson. For now, all you need to know is,
Some items are invisible from the GUI, but you can readily see them in the command line. Other items are hidden from the GUI and the command line. You can see them by adding the -a option.
So, you can list items with ls. You can list items in long format with ls -l. And you can list all items with ls -a. But, what if you want to list all items, AND in long format?
Will do the trick. See what happens?
drwxr-xr-x+ 19 demo staff 646 Jun 18 22:25 . drwxr-xr-x+ 6 root admin 204 May 16 16:23 .. -rw-r--r-- 1 demo staff 3 May 9 14:07 .CFUserTextEncoding -rw-r--r--@ 1 demo staff 21508 Jun 18 22:25 .DS_Store drwx------ 6 demo staff 204 Jun 18 13:24 .Trash -rw------- 1 demo staff 397 Jun 18 19:25 .bash_history drwx------ 3 demo staff 102 Jun 5 11:32 .cups drwx------ 14 demo staff 476 Jun 18 19:01 .dropbox drwx------+ 3 demo staff 102 May 16 16:23 Desktop drwx------+ 4 demo staff 136 May 16 16:23 Documents drwx------+ 4 demo staff 136 May 16 16:23 Downloads drwx------+ 25 demo staff 850 May 16 16:23 Library drwx------+ 3 demo staff 102 May 16 16:23 Movies drwx------+ 3 demo staff 102 May 16 16:23 Music drwx------+ 4 demo staff 136 May 16 16:23 Pictures drwxr-xr-x+ 5 demo staff 170 May 16 16:23 Public drwxr-xr-x+ 3 demo staff 102 May 16 16:23 Sites
We combined -l and -a and it applied both. But you just need one minus sign, then mush together all the options you please. Try this:
Aah! Whats all that? The -l was LONG format, a was ALL items, h labeled the size column in HUMAN READABLE format, Byes, Kilobyes, Megabytes, etc and the G option color-coded your files, folders and links. And that’s just for starters. There are dozens and dozens of options for just this one command! I’ll show you how to look up more of them on your own next time. But for now, two final lessons.
First, notice the order? Try mixing it up. Type
Did they work? Yes, they did. After the minus, you can add your options in any order and they will (almost always) still work fine.
Finally, did you notice I typed a lower case a, lower case l, lower case h, and a capital G? That was no typo. The command line is case sensitive. -l and -L do VERY different things, don’t mix them up! I’m not saying being careless with cases could make the difference between, say, copying a file or erasing it, but, well… it could. So don’t just guess! If you’re not sure how a command works, look it up before you try it! Don’t cry to me if you delete your wedding photos! Okay? Okay. Thank you.