Working with Files (MacBook)

If you’ve used a computer for any time at all, you’re no doubt familiar with the idea of files. Even before the first floppy drive appeared in personal computers, operating systems have stored data in files . . . they date back to the days when a mainframe computer occupied an entire floor of an office building. Mac OS X is no exception, and it’s important to understand how Mac OS X arranges them into folders and how you go about accessing them via the command line. This section describes the basic file and folder information that you need to know to tame the beast that is UNIX.

Paths

Before you dive into UNIX commands, you should first know a few facts . . . nasty things, facts, but you can’t earn your pair of techno-wizard suspenders without ‘em. For starters, as a Mac user, you might not be familiar with how paths work in UNIX. A path is simply a textual representation of a folder or file. The simplest path is your Home directory, which is denoted by a tilde character (~) — the tilde character acts as the equivalent of /Users/<your short account name> . Any folder within the Home directory is represented by the folder’s name preceded by a forward slash (/). For example, a document entitled myDoc that resides in the current user’s Documents folder would have a path like this:
~/Documents/myDoc
Similarly, a folder named myFolder that resides in the current user’s Documents folder would have a path like this:
~/Documents/myFolder/
As you’ve probably surmised, a folder and a directory are two different names for the same thing. Folder is the name with which most Mac users are familiar, and directory is a term that UNIX power users prefer. I use the terms interchangeably throughout the remainder of the topic.
Because Mac OS X is a multiuser environment — and your applications and files may be located in many different folders across your hard drive — you will want to work with folders or files somewhere other than in your Home folder. Starting from your Home folder, enter the following command:
cd ..
This moves you to the folder right above your Home folder, which happens to be the Users folder. Using another quick ls command will show you all users who are permitted to use the machine. (By the way, Shared isn’t a user — it’s a folder with privileges set so that any user can access its contents.)
Enter cd .. once again, and you find yourself at the root of your main hard drive. The root directory is what you see in the Finder when you double-click your hard drive icon on the Desktop. A user’s Home directory is represented by a tilde character (~), and the root of the hard drive is denoted by a forward slash (/), as displayed by the prompt:


WHITEDRAGON:

It’s easy to return to your Home directory by following this sequence:
WHITEDRAGON:/ cd Users WHITEDRAGON:/Users  cd WHITEDRAGON:-
Here’s a faster way. Instead of moving through each successive folder until you reach your intended destination, you can specify the path by using just one cd command:
WHITEDRAGON:/  cd /Users/ WHITEDRAGON:-
Of course, the Home directory is a special folder in that you can also navigate there by simply entering cd ~, but the main point here is that you can navigate directly to specific folders by using that folder’s path in conjunction with the cd command.
Furthermore, when you navigate your hard drive by using paths, you can jump directly to your desired destination from any place. When you enter cd .. , it is in relation to your current position, whereas entering
cd /Users/
will always take you to the same directory, regardless of your starting point.
tmp116-14_thumb
Copying, moving, renaming, and deleting files
After you’re comfortable with moving around the hierarchy of your hard drive, it’s a cinch to copy, move, and rename files and folders.
To copy files from the command line, use the cp command. Because using the cp command will copy a file from one place to another, it requires two operands: first the source and then the destination. For instance, to copy a file from your Home folder to your Documents folder, use the cp command like this:
cp -/MyDocument -/Desktop/MyDocument
Keep in mind that when you copy files, you must have proper permissions
to do so! Here’s what happens when I try to copy a file from my Desktop to another user’s Desktop (strangely named fuadramses):
WHITEDRAGON:-  cp -/Desktop/MyDocument /Users/ fuadramses/Desktop/MyDocument
Denied! Thwarted! Refused!
cp: /Users/fuadramses /Desktop/MyDocument: Permission denied
If you can’t copy to the destination that you desire, you need to precede the cp command with sudo. Using the sudo command allows you to perform functions as another user. The idea here is that the other user whom you’re “emulating” has the necessary privileges to execute the desired copy operation. When you execute the command, the command line asks you for a password. If you don’t know what the password is, you probably shouldn’t be using sudo. Your computer’s administrator should have given you an appropriate password to use. After you enter the correct password, the command executes as desired.
In case you’re curious, sudo stands for set user and do. It sets the user to the one that you specify and performs the command that follows the username.
sudo cp -/Desktop/MyDocument /Users/fuadramses/Desktop/
MyDocument Password:
A close cousin to the cp (copy) command is the mv (move) command. As you can probably guess, the mv command moves a folder or file from one location to another. (I told you that all this character-based stuff would start to make sense, didn’t I?) To demonstrate, this command moves MyDocument from the Desktop folder to the current user’s Home folder:
mv -/Desktop/MyDocument -/MyDocument
tmp116-15_thumb
Ah, but here’s the hidden surprise: The mv command also functions as a rename command. For instance, to rename a file MyDocument on the Desktop to MyNewDocument, do this:
mv ~/Desktop/MyDocument ~/Desktop/MyNewDocument
In this case, you can see that the mv command is really copying the original file to the destination and then deleting the original. Because both folders in this example reside in the same folder (-/Desktop/), it appears as though the mv command has renamed the file.
Again, like the cp command, the mv command requires that you have proper permissions for the action that you want to perform. Use the sudo command to perform any commands that your current user (as displayed in the prompt) isn’t allowed to execute. On UNIX systems, not all users are necessarily equal. Some users can perform functions that others can’t. This is handy for keeping your child’s mitts off important files on your laptop. It also creates a hurdle should you choose to work on files while using your child’s restricted user account. The sudo command lets you temporarily become another user — presumably one that has permission to perform some function that the current user can’t.
What would file manipulation be without the ability to delete files? Never fear; UNIX can delete anything that you throw at it. Use the rm (short for remove) or rmdir (short for remove directory) command to delete a folder or file. For example, to delete MyNewDocument from the Desktop folder, execute the rm command like this:
rm ~/Desktop/MyNewDocument
Once again, deleting files and folders requires that you have permission to do so. In other words, any time that you manipulate files with the command line, you’re required to have the proper permission. If your current user lacks these permissions, using sudo helps. You should also check to make sure that your target is correctly spelled and that no pesky spaces that could wreak carnage are lurking in the command.

Opening documents and launching applications

Launching applications and opening documents is child’s play for a UNIX pro like you. The open command does it all. For example, to bring the Finder to the foreground without touching the mouse, use
open /System/Library/CoreServices/Finder.app
To open a document from the command line, follow a similar scheme. For example, to view an image named mylmage.tif that’s stored in your Documents folder using the default application (typically Preview), try this:
open -/Documents/mylmage.tif

Next post:

Previous post: