Move and Copy Files and Folders with Bash

Cameron Nokes
InstructorCameron Nokes
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 5 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.

Vitaly K.
Vitaly K.
~ 5 years ago

I'm not sure what's the default behaviour on MacOS for the mv / cp wildcards, but on the Ubuntu (I believe Linux systems in general) and Git bash on Windows (that comes with mv and cp tools) dot files (.editorconfig, .stylelint, etc.) will not be copied with cp lib/* src command as well as they won't be moved with the same mv command, at least by default.

You can enable dot files to be matched against wildcard * with the following command: shopt -s dotglob - it will turn the dotglob option allowing * to grab the dot files and the regular files at the same time.

Shaul Fridman
Shaul Fridman
~ 5 years ago

You said that you must put the full name of the target file with mv\cp. On my mac you don't. you can just 'mv some.txt src' and it will move it to 'src' folder.

Cameron Nokes
Cameron Nokesinstructor
~ 5 years ago

Thanks Shaul, you're right, sorry for the inaccuracy -- that's like my greatest fear 😱. I'll look into fixing it.

Shaul Fridman
Shaul Fridman
~ 5 years ago

Anyway, it's a great course man :-) I wanted for a long time to get to know bash, and its a clear and simple way to start for me.

Cameron Nokes
Cameron Nokesinstructor
~ 5 years ago

Thanks Shaul! ❤️

Andy Hu
Andy Hu
~ 5 years ago

Thanks for the great course! One thing I found a bit confusing is the cp -R src/* lib/ part. Looks like we don't need the -R flag to copy every file from src to lib, and if we do something like cp -R src lib then we also copied all the files from src to lib, so I think it may be clearer if you provided both two sample commands here.

Markdown supported.
Become a member to join the discussionEnroll Today