Copy, Move, Delete on Command Line

Video loading...

  • 0:02
    Copy, Move, Delete on Command Line with Addison Berry
  • 0:05
    Welcome to the next video in the Command Line Basics series.
  • 0:07
    In the first video we looked at just sort of moving
  • 0:10
    around from the command line within a file structure.
  • 0:13
    And today, we're going to actually look a little bit more
  • 0:16
    at actually working with the files themselves,
  • 0:18
    so we're going to be doing copy, move, and delete, renaming,
  • 0:22
    and just sort of play around a little bit and figure out what
  • 0:24
    you can do with the files that are already there.
  • 0:28
    To start off here, real quick, I am in my terminal window at
  • 0:32
    the command line, and I just want to review
  • 0:35
    some stuff real quick from the first videos.
  • 0:37
    The first thing that we have is pwd, where I am,
  • 0:40
    and then I can actually move around.
  • 0:44
    So, change directory, cd.
  • 0:45
    I'm going to cd into my Drupal directory
  • 0:48
    and then ls to list the contents of this directory
  • 0:52
    so I can see what I'm working with.
  • 0:54
    The directory I want to work with today is the stuff
  • 0:57
    folder that's here.
  • 0:58
    But first, important, important command whenever doing any stuff
  • 1:02
    here is to learn how to make a copy of something.
  • 1:05
    So, instead of actually messing with this,
  • 1:06
    I'm going to do the cp command, which is for copy.
  • 1:09
    I'm going to copy my stuff folder to another folder,
  • 1:14
    and then just play with that, so if I mess everything up,
  • 1:16
    it's fine.
  • 1:17
    I'm not actually going to destroy any of my stuff.
  • 1:19
    So, I put the name of the folder.
  • 1:22
    I want to copy, and then I put the name of the new folder
  • 1:25
    after that, after space.
  • 1:27
    And you'll notice it didn't actually do it,
  • 1:30
    and it's giving me a little message here
  • 1:32
    because I kind of messed up.
  • 1:34
    So stuff is a directory, and whenever you're working
  • 1:36
    with directories, oftentimes, the command line
  • 1:38
    would like you to work recursively,
  • 1:40
    so you have to put a -r.
  • 1:42
    So, I'm going to do cp, then space, then dash,
  • 1:45
    and then r to say copy this recursively.
  • 1:47
    So not just the top thing, but all the stuff inside.
  • 1:50
    So now I do the same thing.
  • 1:52
    Put the name of my folder, stuff,
  • 1:54
    and then the name of the new folder that I am copying it to,
  • 1:57
    which is going to be more_stuff. And now that
  • 2:01
    time it worked.
  • 2:01
    And when I do my ls, you can see now I have a stuff
  • 2:04
    and a more_stuff folder.
  • 2:05
    And they have identical contents in them now.
  • 2:08
    So, copy is sanity.
  • 2:10
    So, most important thing to learn.
  • 2:13
    I'm going to go into more_stuff,
  • 2:15
    and we're going to sort of work in here.
  • 2:16
    This is my ls.
  • 2:18
    These are all the crazy, just random files that
  • 2:21
    I have in my more_stuff folder now.
  • 2:24
    First thing I'm going to do here to help organize
  • 2:27
    is actually make a new folder.
  • 2:29
    You do that by doing a mkdir, make directory.
  • 2:32
    So, m-k-d-i-r is saying make a folder for me,
  • 2:35
    and then give the folder a name.
  • 2:36
    So,I've just made a new folder call stats,
  • 2:40
    and so when I do my ls, you'll see I have stats.
  • 2:42
    If I do a ls -al to get more detail,
  • 2:47
    when you look at this, you can see it has the d,
  • 2:50
    if you remember from the first video,
  • 2:51
    which is telling me it's a directory rather than a file.
  • 2:55
    So, I created my new folder.
  • 2:57
    It's that simple.
  • 2:58
    Done.
  • 2:59
    So now I can actually, you know, get some stuff into
  • 3:02
    that to help organize this crazy.
  • 3:05
    So, I can go ahead and copy a file,
  • 3:08
    and I'm going to copy one of these stats.
  • 3:11
    Drupal_January, there we go.
  • 3:15
    I'm going to copy that into my stats folder.
  • 3:17
    So, filename and the folder I want it to go.
  • 3:21
    And when I do the ls now, you'll notice that the
  • 3:25
    Drupal_January file is still in my
  • 3:28
    more_stuff folder.
  • 3:29
    It's also, if I go into the stats folder,
  • 3:34
    it's in there as well.
  • 3:35
    So, I copied it, so it didn't remove it.
  • 3:37
    It just actually made a copy of it,
  • 3:39
    which isn't exactly what I wanted to clean up.
  • 3:42
    So if I go back up, I'm now in my more_stuff folder,
  • 3:46
    rather than stats. I want to get rid of this file from here,
  • 3:51
    since I've already copied it to the other location.
  • 3:53
    So, to get rid of stuff, you remove, which is rm.
  • 3:57
    So, if I want to remove a file, delete it,
  • 4:02
    it's going to go bye-bye, then I can just do my rm
  • 4:06
    command with the filename and boom, it's gone.
  • 4:10
    I do my ls, it's not here, but it is still in the stats
  • 4:14
    folder because I just copied it over there.
  • 4:16
    So now, that was sort of annoying to have to copy
  • 4:20
    and then remove, and there is a better way to
  • 4:24
    do that using the move command, which is mv.
  • 4:28
    So, instead of copying and removing,
  • 4:32
    I'm just going to move and do an mv for the other file,
  • 4:35
    which is drupal_downloads_january.
  • 4:38
    So, I will move that into my stats folder.
  • 4:45
    So, that's the folder.
  • 4:48
    Now I do the ls, that one's gone.
  • 4:50
    It actually removed it and put it into stats for me.
  • 4:54
    So, move, copy, remove; those are the three basic commands
  • 5:00
    we'll be looking at.
  • 5:00
    We'll play with them a little bit more.
  • 5:02
    Okay, so let's pop back up to the more_stuff folder here,
  • 5:08
    so I'm back up there, and the next thing I want to do is look
  • 5:12
    at moving a file up out of this directory.
  • 5:16
    I have this Drupal Usability thing,
  • 5:18
    and I actually have a usability folder that's
  • 5:20
    a sibling of more_stuff.
  • 5:22
    And just like we did when we were doing cd in
  • 5:24
    the first video, I can just use that dot-dot for moving
  • 5:28
    in my directory.
  • 5:29
    So I can move the usability file that's here.
  • 5:32
    dot-dot, go up one directory, slash,
  • 5:35
    put it into the usability folder.
  • 5:38
    And I can go ahead and do that and that will
  • 5:42
    move me up an ounce.
  • 5:44
    So, the same things that you're using for cd to move around
  • 5:47
    directories, you can also use with things like the move
  • 5:49
    and copy commands, as well.
  • 5:50
    And you can see it's now gone; took it out of the directory
  • 5:54
    for me, which is awesome.
  • 5:56
    So we're cleaning up.
  • 5:59
    Keep the cleanup motion going, here.
  • 6:01
    What I want to do now is move the stats folder
  • 6:03
    up and out of here, because it doesn't need
  • 6:04
    to stay in this stuff anymore.
  • 6:06
    So, I can use a move command with a folder, just like a file.
  • 6:10
    Put mv, the folder name, and then a dot-dot to move
  • 6:15
    up a directory.
  • 6:16
    Notice, I'm not using a -r for recursive like I needed
  • 6:20
    to with copy and remove.
  • 6:22
    Move works fine without that.
  • 6:24
    It's just going to move the whole folder,
  • 6:25
    so you see it's gone from here.
  • 6:28
    If I move back up, you can now see that stats is a sibling
  • 6:32
    of more_stuff, so move works the same either way.
  • 6:35
    Let's go back into more_stuff here and keep cleaning up.
  • 6:41
    The next thing that I want to do,
  • 6:43
    is I'm going to create a junk folder of just real junk.
  • 6:46
    So, let's do mkdir, m-k-d-i-r.
  • 6:50
    Name the folder as junk because it's just random stuff.
  • 6:54
    I have my new folder here now.
  • 6:57
    And let's move some stuff in there.
  • 6:59
    I've got some stuff that's just old,
  • 7:01
    like the cheat sheet from 4.7, I don't
  • 7:04
    really need that anymore.
  • 7:06
    I'm going to move that cheat sheet into my new junk folder.
  • 7:09
    And then, let's see,
  • 7:12
    what else do we have?
  • 7:13
    We have,
  • 7:15
    I have some stuff that's,
  • 7:16
    let me clear this up so you can see it again.
  • 7:18
    OK, at the top, we have 2003 and 2005 files.
  • 7:22
    I don't need those either, so I'm going to move those,
  • 7:25
    but I can use a wildcard so they both begin with 2000,
  • 7:28
    and then the rest, so I can use a wildcard asterisk,
  • 7:32
    and all files that start with 200 will get moved.
  • 7:37
    So, when I do that, you can see now they are
  • 7:39
    both gone at the same time.
  • 7:41
    Go into junk, they're both here.
  • 7:44
    You can also use that asterisk as a wildcard either
  • 7:46
    at the beginning or at the end, which comes in
  • 7:49
    handy if you have a lot of stuff, especially
  • 7:51
    like, say, filenames.
  • 7:52
    If I wanted star.jpegs.
  • 7:56
    Alright, let's go back up here to more_stuff.
  • 7:57
    There's my junk folder.
  • 7:59
    I don't really need a junk folder.
  • 8:01
    It's trash, so let's just actually remove the whole thing.
  • 8:04
    So, I'm going to remove junk, and again,
  • 8:08
    I need this recursive thing I mentioned before with copy.
  • 8:13
    Since it's a directory, it wants to be specifically
  • 8:18
    told to do this recursively so that I know what I'm doing.
  • 8:21
    So, rmr junk, and it is gone.
  • 8:27
    While we've been moving stuff around,
  • 8:29
    one thing we haven't looked at is how to
  • 8:30
    actually rename a file.
  • 8:32
    Let's see how you actually just,
  • 8:35
    I just want to rename it.
  • 8:36
    I don't actually want to move it or copy it anywhere.
  • 8:38
    And so, I have this file up here.
  • 8:41
    It's got a lot of spaces and dashes.
  • 8:42
    It's just sort of a funky name.
  • 8:43
    I want it shorter and more compact.
  • 8:45
    Now, you might think that there's a rename command,
  • 8:49
    like rn or something, and that's not true.
  • 8:52
    What we're going to do is actually use the move command
  • 8:54
    to move a file from one name to another.
  • 8:57
    So, I'm going to move this file.
  • 8:59
    And you can see these spaces, like on the command line,
  • 9:02
    you have to do a backslash and a space.
  • 9:04
    It's just sort of a mess on the command line,
  • 9:06
    so I want to get rid of that.
  • 9:06
    I'm going to move that file to this file.
  • 9:12
    It's essentially just going to rename it.
  • 9:14
    But this will be a nice, shorter name that'll be easier
  • 9:19
    for me to type out.
  • 9:21
    So now when I do my ls, you can see that totally
  • 9:25
    got changed from that long, big thing with lots
  • 9:28
    of spaces in it into just this little filename.
  • 9:30
    That's the simple thing with rename.
  • 9:32
    It's a little confusing because you're moving a file,
  • 9:34
    and all you're really doing is changing the name of it.
  • 9:37
    But handy.
  • 9:38
    Works really quickly and easily.
  • 9:40
    Good to know.
  • 9:43
    OK, quick review of the commands that
  • 9:44
    we have played with.
  • 9:45
    cp is copy. If you're working with a folder,
  • 9:49
    then you need to do the -r for recursive.
  • 9:52
    And then we have mv for actually moving
  • 9:55
    rather than copying.
  • 9:56
    So that will actually completely switch the location.
  • 9:59
    And then we have remove, which is deletion.
  • 10:02
    So, rm, and then again, if you have a folder,
  • 10:05
    you have to do that recursively with that -r.
  • 10:08
    mkdir simply gives you a new folder.
  • 10:11
    It's pretty straightforward.
  • 10:14
    And that's pretty much the commands we've looked at.
  • 10:17
    We have cd. We have ls from the earlier ones.
  • 10:22
    And I'm going to clean myself up here,
  • 10:24
    so what I want to do is go ahead and remove
  • 10:29
    that stats folder I made, and I'm going to remove
  • 10:31
    the more_stuff folder I made so that I can just
  • 10:35
    return back to my regular same stuff folder.
  • 10:38
    So, that's it for this video, and next time we'll be looking
  • 10:42
    at playing around with ownership and permissions
  • 10:44
    of files and such wonderful things as that.
  • 10:48
    Thanks a lot and see you next time.
Loading ...

Copy, Move, Delete on Command Line

Loading...

The second video in the command line basics series, this one covers common commands for dealing with files; copying, moving and deleting them. We walk through examples for the following commands: cp, mv, and rm.

Just a a fair warning that I say the word "stuff" way too many times in this video. Please just bear with it.

Note: this video was originally released September 8, 2009 on Lullabot.com.

Additional resources:
There are no resources for this video. If you believe there should be, please contact us.