Skip to content

Lesson 3: Creating and organizing folders

So far, you've learnt how to interact with files and folders that already exist. Now, it's time to learn how to create some of your own.

Creating new folders

To make a new folder, use the mkdir (think make directory) command. To create a folder called Hogwarts, run:

> mkdir Hogwarts

As usual, the command line does not bother to tell us that we successfully created a new folder. To verify, run ls in your Home folder:

> ls
Documents  Downloads  Music  Pictures  Hogwarts

So, apart from the usual suspects, you have a brand new folder called Hogwarts. Success!

Organizing your life

The most important use of creating new folders is, of course, to organize your other files and folders. Let's see an example of nested folders in action:

> mkdir Hogwarts/Houses
> cd Hogwarts/Houses
> mkdir Gryffindor Slytherin Ravenclaw Hufflepuff Dumbledore
> ls
Dumbledore Gryffindor Hufflepuff Ravenclaw Slytherin
  • First, the mkdir command creates a folder called Houses inside the Hogwarts folder.
  • Next, the cd command navigates to the Houses folder that you just created.
  • Once inside the Houses folder, we use the mkdir command with multiple arguments--Gryffindor, Hufflepuff, Ravenclaw, Dumbledore, and Slytherin--each of them separated by a space.
  • Finally, ls lists the new folders.

Deleting folders

From time to time, you'll make a mistake and create a folder that shouldn't be there. In our example, Dumbledore does not really qualify as a house, so let's get rid of that folder with the rmdir (think remove directory) command and confirm the deletion with ls:

> rmdir Dumbledore
> ls
Gryffindor Hufflepuff Ravenclaw Slytherin

It's worth noting that the rmdir command only deletes empty folders. If you try to delete a non-empty folder, it throws out an error:

> rmdir ~/Hogwarts
rmdir: failed to remove 'Hogwarts': Directory not empty

For the sake of this exercise, knowing how to delete empty directories is all you need. You'll learn how to delete files and non-empty directories in later chapters.

Exercises

  1. Go to the Houses directory and run mkdir gryffindor slytherin ravenclaw hufflepuff, followed by the ls command to see what happened. Is the result what you expected it to be? Hint: pay attention to the uppercase and lowercase characters.

  2. Notice the order of the files and folders whenever you run the ls command. Try to figure out how the various files and folders are sorted.

Doubts and explanations

Linux: Why do I see all the house names twice after doing the first exercise?

Recall that on Linux, file and folder names are case-sensitive -- Gryffindor, gryffindor, GRYFFINDOR, gRyFfInDoR, and any other combination of uppercase and lowercase letters are different files.

macOS: All I get when doing Exercise 1 is "mkdir: gryffindor: File exists" etc.

Unlike Linux, filenames on macOS are case-insensitive -- the system does not consider Gryffindor, gryffindor, GRYFFINDOR, gRyFfInDoR, and any other combination of uppercase and lowercase letters as different files.

For the sake of simplicity and to avoid confusion, treat macOS filenames as case-sensitive, too. That's because the eventual goal of this course is to be able to use Linux servers one day.

Note: Even though filenames are case-insensitive on macOS, commands are not -- ls, mkdir, etc. work, but Ls, MKDIR and LS don't.