Bummer! This is just a preview. You need to be signed in with a Basic or Pro account to view the entire video.

Preview

Start a free Basic trial
to watch this video

Sign up for Treehouse

Moving and Deleting Files

11:30

This video will show you the different ways to move, copy, rename and delete files using the console.

  • 0:00

    And let's learn a little bit on how to move files and copy files.

  • 0:04

    Now if we take a look at ls here, we can see our file called hello.txt.

  • 0:09

    Now if I wanted to change this to be called hi.text

  • 0:13

    that would be something we normally call renaming.

  • 0:16

    But we can think of it another way instead of renaming hello.txt to something like hi.txt,

  • 0:23

    we can think of it as moving the file from one location to another.

  • 0:27

    Currently, it's in hello.txt, we want to move that file to hi.txt

  • 0:32

    and that's where the move command comes in handy.

  • 0:35

    Move is shortened to mv and it takes 2 arguments.

  • 0:39

    It takes from where we want to move something to where we want to move it.

  • 0:44

    So in our case we'll type in hello.txt and I can use tab completion for that

  • 0:49

    but our second argument that we need to move it to a place.

  • 0:52

    I can't use our text completion because we're moving it to a place that does not exist.

  • 0:56

    So I want to move it to hi.txt.

  • 0:59

    So I'm saying move hello.txt to hi.txt which is equivalent to renaming it.

  • 1:05

    And so if I hit enter and now we type in ls, we'll know see is hi.txt.

  • 1:13

    And if I type in less hi.txt, we'll see it's the exact same file and I just hit Q to exit ls again.

  • 1:20

    Now that's a simple case if we move a file within a directory

  • 1:23

    it's really just sort of renaming but we can actually move that one directory to another.

  • 1:28

    So for instance, if we want to move hi.txt into our documents directory,

  • 1:33

    well I can do this into documents.

  • 1:36

    Now since documents is a directory, I can just pass that as my second argument

  • 1:41

    and what it's going to do is it's going to move the hi.txt from my current directory

  • 1:44

    into the documents directory but with the same filename.

  • 1:51

    So if I execute this and now I type ls, we are going to see we only have our documents directory,

  • 1:57

    but if I do ls and passing documents as the directory that I want to list.

  • 2:02

    Again, I'm not moving into documents, I am just listing it from where I currently am.

  • 2:08

    Now we see the documents directory have hi.txt and how to go home.txt.

  • 2:13

    And I can move it back by doing the opposite.

  • 2:16

    So if I move documents, I can tab complete hi.txt again tab completing to our current directory

  • 2:24

    which I could do like that with a tilde or we saw how .. represents the directory above us

  • 2:33

    but there's another special directory called . which is our current directory.

  • 2:39

    So by simply typing . what I'm saying is I want to move

  • 2:42

    the document/hi.txt from its current location into my current directory

  • 2:48

    and . represents my current directory which in our case right now is our home directory.

  • 2:54

    So by doing that, I move it back into our current directory and we can see hi.txt is back.

  • 2:59

    Now remember before I use hi.txt and move it into documents without getting it a full filename

  • 3:05

    but if I gave it document/hello.txt, it would not only move the file into the documents directory

  • 3:12

    but also rename it to hello.txt within that directory.

  • 3:15

    So I type ls, we can see it's no longer in our current directory

  • 3:19

    and if I type in ls documents we can see it now exists in our documents directory as hello.txt.

  • 3:25

    So we've not only moved it from one location to another but we've also renamed it.

  • 3:29

    So that's how move can be a very powerful tool and then clear once again

  • 3:34

    and move will not only work for files but also directories as well.

  • 3:37

    So if I type in move documents and I want to rename it to docs, I can just do that.

  • 3:43

    And now if I type in ls, we can see that folder is now called docs

  • 3:47

    and we move it back to documents so by just typing in move docs to documents,

  • 3:52

    I can rename it back and again I can move document into another folder

  • 3:56

    and all the files and directories and site of that directory will be moved along with it.

  • 4:01

    Now we move our hello.txt back, so it's in our documents directory now.

  • 4:06

    It's in hello.txt and I want to move it back to our current directory.

  • 4:11

    So now were back to where we started and what if I want to make a copy of hello.txt

  • 4:16

    Well, I can use the copy command.

  • 4:18

    Let's clear out our console once again by typing clear.

  • 4:23

    And copy is shortened by cp for copy and copy works a lot like move

  • 4:28

    except it leaves the original one in place.

  • 4:31

    And if I wanted to copy hello.txt to hi.txt that's what I can do.

  • 4:37

    It's a lot like move but it leaves the original in place, so I hit it, type in ls.

  • 4:41

    We can see there's hello.txt so if I type in less hello.txt, we can see is that file.

  • 4:48

    And if I type in less hi.txt, you'll see it's the same file.

  • 4:54

    So I've just created a copy of it.

  • 4:56

    So if I wanted to copy a directory, however, it's a little bit different. Let's see what happens.

  • 5:00

    Now if I want to copy my documents directory into a docs directory,

  • 5:04

    I can try the same thing--documents to docs but I have a problem.

  • 5:10

    I can't just copy a directory--actually I have to pass this special option called -r.

  • 5:17

    This stands for recursive which means it will copy everything recursively.

  • 5:21

    By default, copy will only work on files but we need to do a directory and all the files within it

  • 5:26

    by typing cp -r, the source directory and the new directory you want to create from it

  • 5:34

    it'll recursively create all the files that exist in documents inside of a new folder call docs.

  • 5:40

    So by typing ls because there's docs and documents,

  • 5:43

    and if I type in ls docs, you can see it retains a copy how to go home.txt.

  • 5:49

    Again, that's a copy of the how to go home.txt that exists in documents.

  • 5:55

    There are two different files so it's recursively created a copy

  • 5:59

    of not only the directory but every file and every sub directory in that directory.

  • 6:04

    So that's why we need to do -r if we're copying directories from one place to another.

  • 6:09

    So if I want to remove the hi.txt, I would use the rn command which removes it.

  • 6:14

    So I'm going to clear it and will do an ls to see exactly what we have now.

  • 6:19

    And to remove the hi.txt which is a copy of our hello so that we don't really need it.

  • 6:23

    Type in rm for remove.

  • 6:26

    Now if I wanted to remove hi.txt, that's all I need to do.

  • 6:31

    Now you need to be very careful with the rm command

  • 6:33

    because it's not like in our desktop operating systems where we have a recycle bin

  • 6:37

    and we can go and undo it.

  • 6:39

    It's really just gone. There is not really an easy way to bring back from an rm.

  • 6:43

    So be very, very cautious when deleting files and especially when deleting directories.

  • 6:49

    Be careful not to hit enter too soon and maybe delete something you really needed.

  • 6:54

    So I've reviewed my command.

  • 6:56

    I'm sure I want to get rid of hi.txt and now I've done it.

  • 7:00

    There is no confirmation or anything so you can see how it could be kind of dangerous.

  • 7:05

    But if type in ls, we can see we're now down to hello.txt.

  • 7:09

    So what if I want to remove our docs. We'll let's try it. Let's clear out.

  • 7:15

    Let's type in ls to see what's going on. We have docs and documents.

  • 7:18

    I want to get rid of docs because it's a copy that we don't need to do rm docs.

  • 7:24

    But we've got a problem. We cannot remove docs because it is a directory.

  • 7:29

    Remember how in copy we couldn't copy it because of the directory,

  • 7:32

    the same thing works for rm.

  • 7:35

    Again, removing a directory could be way, way more dangerous than just removing a single file

  • 7:40

    because it's not only going to remove the directory but everything within it.

  • 7:44

    So again we need to signify that we are okay with recursively removing everything

  • 7:49

    not only in the docs directory but any directories inside of a docs directory.

  • 7:55

    So much like we did for copy, we need to pass through the -r flag.

  • 7:59

    So now we can remove the docs directory. Type in os and it's gone.

  • 8:05

    So what if we want to make a directory.

  • 8:07

    We have our documents directory and let's say we want to create a pictures directory.

  • 8:13

    We'll be using this command called make directory and that shortened to mkdir..

  • 8:19

    The mk we could think of as make and dir stands for directory mkdir

  • 8:25

    and we'll just give it the path that we want to do.

  • 8:28

    In our case, we'll say mkdir pictures.

  • 8:32

    Now we type ls. We can see there's now a new folder called pictures.

  • 8:36

    We can see that pictures is empty but we could place files in it.

  • 8:40

    Now say I want to create a directory inside of our documents directory

  • 8:43

    and maybe even a directory inside of that.

  • 8:46

    So let's say I want to have documents/notes/console.

  • 8:51

    I want to have that level of directory.

  • 8:53

    Now you might think, we could that by typing mkdir documents/notes/console.

  • 9:02

    So I want to create notes on my console within my documents.

  • 9:06

    Now the reason this won't work is because even though documents exists,

  • 9:12

    the notes directory inside of it does not exist so we can't create the console directory inside of that.

  • 9:18

    Now, there's two different things we can do.

  • 9:19

    We could mkdir documents/notes

  • 9:26

    and then mkdir documents/notes/console and that'll work.

  • 9:31

    But I'm going to show you another neat tip

  • 9:33

    for creating nested directories when you need them like that.

  • 9:38

    So now we have them, I'm going to ahead and remove

  • 9:40

    the notes directory by doing rm -r documents/notes.

  • 9:45

    Now if we take a look at documents, we're back to our beginning state.

  • 9:51

    If we mkdir -p, the -p option will allows us to create any level deep documents.

  • 9:59

    So we can see, say you wanted to create inside of the documents directory

  • 10:02

    which does exist--it happens to exist.

  • 10:05

    We're going to create notes directory and inside of that, we'll say console

  • 10:08

    and even inside of that we can say we can say part 1.

  • 10:13

    And it's going to go ahead and created all those levels deep.

  • 10:17

    So if we take a look at ls, we can see we have a documents directory.

  • 10:19

    We can go into documents--the ls, we can see there's now a notes directory.

  • 10:23

    We can go into that by typing notes. We ls that.

  • 10:28

    We can see we have a console directory.

  • 10:29

    We can go into that and if we can just continue tabbing through

  • 10:33

    we can see that there's even a part 1 directory.

  • 10:36

    So we see our prompt has gotten pretty long, let me clear it out.

  • 10:39

    And if we type in pwd, we can see our current location now is in our

  • 10:43

    home/treehouse/documents/notes/console/part1.

  • 10:49

    If you want to get home, let's use the .. to get home.

  • 10:53

    So we can do cd .. which would place us in the console directory/..

  • 10:59

    which would now place us in the notes directory/..

  • 11:02

    which would now place us in the documents directory /..

  • 11:06

    which would place us in the home directory.

  • 11:08

    Each.. takes us one level up.

  • 11:12

    And so now, if I calculate that right, we should end up in our home directory.

  • 11:15

    Bam! We did.

  • 11:17

    So there you are. You can now move around the computer freely.

  • 11:20

    You can add and edit and delete files in directories.

  • 11:23

    You now know enough console to be dangerous.

  • 11:26

    Next, we'll learn a little bit more about how the operation system works.

Show full transcript