Maintain, optimize and troubleshoot your NLE
Professional cloud workflow platform
Simplified media management
< 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.

Setting up

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".

bash 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 bash 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.

Directory navigation

You will see something like this on screen:
Last login: Mon Jan 5 15:27:17 on ttys000
MacBook:~ Jon$


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:

pwd

This stands for Print Working Directory and on my machine outputs the path /Users/Jon. 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 cd command like so:
cd Documents

The prompt changes to MacBook:Documents Jon$.

It's worth noting at this point that all commands are case-sensitive, so cd Documents is not the same as cd documents, and cd is not the same as CD.

There are some specific shortcuts you can use with the cd 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.

cd /
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 ls command. ls is short for "list" (you'll notice that shell commands are generally quite short as they need to be typed often).

cd /
ls


Returns:


Notice that ls 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 ls -a (short for "all").

You can also specify a directory as a parameter such as ls /usr/bin 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.

ls -1
Forces one entry per line.

ls -h
"Human-readable" mode displays file sizes in kilobytes, megabytes and gigabytes instead of bytes.

ls -R
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:
ls -lR

(note: some parameters automatically override others)

These are just a few parameters of many. For exhaustive details of the parameters available, type:

man ls

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 man 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 man with any command such as man cd, man ls, man echo. 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

Use the cp command to copy files like so:

cp [source file] [destination path]

Here's an example on my system:
cd ~/Desktop
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 cp -R. Type man cp to learn about the other parameters.

To move a file, use the mv command with the exact same syntax as the cp command.

To rename a file, you can either use the rename command (same basic syntax as cp) or use mv like so:
mv myfile.txt mynewfile.txt

To delete a file, use rm:
cd ~/Desktop
rm myfile.txt


Some useful rm parameters are listed below:

-d
Delete directories as well.

-f
Force delete files, even if they are write protected.

-P
Overwrite files before deleting them. This is similar to the Secure Empty Trash option in Mac OS X.

-R
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, Software