< Blog Home
Getting to know the Terminal Part 1: Basic File Operations
The Terminal is an application that drives fear into the heart of a lot of Mac users - an application they only dream of using in their worst possible nightmares.
It's really not that scary - in fact, it can actually be a very useful timesaving device. You can perform operations on a large number of files at once in a fraction of a second, saving a lot of time.
Although the GUI is prettier and more intuitive, constantly moving, clicking and dragging the mouse around the screen wastes time - not to mention that rendering the GUI takes away precious processing cycles from the operation you are trying to perform. And you don't even need to constantly type things either - you can write a shell script to perform a task and set it to run automatically.
If you are thinking of entering the visual effects industry, most employers will expect basic command-line knowledge and shell scripting abilities. While most VFX houses use some form of Linux, Mac OS X's Terminal is almost identical with the exception of a small number of proprietary commands.
So let's get started.
First of all, open up the Terminal by navigating to /Applications/Utilities
and double-clicking the Terminal application. I find it useful to ctrl-click the dock icon and select Keep in Dock
so it is always there for convenience, but this is up to you.
A window like this will appear:
The title bar of my window says "Terminal - bash - 80x24"
refers to the shell I am using. There are various different shells available, each with minor differences. We will just concentrate on bash. Incidentally, if your title bar doesn't say "bash", type
and press enter.
80x24 refers to the number of characters that can be displayed on screen at once - 80 horizontally and 24 vertically. If you resize the window these numbers will change. It doesn't matter if yours has a different value to mine.
You will see something like this on screen:
Last login: Mon Jan 5 15:27:17 on ttys000
MacBook is the name of my machine, Jon is my username. The ~ indicates that the current directory is my user directory. To view the full path of the current directory, type:
This stands for Print Working Directory
and on my machine outputs the path
. The working directory is the directory the shell will perform all commands in unless specifically told otherwise.
To change to a different working directory use the
command like so:
The prompt changes to
It's worth noting at this point that all commands are case-sensitive, so
is not the same as
is not the same as
There are some specific shortcuts you can use with the
command that are summarized below.
cd .. (that's
cd followed by two dots)
Moves up one directory. If the working directory was /Users/MyName/Documents, it would change to /Users/MyName.
cd - (that's
cd followed by a dash)
Goes back to the previous directory you were in. So if you were in /usr/bin and you changed to /Library, this command will refer you back to /usr/bin again.
Changes to the root directory
It's worth mentioning here that shell commands are easily confused by spaces in a file path. If you must type a path with spaces, you must either:a) Surround it in quotes
cd "~/Desktop/My Spaced Filename.doc"
or b) Use escape characters
Escape characters are characters placed before a potentially conflicting character (a space in this case) so that the shell knows to treat it as text and not as a command. The escape character for the Unix shell is \ (backslash).
cd ~/Desktop/My\ Spaced\ Filename.doc
Viewing directory contents
If you want to view the contents of a directory inside the Terminal window, use the
command. ls is short for "list" (you'll notice that shell commands are generally quite short as they need to be typed often).
returns system files and folders that would normally be hidden by OS X, except for files and folders that have a dot at the front of their name such as .DS_Store. You can show these by typing
(short for "all").
You can also specify a directory as a parameter such as
and it will list the contents of this directory instead of the current one.
You can set several options when you call ls such as:
ls -l (that's lower-case L)
Returns more detailed results including file sizes, permissions and modification dates.
Forces one entry per line.
"Human-readable" mode displays file sizes in kilobytes, megabytes and gigabytes instead of bytes.
Recursively lists subdirectories (be careful - this can take a while).
You can combine as many of these parameters as you like such as:
ls -l -R
Or more simply:
(note: some parameters automatically override others)
These are just a few parameters of many. For exhaustive details of the parameters available, type:
This is an important Unix concept that also translates to OS X. Man (manual) pages are documentation files easily accessible from the shell using the
command. They will tell you everything you could possibly want to know about the command such as its syntax, parameters, compatibility and return values (more on that in the upcoming scripting tutorial).
You can use
with any command such as
. With the man page open, press Space to go to the next page and press q to quit and return to the shell.
This gives you documentation instantly at your fingertips, even if the machine you are using doesn't have an internet connection.
Basic File Operations
command to copy files like so:
cp [source file] [destination path]
Here's an example on my system:
cp UntitledDoc.txt ../Documents
This will set the working directory to my desktop. It will then copy UntitledDoc.txt to my Documents folder (remember that .. refers to the parent directory).
You can also create a duplicate of a file within a directory by changing the destination filename:
cp UntitledDoc.txt UntitledDocDupe.bak
To copy a folder and all its subfiles and subfolders, use
to learn about the other parameters.
To move a file, use the
command with the exact same syntax as the
To rename a file, you can either use the
command (same basic syntax as
) or use
mv myfile.txt mynewfile.txt
To delete a file, use
parameters are listed below:
Delete directories as well.
Force delete files, even if they are write protected.
Overwrite files before deleting them. This is similar to the Secure Empty Trash option in Mac OS X.
Recursively delete files inside subfolders. Use with the -d command to delete a folder and all of its subfiles and subfolders.
That's it for the first tutorial. In Part 2 I will be covering more file operations, working with directories, wildcards and permissions.
Posted by Jon Chappell on Jan 5 2009 to Apple