how to make a directory in linux

How to Create a Directory in Linux

By David Walker
AD

Creating a directory in Linux is a fundamental operation that all users should understand. Directories, traditionally referred to as folders, are used to organize files and subdirectories. Here, we'll guide you through this process using simple commands and best practices.

Creating a New Directory

To start, let's understand the basic syntax for creating a new directory. The command is 'mkdir', which stands for 'make directory'.

Let's create a new directory named 'documents' in the home folder. The command would be: `mkdir documents`

AD
How to List Only Directories in Linux - LinOxide
How to List Only Directories in Linux - LinOxide
Linux Directory Structure Explained for Beginners
Linux Directory Structure Explained for Beginners
Navigating Linux Directory Tree
Navigating Linux Directory Tree

Creating Multiple Directories

With 'mkdir', you can also create multiple directories at once. Let's create 'documents', 'pictures', and 'videos' directories. The command would be: `mkdir documents pictures videos`

Remember, these directories are created in the current directory where you're running the command. If you want to create them in a different location, you'll need to specify the full path.

Learn how to copy a directory on Linux
Learn how to copy a directory on Linux
Daniel Dietrich  on Twitter
Daniel Dietrich  on Twitter
How to Linux
How to Linux

Creating Directories with Subdirectories

What if you want to create a directory structure like 'documents/letters' or 'pictures/holidays'? You can achieve this using the '-p' option with 'mkdir'. This tells 'mkdir' to create parent directories as needed.

For instance, to create 'documents/letters' and 'pictures/holidays', the command would be: `mkdir -p documents/letters pictures/holidays`

AD

Understanding Directory Permissions

Before diving deeper into directory manipulation, it's crucial to understand permissions. They determine who can read, write, or execute files in the directory. By default, the creator of a directory can read, write, and execute, but others can only read.

How to Use the Tree Command to Navigate File Directory in Linux - Make Tech Easier
How to Use the Tree Command to Navigate File Directory in Linux - Make Tech Easier
How to add home directory on your Desktop on Linux Mint
How to add home directory on your Desktop on Linux Mint
Create Multiple Directories & Subdirectories Via Single Linux Command
Create Multiple Directories & Subdirectories Via Single Linux Command
How to Rename a File or Directory on Linux
How to Rename a File or Directory on Linux
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
Linux Commands Cheat Sheet: Essential Commands Every Beginner Should Know
Linux Commands Cheat Sheet: Essential Commands Every Beginner Should Know

Viewing and Changing Directory Permissions

To view permissions, use the 'ls -l' command. For instance, if you want to see permissions for the 'documents' directory, you would type: `ls -l documents`

To change permissions, use the 'chmod' command. For example, to give others write access to 'documents', you can use: `chmod u+w,go-w documents`

AD

Understanding SetUID, SetGID, and Sticky Bit

These are special permissions that can be set on directories:

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
Linux Directory Structure Explained (FHS Guide)
Linux Directory Structure Explained (FHS Guide)
How to use mkdir command to make directories in Linux and UNIX? mkdir -p Example
How to use mkdir command to make directories in Linux and UNIX? mkdir -p Example
  • SetUID (Suid): When set on a directory, it changes the owner ID of files created in the directory to the creator's ID. This is primarily used with the 'setgid' option on /usr/local/bin and /usr/local/lib directories to avoid permission problems.
  • SetGID (Sgid): When set on a directory, it changes the group ID of files created in the directory to the group ID of the directory, regardless of the creator's group.
  • Sticky Bit (T): Prevents files in the directory from being removed by any user other than the owner of the file or root.

To set them, you can use 'chmod' with the appropriate flag: 'u+s', 'g+s', or '+t'.

Removing Directories

Once you're done with a directory, you might want to remove it. The command for removing a directory is 'rmdir'.

AD

Warning: Use caution with 'rmdir'. It only removes an empty directory, so you'll need to remove any files or subdirectories first.

For non-empty directories, you can use the 'rm' command with the '-r' (recursive) flag: `rm -r directory_name`

And there you have it - a comprehensive guide on creating, understanding, and removing directories in Linux. With these commands at your disposal, you're well on your way to navigating the Linux file system efficiently.

AD