Alex Kaloostian

Apple Certified Master Trainer | Systems Integrator | Video Editor | Motion Graphics Artist

Command Line Basics 8 – copying, moving, deleting

Leave a comment

Okay, let’s get right to it! Last week you learned how to go to a specific folder, and how to look at what’s inside a certain folder. Today we’re going to talk about copying, moving, renaming and deleting files.

Let’s pretend you have a file in your home folder, called file.txt. No, wait, you know what? Lets MAKE a file called that! You could stop what you’re doing right now, open your word processor of choice, fire off a few AC/DC lyrics, and save the file, and name it file.txt, but since we just need some dummy file that we can play around with and it doesn’t really matter at all whats IN the file, lets play with the touch command. Touch can create a blank file. Open the Terminal and type:

touch file.txt

To create a blank folder, you can use mkdir.

mkdir zfolder

Now list the contents of your home folder. You’ll see a file named file.txt and a folder named zfolder:

drwxr-xr-x 1 root staff 15 May 9 15:35 Desktop
drwxr-xr-x 1 root staff 17 May 9 15:39 Documents
drwx------+ 6 overstim staff 204 Jul 16 16:33 Downloads
drwx------@ 44 overstim staff 1496 Jul 9 10:46 Library
drwxr-xr-x 5 overstim staff 170 Jul 11 14:13 Movies
drwx------+ 5 overstim staff 170 May 23 19:46 Music
drwxr-xr-x 5 overstim staff 170 Jul 10 21:44 Pictures
drwxr-xr-x+ 5 overstim staff 170 May 9 15:41 Public
-rw-r--r-- 1 overstim staff 0 Jul 17 15:17 file.txt
drwxr-xr-x 2 overstim staff 68 Jul 17 15:18 zfolder

See? There they are at the bottom. (You may have noticed, the command line alphabetizes items strangely: capital letters come first, then lower case. If you named your file or folder with a capital letter, it may show up higher in the listing). The file will be tiny, because it’s blank. It’s kind of just a placeholder. So don’t bother opening it. We’re just going to use it to try out the next few commands.

Copy

The copy command is cp. Just type cp, a space, the name of the file, a space, and the name you want the new copy to have. Like this:

cp file.txt file2.txt

Don’t forget the extension! If you just call the new file “file2”, it will really call it that, and the Mac won’t know its a text file. You gotta be specific. You can use the same command to copy a folder, however, you want to copy the folder, and everything inside it, recursively, so you have to add the R option. like this:

cp -R zfolder zfolder2

Yes, capital R, not lower case.

dora:~ overstim$ ls -l
total 15
drwxr-xr-x 1 root staff 15 May 9 15:35 Desktop
drwxr-xr-x 1 root staff 17 May 9 15:39 Documents
drwx------+ 6 overstim staff 204 Jul 16 16:33 Downloads
drwx------@ 13 overstim staff 442 Jul 13 10:39 Dropbox
drwx------@ 44 overstim staff 1496 Jul 9 10:46 Library
drwxr-xr-x 5 overstim staff 170 Jul 11 14:13 Movies
drwx------+ 5 overstim staff 170 May 23 19:46 Music
drwxr-xr-x 5 overstim staff 170 Jul 10 21:44 Pictures
drwxr-xr-x+ 5 overstim staff 170 May 9 15:41 Public
drwxr-xr-x+ 4 overstim staff 136 Jun 21 15:19 Sites
-rw-r--r-- 1 overstim staff 0 Jul 17 15:17 file.txt
-rw-r--r-- 1 overstim staff 0 Jul 17 15:20 file2.txt
drwxr-xr-x 2 overstim staff 68 Jul 17 15:56 zfolder
drwxr-xr-x 2 overstim staff 68 Jul 17 15:59 zfolder2

All righty, we’re off to a good start. We can easily copy files with cp, and folders with cp -R. This is fine if we’re copying an item in the same place. But we can also copy an item to a different location. All we have to do it type a more specific path for the copied file. Like this:

cp file.txt zfolder/file3.txt

Can you guess what that command will do? It will copy file.txt, and place the new copy inside the zfolder. Similarly, you can grab a file that’s off in some other distant folder, and copy it right to where you are, like this:

cp -R /Applications/Utilities/Terminal.app Terminal2.app

Okay, there was a little more going on there, so let’s break it down: We typed cp, for copy. Next was -R, because applications are considered folders. I know it seems weird, but roll with it. Next, we wanted to grab a copy of the Terminal.app, located inside the Utilities folder, which is located inside the Applications folder. And finally, the name of the new copy was Terminal2.app.

Why did we have to type /Applications/Utilities/Terminal.app? Because that’s where the original is. We have to specify exactly what we want to copy. But why didn’t we have to type the whole path for the copy? Because, unless specified, the new copy will be placed right where you are. You’re in your home folder when you’re typing this, so it is placed in your home folder. You’re saying, “Grab a copy of that app, and save it here.”

Move

Sometime’s you don’t need a copy of an item, you just want to move an item from point a to point b. That’s where mv, the move command comes in. Much like the cp command, you simply type mv, space, the file you want to move, space, and where you want to move it to. So,

mv Terminal2.app zfolder/Terminal2.app

Will move the Terminal2 from your home folder to the folder. And to move it back?

mv zfolder/Terminal2.app Terminal2.app

Remember, by not specifying a path in the second part, you are saying, “Move it to where I am”. And since you are in your home folder, it’s moving it to your home folder. If you wanted to move it from folder to zfolder2, you could have typed

mv zfolder/Terminal2.app zfolder2/Terminal2.app

But if you have been following along with every step, that wouldn’t have worked, because it’s not in folder any more, you moved it earlier. But you get the drift.

Rename

You can also use the move command to rename a file. Simply move the file to where it already is, but with a new name. Eh? I know, it was weird to me at first. Let’s try it. Right now, you should still have a file, in your home folder, named file.txt. If you don’t, make one:

touch file.txt

Okay. Now, you don’t want a copy, you don’t want to move it, you simply want to give it a new name:

mv file.txt newfile.txt

That’s it. Move, space, old file name, space, new file name. It works for folders too, and you don’t need the -R option.

mv zfolder znewfolder

Remove

Okay, now the scary one! rm is the remove command. Now before we try this one, please remember: there’s no trash in the command line, there’s no are you sure? and there’s no undo. You delete a file, its gone, daddy-o. So proceed with caution.

rm file.txt

That’s it, that will remove the file. If you want to remove a folder, and everything in it, recursively, we bring back the -R option:

rm -R znewfolder

A common prank for Unix admins is to tell news to type a command which will recursively remove everything from the root of the hard drive. Yeah, Unix admins can be jerks 🙂 It goes without saying you SHOULD NOT EVER type this, and that’s why Im not telling you what it is. You can Goole it. Or probably figure it out from what we’ve learned so far. I just wanted to point out, that when you’re in the command line, the gloves are off. Your computer will do anything you tell it to, even if it means destroying itself. So double-check your typing, the file you save could be your own.

I’ll leave things today with a funny cartoon that’s semi on-topic:

Advertisements

Author: alexkaloostian

I'm a video editor, motion graphics designer and Mac IT consultant in the Boston area.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s