Move and Copy Files and Folders with Bash

Cameron Nokes
InstructorCameron Nokes
Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 6 years ago

In this lesson we’ll learn how to move and rename files (mv) and copy (cp) them.

Instructor: [00:00] The move command, or MV command, allows us to move files and folders. In my folder here, I have a JS file, and then I have a source folder. The source folder is empty.

[00:11] Let's say I want to move my index.js into the source folder. I just pass the target of the move, and then the destination. You have to pass the complete file name to the destination.

[00:24] I could rename the file if I wanted here, so I do that. You see I just have a source folder in the root, and if I list out source, the index.js is in there now.

[00:32] For example, if you just wanted to rename a file, let's create a file here, and then let's say I want to make AJS, BJS. I just move it to the same folder, and just pass a different file name here as the second parameter.

[00:46] So I do that, then I have my BJS there. In this folder, I have the source directory. Let's say I wanted to rename that to lib. I could do that just like this. That renames the folder.

[00:57] Let's say I now wanted to move everything in lib into source. I'll recreate a source folder here, and we're going to do move lib.

[01:07] We'll do the asterisk so that it grabs all files and folders under this lib folder, and then I do source, like that. Let's list out lib. We see it's empty now, and then the index.js is now in source, like we want.

[01:19] To copy a file, we use the copy, or CP command. Let's set up a file here, create a readme.md file. Let's copy that and put a copy in the source folder.

[01:33] When we do this, we can rename it if we want. I want to keep the same name, but you do have to type out...Like with move, you have to type out the file name again.

[01:40] So we do that, we can see we still have a readme.md here in the root. If we list it out, we see it there, and it should have the same contents. It does, because we just had the hello in there.

[01:53] If we want to copy an entire folder and all of its sub-folders from one to another, we use the copy command. We pass the R flag, which standards for recursive.

[02:02] If I want to move everything in the source folder, such as source/asterisk, a wild card to mean everything, and then let's copy everything from the source folder to the lib folder.

[02:12] We do that. We should see it has those two files which are the same as are in the source folder.