Join Scott Simpson for an in-depth discussion in this video Create and remove folders, part of Learn the Linux Command Line: The Basics.
- [Voiceover] Sometimes we'll need to create folders to organize files, and remove them. I've set up my screen here with the file browser on the right, so we can see the effects of what we'll do over here in the terminal. First I'll create a new folder here in my Exercise Files folder with mkdir, for make directory, and I'll give it a name, I'll call it new_folder, and I can see over here that folder appeared. Of course, I can see that with the ls-l command as well. Here's my new folder.
If we put a name after mkdir, it assumes we want to create the folder inside of the current working directory, in this case, Exercise Files. We can also specify a path outside the current folder, or a folder deeper inside the working folder. For example, I'll add a new folder inside our Departments folder for customer service. I'll write mkdir departments/customerservice, and we can see that that folder was created inside of the Departments folder, instead of inside the current working directory.
I can string together folder names too to make more than one folder at a time. Let's create a Documents folder, a Cases folder, and an Awards folder within the Customer Service Department folder. To do that, I'll write mkdir departments/customerservice/documents, and here I'm using tab completion for the first two parts. Then I'll put a space, and I'll write departments/customerservice/cases departments/ customerservice/awards, and I'll press Enter.
And we can see that those sub-folders were created. Let's say we also need a contracts folder for our legal department. Instead of creating a Legal folder and then creating a Contracts folder inside that, we can do it all at once using the -p option for mkdir. This option creates all needed parent folders, so in this case, it'll create the Legal folder for us, and then create the Contracts folder after that. I'll write mkdir-p departments/legal/contracts Keep an eye on the file browser on the side.
Notice that there's no Legal folder. I'll press Enter, and Legal appears down here with a Contracts folder inside. Pretty handy. We can also remove directories using the rmdir command. Let's go remove that Contracts folder that we just created. I'll write rmdir departments/legal/contracts, and I can see it's disappeared. Let's do that again with the Legal Department folder. rmdir departments/legal I'll clear the screen.
One thing to keep in mind about removing folders is that in order to remove a folder, it has to be empty. I can verify that by trying to remove the Customer Service folder. I'll write rmdir departments/customerservice/ and I get an error. That means it's a little tedious to remove a large folder structure with rmdir, but we'll see another way of doing that soon.
This course will establish the foundation for more advanced Linux topics. Find other Linux training courses here.
- What is the Linux command line?
- Writing Linux commands at the prompt
- Finding help for Linux commands
- Editing files and folders
- Configuring user roles and file permissions
- Using pipes to connect commands
- Peeking at files
- Searching and editing text
- Finding disk and system information
- Installing and updating software