Copying, Moving, Renaming, and Deleting Files from the OS X Command Line - dummies

Copying, Moving, Renaming, and Deleting Files from the OS X Command Line

By Mark L. Chambers

After you’re comfortable moving around the hierarchy of your hard drive, it’s a cinch to copy, move, and rename files and folders from the OS X command line.

To copy files from the command line, use the cp command. Because using the cp command copies 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 ~/Documents

Keep in mind that when you copy files, you must have proper account permissions to do so! Here’s what happens when you try to copy a file from the Desktop to another user’s Desktop (an account named fuad):

WHITEDRAGON:~ mark$ cp ~/Desktop/MyDocument ~/Users/fuad/Desktop/MyDocument

Denied! Thwarted! Refused!

cp: /Users/fuad/Desktop/MyDocument: Permission denied

If you can’t copy to the destination 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 the password, you probably shouldn’t be using sudo. Your computer’s administrator should’ve given you an appropriate password to use. After you enter the correct password, the command executes as desired.

The curious reader might want to know that 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/fuad/Desktop/MyDocument

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.

To demonstrate, this command moves MyDocument from the Desktop folder to the current user’s Home folder:

mv ~/Desktop/MyDocument ~/MyDocument

Ah, but here’s the hidden surprise: The mv command can be used as a rename command. For instance, to rename a file MyDocument on the Desktop to MyNewDocument, do this:

mv ~/Desktop/MyDocument ~/Desktop/MyNewDocument

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 you want to perform. Use the sudo command to perform any commands that your current user (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 (handy for keeping your child’s mitts off important files on your computer). 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 who has permission to perform some function that the current user can’t.

What would file manipulation be without the capability to delete files? Never fear; Unix can delete anything you throw at it. Use the rm (remove) or rmdir (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

Again, deleting files and folders requires that you have permission. In other words, any time 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.