First commands
First commands
Change Directory
When you open the terminal you will automatically be placed in the current user’s Home Directory. This is all well and good, but if everything you wanted to do was in this one directory you’d probably just use Finder. There will be more explanation about how the average Mac OS X hard drive is structured later. But first, let’s just find out how to take this terminal out for a spin.
Lets go to the Desktop by typing cd Desktop

You are immediately returned back to the command prompt, but now something has changed. It no longer says ~ after the computer name but rather Desktop. That is because you changed the current directory and now any command you issue will be as if you are there instead of your home directory.
This should be different to you than you might have previously been used to. When using a graphically based shell like Finder you may have multiple windows open with different folders. Perhaps you have 2 or 3 different programs running, each with their own document. In a terminal you only have one current working directory, and for the time being you will only be running a single program at a time.
When you run a program, you might be able to give it a filename to perform some action on. The way you would type that will vary depending what directory you are in. If you have a file “myfile.txt” on the Desktop for instance, you cannot access it from the Documents directory by just typing “myfile.txt.” You might have to specify the entire Path to the file.
More on this later, but for now just keep in mind that what directory you are in when doing something can be very important in the future.
Lets go back to the directory we came from:
#> cd .. Important: There is a space between “cd” and “..”
No matter where you are, typing cd .. will take you up one directory. The double period is an alias for Parent Directory. A single period is an alias for the current directory. The usefulness of that will be seen later.
Lets go into your Documents directory:
#> cd Documents
Now, instead of jumping back to the Parent Directory we are going jump to our home directory:
#> cd Yes, all by itself
You should now see something like this:

Every time we return to the home directory, do you see that the name of the directory changes to a tilde. That is because that is another alias, it means “my home directory.” No matter what user you are, or where you are on the system typing cd by itself will bring you back home. Comforting thought, no?
You are going to find that there are lots of little aliases that mean something very important. Most of the non-alphanumeric characters will actually have some significant meaning to our shell, so be careful when running commands with them. We’ll talk more about the general structure of a command, and things to watch out for later.
Lets start off with some commands that will allow you to navigate your way around the hard drive. These commands are a good testing ground because shouldn’t be able to delete or modify anything. So go wild and experiment, as limited as that will be.
Listing Files
When you open a folder in Finder you will automatically receive a list of all of the files in the folder. This makes perfect sense. In the text world, this doesn’t happen automatically for you. In fact, a lot of the times it might be pretty unhelpful for the screen to continually be updated with a list of files. But alas, you probably haven’t memorized the location of every single file on your computer, and can recall it with 100% accuracy.
The command get a list of the files in the current working directory is ls.

By default ls will list all “unhidden” directories and files in the directory in alphabetical order, in columns. The height of the columns will be determined by number of files, and the number of columns will be determined by the width of the window. If there are unhidden files then there must be hidden files, right? A Hidden File or directory in Bash is one who’s filename starts with a period. Mac OS X and Finder also have their own system for hidden files. Finder will ignore the ones starting with a period, but it will also ignore special files that have been set as hidden by Mac OS (or us, once we learn the right command :)
Here are some common ways to run ls that you will likely find useful:
#> ls -l
That is an “el” not a “one”. This will not only list the files but information about each one.

#> ls -a
This will display the hidden files with the rest of the files.

Notice that the unhidden files have nice pretty names, and the hidden ones are less than friendly? They’d be why Apple hides them in Finder too. You should try this command from the root of your hard drive. And try combining the settings.
#> cd /
#> ls -a -l
The next page on Basic Command Structure will give you an idea of how stuff typed at the command line is made up, and how you can combine different settings like above to achieve better results.