Introduction to Bash, VIM & Regex

Moving Files and Creating Directories

Introduction to Bash, VIM & Regex

Check out a free preview of the full Introduction to Bash, VIM & Regex course

The "Moving Files and Creating Directories" Lesson is part of the full, Introduction to Bash, VIM & Regex course featured in this preview video. Here's what you'd learn in this lesson:

To move files, James introduces the mv command. Afterward, James shows how to create a director with mkdir command and how to use tab to auto-complete directories.

Preview
Close

Transcript from the "Moving Files and Creating Directories" Lesson

[00:00:00]
>> James Halliday: Okay, so a related command to the copy command is the move command, which is mv. You can use this to rename commands which is kind of the same idea as moving files. You can also use it to overwrite files. So, if I have a file, maybe I want to move message.txt, I wanna rename it to be something else, like msg.txt.

[00:00:31]
>> James Halliday: Now I can cat it out, and list the files. The original one is gone, and now it's in a new place. Also, if I wanna, for example, override an existing file, I can do that. So if I wanna move msg.txt, which I should change, put some exclamation marks at the end.

[00:00:54]
>> James Halliday: So now, I can move msg.txt over top of boop.txt. And now, if I kept that file, it has the contents of msg.txt, which no longer exists.
>> James Halliday: So you can do the same thing with directories as you can with files. So if you wanna rename a directory, for example, if I were to move the directory called cool into something else called good, then now there's a directory called good.

[00:01:36]
>> James Halliday: Because of how directories work, you don't ever have to think about like doing things recursively, like you would with the copy command because all that's it's doing is just changing the name of the directory.
>> James Halliday: The other fun commands that we can use is the command to make a new directory, which is called mkdir, which is often said mikdir.

[00:02:07]
So, if you wanna make a new directory, you can type mkdir, and then the name of the directory that you wanna create like wow.
>> James Halliday: If do ls, there's a new directory called wow.
>> James Halliday: The mkdir command takes a really nifty argument called -p. And so the -p command says, if this directory name already exists then just go ahead and use it.

[00:02:36]
But if it doesn't exist, then create it. So you can do something with mkdir -p, here I can show you. So if I try to make a new directory called a/b/c/d, then it's not going to work, because there's no directory called a, and there's no directory inside of a called b, and there's no directory inside of b called c.

[00:02:57]
So that's not gonna work, but you can pass in this flag called -p. And then I just go ahead, then it makes this whole nested directory structure for you.
>> James Halliday: So it's a handy shortcut so that you don't have to do a bunch of mkdir commands.
>> James Halliday: Another nice thing about the mkdir command is, if some of those directories already exist, but not all of them, or if all of them exist, then it succeeds no matter what.

[00:03:26]
So here, I already have a directory called wow, but I can mkdir -p wow/great/nifty. And wow is a directory that already exists, but great and nifty don't already exist, so the mkdir command will create those directories.
>> James Halliday: Check and see, wow, great, whoops, nifty.
>> James Halliday: Another important thing as your exploring your system is bash has this very useful feature called tab completion.

[00:04:05]
So if you start typing the name of a file and then hit the tab key, so for example, if I start typing cat b-e-e and then I hit tab, then it bashes clever enough to complete the rest of what that would be.
>> James Halliday: So, bash only needs enough information to uniquely identify the file before you hit tab.

[00:04:31]
And if it doesn't have enough information, and I hit tab, so if I just type b and I hit tab, it doesn't do anything because that's still ambiguous. But you can hit tab twice.
>> James Halliday: And it lists out all of the options which is pretty handy sometimes.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now