Linux mkdir Command: Directory Creating

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we'll explore the mkdir command in Linux, a fundamental tool for creating and organizing directories. We'll simulate the process of setting up a digital garden - a personal knowledge management system - to learn how to effectively use mkdir in various scenarios. This hands-on experience will help you understand directory creation, nested structures, and permission settings in Linux, even if you're completely new to the command line.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux/BasicSystemCommandsGroup -.-> linux/tree("`Directory Tree Display`") linux/PackagesandSoftwaresGroup -.-> linux/apt("`Package Handling`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/FileandDirectoryManagementGroup -.-> linux/pwd("`Directory Displaying`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") subgraph Lab Skills linux/tree -.-> lab-209739{{"`Linux mkdir Command: Directory Creating`"}} linux/apt -.-> lab-209739{{"`Linux mkdir Command: Directory Creating`"}} linux/cd -.-> lab-209739{{"`Linux mkdir Command: Directory Creating`"}} linux/pwd -.-> lab-209739{{"`Linux mkdir Command: Directory Creating`"}} linux/mkdir -.-> lab-209739{{"`Linux mkdir Command: Directory Creating`"}} linux/ls -.-> lab-209739{{"`Linux mkdir Command: Directory Creating`"}} end

Creating Your Digital Garden Root

Let's start by creating the root directory for your digital garden.

First, open your terminal. You'll see a prompt that looks something like this:

labex:project/$

This indicates that you're in the /home/labex/project directory, which is perfect for our lab.

Now, let's create a directory named digital_garden:

mkdir digital_garden

Here, mkdir stands for "make directory". This command creates a new folder named digital_garden in your current location.

To verify the directory has been created, we'll use the ls command, which lists the contents of a directory:

ls

You should see digital_garden in the output. If you don't see it, don't worry! Just try the mkdir command again.

Adding Main Sections

In a digital garden, you might want to organize your thoughts into main categories. Let's create directories for different types of content.

We'll create three directories inside our digital_garden: 'notes', 'projects', and 'resources'. You can do this with three separate commands:

mkdir ~/project/digital_garden/notes
mkdir ~/project/digital_garden/projects
mkdir ~/project/digital_garden/resources

Here, ~/project/digital_garden/ is the full path to our digital garden. The ~ is a shortcut that means "home directory".

To see the new structure, use the ls command with the path to your digital garden:

ls ~/project/digital_garden

You should see the three new directories listed: notes, projects, and resources.

If you're curious about what these directories might be used for:

  • notes could store quick thoughts or daily reflections
  • projects might contain longer-term work or studies
  • resources could be for storing reference materials

Creating Nested Directories

Often, you'll want to create directories inside other directories, creating a nested structure. The -p option allows you to create parent directories as needed, which is very handy for deep structures.

Let's create a nested structure for a hypothetical web app project:

mkdir -p ~/project/digital_garden/projects/web_app/src/components

This command does a lot at once:

  • It creates a web_app folder inside projects
  • Inside web_app, it creates an src folder
  • Finally, inside src, it creates a components folder

The -p option (think "parents") tells mkdir to create any missing parent directories along the way. Without -p, you'd get an error if any part of the path didn't exist yet.

To see this new structure, use the ls command with the -R option, which shows the contents recursively:

ls -R ~/project/digital_garden/projects/web_app

You should see the nested directory structure displayed.

Setting Directory Permissions

When creating directories, you can set specific permissions. This is useful for controlling who can access, modify, or execute files within the directory.

Let's create a directory named 'private' with restricted permissions:

mkdir -m 700 ~/project/digital_garden/private

Here's what this does:

  • mkdir creates the directory
  • -m 700 sets the permissions
    • 7: Read, write, and execute for the owner
    • 0: No permissions for group
    • 0: No permissions for others

In other words, only you (the owner) can access this directory.

To check the permissions, use:

ls -ld ~/project/digital_garden/private

The output should look like this:

drwx------ 2 labex labex 6 Aug  7 18:40 /home/labex/project/digital_garden/private

Here, drwx------ means:

  • d: It's a directory
  • rwx: You (the owner) have read, write, and execute permissions
  • ------: Neither your group nor others have any permissions

Using Verbose Mode

The verbose mode can be helpful when creating multiple directories, as it provides feedback on each creation. This is especially useful when you're creating many directories at once and want to ensure they're all created correctly.

Let's create several directories in verbose mode:

mkdir -v ~/project/digital_garden/resources/books ~/project/digital_garden/resources/articles ~/project/digital_garden/resources/videos

The -v option stands for "verbose". It tells mkdir to print a message for each directory it creates.

You should see output similar to this:

mkdir: created directory '/home/labex/project/digital_garden/resources/books'
mkdir: created directory '/home/labex/project/digital_garden/resources/articles'
mkdir: created directory '/home/labex/project/digital_garden/resources/videos'

This feedback can be very helpful in complex scripts or when troubleshooting.

Combining Options

You can combine multiple options with mkdir. This allows you to create complex structures with specific permissions and verbose output all in one command.

Let's create a nested structure for a hypothetical research project with restricted permissions:

mkdir -pvm 750 ~/project/digital_garden/projects/research_paper/drafts ~/project/digital_garden/projects/research_paper/references

Let's break this down:

  • -p: Create parent directories as needed
  • -v: Verbose mode, print a message for each created directory
  • -m 750: Set permissions (owner: full access, group: read and execute, others: no access)

This command creates two directories (drafts and references) inside research_paper, which itself is created inside projects if it doesn't already exist.

To check the structure and permissions:

ls -lR ~/project/digital_garden/projects/research_paper

You should see the nested directories with the specified permissions (drwxr-x---).

Visualizing Your Digital Garden with the tree Command

Now that we've created our digital garden structure, let's use the tree command to visualize it. The tree command displays the directory structure in a tree-like format, which is both informative and visually appealing.

Now, let's use tree to view our digital garden structure:

tree ~/project/digital_garden

You should see output similar to this:

/home/labex/project/digital_garden
|-- notes
|-- private
|-- projects
|   |-- research_paper
|   |   |-- drafts
|   |   `-- references
|   `-- web_app
|       `-- src
|           `-- components
`-- resources
    |-- articles
    |-- books
    `-- videos

13 directories, 0 files

This tree structure gives us a clear overview of our digital garden. We can see all the directories we've created, including the nested structures.

If you want to see more details, including the permissions we set, you can use the -p option with tree:

tree -p ~/project/digital_garden

This will show the permissions for each directory, like this:

[drwxrwxr-x]  /home/labex/project/digital_garden
|-- [drwxrwxr-x]  notes
|-- [drwx------]  private
|-- [drwxrwxr-x]  projects
|   |-- [drwxrwxr-x]  research_paper
|   |   |-- [drwxr-x---]  drafts
|   |   `-- [drwxr-x---]  references
|   `-- [drwxrwxr-x]  web_app
|       `-- [drwxrwxr-x]  src
|           `-- [drwxrwxr-x]  components
`-- [drwxrwxr-x]  resources
    |-- [drwxrwxr-x]  articles
    |-- [drwxrwxr-x]  books
    `-- [drwxrwxr-x]  videos

13 directories, 0 files

This visual representation is a great way to verify that we've created all the directories we intended, with the correct structure and permissions.

This step provides a satisfying conclusion to our lab, allowing us to see the entire structure we've built. The tree command is not only useful for this exercise but is a valuable tool for navigating and understanding directory structures in your future Linux endeavors.

Summary

In this lab, we explored the versatility of the mkdir command in Linux by simulating the creation of a digital garden. We learned how to:

  1. Create single directories
  2. Create multiple directories at once
  3. Create nested directory structures using the -p option
  4. Set specific permissions when creating directories with the -m option
  5. Use verbose mode -v for detailed feedback

We also saw how these options can be combined for more complex operations.

While not covered in our exercises, mkdir has a few additional parameters that can be useful in specific situations:

  • -Z: Set SELinux security context of each created directory to the default type
  • --context[=CTX]: Like -Z, or if CTX is specified, set the SELinux or SMACK security context to CTX
  • --help: Display help message and exit
  • --version: Output version information and exit

These skills are fundamental for efficient file system organization in Linux environments. Remember, while we focused on a digital garden scenario, these techniques apply to any situation where you need to create and manage directory structures in Linux. As you continue your Linux journey, you'll find mkdir to be an indispensable tool for organizing your files and projects.

Resources

Other Linux Tutorials you may like