Last time, I showed you how to read a man page for a command by typing man followed by the command. In lesson 2, I showed you how to list files in long format by typing ls followed by a hyphen and an option, like ls -l. Why does one command use a hyphen, and the other not?
It’s all in the difference between options and targets. Here’s how most commands are entered:
command -options target
The command is the thing you want to do, the option is how you want to do it, and the target is where you want to do it. Some commands need an option, some don’t. Some commands need a target, some don’t. But if you use them, the computer can only tell the difference between an option and a target based on that little hyphen. Imagine it wasn’t there, and you tried typing
Are you telling the computer to list all items, or list the folder named “a”? The hyphen is how the computer knows you are typing an option or a target. Options have a hyphen before them, targets don’t.
Try the following commands:
ls ls -l
ls simply lists the contents of the folder you’re currently in. If you opened a new Terminal window, you’d start in your home folder, so ls will list the contents of your home folder. ls -l lists the same folder in long format. But what if you wanted to list some other folder? You could go to the folder in question with cd, the Change Directory command, and then run ls, like this:
cd /Applications ls -l
But that is two steps, and then if you want to be back in your home folder, you have to go back there with the cd command again. So it’s really three steps:
cd /Applications ls -l cd /Users/yourusername
Why do it in three steps when you can just do it in one?
ls -l /Applications
Options and targets are optional, but many, many, many commands use them. Practically all of them, in fact. Next time I will explain what that slash before Applications was about, and it will all start to come together, I promise. But for now, here are a few more commands. Try them yourself to see what happens, or at least just think to yourself which ones have a target, which ones have options, and which ones have both.
uptime ls -lah ls -lah /Library ls ~ man ls man top top top -s5
Don’t forget, some of those commands will keep running until you quit them. Remember how to quit, back in an earlier lesson? Aah, I’ll let you off the hook, it’s q!