linux how to create a directory

How to Create a Directory in Linux

By Robert Clark
AD

In the realm of Linux, creating a directory is not only essential but also incredibly simple and powerful. Understanding this basic task is akin to building the foundation of your Linux learning journey. This article will guide you through the process, walking you step-by-step, from a beginner's perspective to a pro.

Imagine you're organizing your bookshelf. You'd have a main shelf, then sections for different categories, right? The same principle applies to Linux directories. You start with your main home directory, then you create subdirectories, or sections, for different purposes. Let's dive into creating directories in Linux.

Creating Your First Directory

Let's start by creating a new directory named 'documents'. We'll use the Linux command 'mkdir' which stands for 'make directory'.

AD
Learn how to copy a directory on Linux
Learn how to copy a directory on Linux
Create directories on Linux using mkdir
Create directories on Linux using mkdir
How to Rename a File or Directory on Linux
How to Rename a File or Directory on Linux

If you're in your home directory, you can create 'documents' by simply typing:

mkdir documents

Creating Multiple Directories at Once

You can create multiple directories in one go too. Let's add a 'work' and 'personal' directory alongside 'documents'. Instead of typing each command separately, you can do it like this:

the block diagram shows different types of devices and their functions in which they are connected to each other
the block diagram shows different types of devices and their functions in which they are connected to each other
Learn Linux from Beginner to Advanced (Part-1) | Essential Terminal Commands Cheat Sheet
Learn Linux from Beginner to Advanced (Part-1) | Essential Terminal Commands Cheat Sheet
Learn Linux from Beginner to Advanced (Part-2) | File & Directory Commands Cheat Sheet
Learn Linux from Beginner to Advanced (Part-2) | File & Directory Commands Cheat Sheet
mkdir documents work personal

Creating Nested Directories

What if we want to create a directory structure like this: 'documents' -> 'university' -> 'projects'? To achieve this, use the '-p' flag. This tells mkdir to create parent directories as needed. Here's how:

mkdir -p documents/university/projects

Navigating and Understanding Directories

Now that we've created some directories, let's navigate our new document organization. Use 'ls' to list directory contents, and 'cd' to change directories. 'cd' stands for change directory.

AD

Use 'ls -l' to list in a long format, showing permissions and ownership. For a human-readable format, use 'ls -h'. To list hidden files too, use 'ls -a'.

Linux Commands Cheat Sheet: Essential Commands Every Beginner Should Know
Linux Commands Cheat Sheet: Essential Commands Every Beginner Should Know
the linux reference screen is lit up with neon colors
the linux reference screen is lit up with neon colors
the linux file system has many different types of information on it, including text and images
the linux file system has many different types of information on it, including text and images
Master Linux Special Permissions: SUID, SGID, and Sticky Bit
Master Linux Special Permissions: SUID, SGID, and Sticky Bit
🐧 Linux File System — A Beginner-Friendly Guide
🐧 Linux File System — A Beginner-Friendly Guide
the linux directory structure is shown in this diagram, with several different types of web pages
the linux directory structure is shown in this diagram, with several different types of web pages

Navigating Through Directories

To navigate into 'documents', you'd use:

cd documents

Navigating Beyond Directories

To navigate back to your home directory, you'd use:

AD
cd ~

Notice the tilde (~) representing your home directory. This is a convenience feature in Linux.

StudyWithSTEM is Visuals That Make Ideas Click.
StudyWithSTEM is Visuals That Make Ideas Click.
Linux Directory Structure Explained (FHS Guide)
Linux Directory Structure Explained (FHS Guide)

Deleting Directories

Sometimes, you might want to remove a directory. Use 'rmdir' for empty directories. Try to remove 'personal' and 'work':

rmdir personal work

If you've files inside, use 'rm -r' to remove the directory and its contents recursively.

AD

Removing Directories with Contents

Let's make 'documents' have some contents and remove it with 'rm -r':

rm -r documents

Always be careful when using 'rm -r', as it's powerful and can't undo changes.

With this guide, you now know how to create, navigate, and delete directories in Linux. This will help you shape your Linux experience, keeping it organized and easy to manage. Familiarize yourself with these commands, practice them, and you'll be well on your way to mastering Linux.

AD