Introduction to the Terminal

Terminal.app provides a window for display and a scroll buffer of the previous output. It is merely a container for other programs.

It runs that program and displays the output. It can run any program, we just get the most use of something called a shell.

The Shell

terminalscrn

A shell is nothing more than our interface to the system. There are many different kinds of shells, each with their own features. It can be as simple or complex as the author wants to make it. Our default shell in Mac OS X is called Finder. You should be very familiar with this application.

Finder provides you with a robust interface to access your applications, manage your documents, and control the computer. Most of the time Finder is very friendly and intuitive. It has been designed with user experience as its primary goal.

Our default text-based shell on the other hand is called Bash. Text interfaces have been around as long as modern computing. Using a computer with nothing but a keyboard was the primary means of talking to a computer for much of the 70’s and 80’s. This means that when you open up Terminal.app you are delving into a system that has been in development for almost 30 years.

Absolutely everything that you have done with Finder, Mail, Safari, Calculator, Automator, Disk Utility, Network Utility, Software Update, Spotlight, TextEdit, and yes, even some of the iLife Apps can be done from the terminal. That can be a daunting concept, especially when you come to realize that all of these really useful and powerful utilities were not designed with usability in mind. In the text world, function comes before form.

Just to give you an idea here, one of the most confusing things I ran into the first couple of days using a terminal was the help system. The very thing that is supposed to help you out of a jam will be terribly confusing to you for quite some time.

Lets type something

Changing the directory is something you will do often in a shell.
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.

Type :
cd desktop

terminal_cd

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


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.


Listing files is easy too:
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.

terminal_lsl

#>
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

Using the terminal you should now be able to wander around your hard drive with relative ease.

Moving files:

To move a file or directory is very simple. The easiest form of this is:

#>
mv sourceFile destinationFile

This will move (or rename) the file named sourceFile to destinationFile. For a directory the command is the exact same. It will move (or rename) the directory, and the contents inside that directory will be untouched.

Copying files:

As luck would have it copying a file is remarkably similar to moving a file. The basic makeup of the command is exactly the same.
When copying multiple, just like when moving, the final argument needs to be a directory or else you will receive an error message.

When copying files it is sometimes useful to see what file has been copied. This is closest we’ll get to a progress meter. This mode is called verbose mode, because instead of the program being quiet or terse, it will print out a summary of what it is doing. The option switch for verbose mode is
-v.

Copying a directory can be a little trickier because by default the cp utility will not copy a directory. You have to tell it that you’d like to recursively copy the directory to the new location. This is done with the
-R option, and will cause the contents of the directory be copied.
Notice that the entire srcDir directory has been fully copied into destDir. This is important to note, because if you were to put a forward slash (/) after srcDir it would actually only copy the contents of the directory.
Be very careful about that distinction, as at times it can make a huge difference. For instance if you were copying multiple directories into the same directory, you might accidentally combine the contents of all of the contents into one.

Deleting files:

Deleting a file is very simple. I have only separated it out because it is very important that you are sure you want to delete the files you are deleting. When using the terminal there is no trash, there is no undo. The files will be gone.

To delete a directory and its contents you must tell the program to recursively delete it. This can be dangerous if you get the wrong.
The
-v was used to specify which files are being deleted. The -i switch can be used to confirm the file being deleted when it is being deleted.
ffmpeg osx