Create new folder in linux

mkdir command: Create New Directories in Linux

mkdir is one of the essential Linux commands that every Linux user should know. You can create new directories using mkdir.

One of the essential Linux commands is mkdir. The mkdir allows you to make new directories (folders in common term) in Linux.

In this beginner series, you’ll learn to use the mkdir command.

mkdir command examples

The mkdir command is one of the rare few Linux commands that doesn’t have tons of options. And that makes it really simple to use.

mkdir [option] directory_name_or_path

Create new directories

To make a new directory, use mkdir command without any option:

This will create a new directory named new_dir in the present directory. You can check it using the ls command.

[email protected]:~/tuts$ ls [email protected]:~/tuts$ mkdir new_dir [email protected]:~/tuts$ ls -l total 4 drwxrwxr-x 2 abhishek abhishek 4096 May 14 16:15 new_dir

You may also specify the path to where you want to create the new directory.

[email protected]:~/tuts$ ls new_dir [email protected]:~/tuts$ mkdir new_dir/another_new_dir [email protected]:~/tuts$ tree . └── new_dir └── another_new_dir 2 directories, 0 files

Create multiple directories

You may also create several new directories with a single command:

mkdir new_dir_1 new_dir_2 new_dir_3

All the new directories are created at the same level. You may also create nested directories which is described in the next section.

Create nested directories

You can use the option -p to create a nested directory structure. If the parent directory doesn’t exist, it will create it for you.

This is particularly helpful when you want to create a directory structure or if you want to make sure that the directory path exists.

This is what the above command created:

[email protected]:~/tuts$ mkdir -p dir1/dir2/dir3/dir4 [email protected]:~/tuts$ tree . ├── dir1 │ └── dir2 │ └── dir3 │ └── dir4 └── new_dir └── another_new_dir 6 directories, 0 files

You may also use the -p option with a single directory. It won’t create a new directory that already exists, but it won’t throw any errors as well:

[email protected]:~/linuxhandbook$ mkdir new_dir mkdir: cannot create directory ‘new_dir’: File exists [email protected]:~/linuxhandbook$ mkdir -p new_dir [email protected]:~/linuxhandbook$ ls -l total 8 drwxrwxr-x 3 abhishek abhishek 4096 May 14 16:39 dir1 drwxrwxr-x 3 abhishek abhishek 4096 May 14 16:16 new_dir

Create directory with specific permissions

By default, your shell’s umask controls the permission on the newly created directories. If you want different file permissions on the directory, instead of creating the directory first and then changing the permission with the chmod command, you can use the -m option.

Читайте также:  Простые графические редакторы линукс

Suppose you want permission 766 on the directory you are going to create. You can use:

mkdir -m 766 new_directory

That’s pretty much what you need to know about the mkdir command. Now that you know how to create directories, maybe you would want to learn about the rmdir command and deleting directories in Linux command line.

Источник

Create Folder Linux | mkdir command in Linux [Create Directory]

This post will help you to create directory in linux and create folder in Linux or UNIX operating system with mkdir command in the linux System.

Create Folder Linux | mkdir command in Linux [Create Directory]

List of content you will read in this article:

Everyone who is a beginner or advanced Linux administrator somehow is looking to create folder in Linux or create directory in Linux with one of the most commonly used linux commands i.e. mkdir command in linux or UNIX like system.

Linux is widely adopted and used due to its stability and flexibility in carrying out various tasks in an easier way. One of the key features of Linux is the terminal, where you can run any command to run various tasks, from making changes to the system to downloading any required software.

This article will highlight the commands with various options to create directory or create a folder in the Linux system. Linux offers an easy command for that- mkdir command which stands for “make directory”. Also, the mkdir command will allow you to set permissions, create multiple directories using a single command, and do many other tasks.

Before you run the mkdir command, make sure to complete the prerequisites, such as

  • Linux or Unix-like OS.
  • Access to the command-line terminal for executing the commands.
  • The user should be having the right access to create the directory.

What is mkdir command in Linux? [L inux Make Directory ]

If you want to make a directory, you can use the mkdir command to create single or multiple directories. You can use this command along with various options for applying various functionality. The syntax can you can use for the mkdir command is as follows-

mkdir [option] dir_name

mkdir options

Option / Syntax

Description

This command will create a directory within the current directory location.

This command will help in creating multiple directories in the current directory location. Make sure that you do not use the spaces inside the <>

This command will allow you to create a directory structure with the missing parent directories (if there are any)

This command will help in creating a directory and allow you to provide full read, write, and execute permissions for every user

This command will help create a directory in the current location along with the details.

How to Create a Directory in Linux [C reate Directory Linux ]

To run the mkdir command, go to the terminal and open it with admin access if you have one. Otherwise, you just need the right and appropriate access to execute the mkdir command.

Always remember that the options in Linux are case-sensitive, so use them correctly else the meaning will change. As per the below example, we have created a directory called “monovm.”

Читайте также:  Консоль кластера серверов 1с linux

mkdir monovm

This command will result in the creation of the directory in the current working directory. If the directory has been created successfully, you will get an empty line on the terminal. If you want to verify the result of the directory creation, you can use the ls command.

How to Create multiple directories in Linux?

If you want to create multiple directories in the same directory, you can use the mkdir to create them separately. But it will take time to run individual commands. So to save your time by running separate commands, you can use a single mkdir command and the directory names separated by a comma.

Consider the following example:

There is no need to add space between the directory names in the brackets. But, if you add the space, the name will take an extra character.

How to Create a parent directory?

If you want to create a structure with multiple subdirectories, you can use the mkdir command along with the “-p: option. This option will ensure that the missing parent directory will be added in the process.

Here, we are considering the example where we want to add the “dirtest2” directory within the “dirtest1” directory in the Linux directory. Then we have to provide the complete path with the mkdir command, as shown below.

mkdir –p Linux/dirtest1/dirtest2

Once you run the mkdir command with the complete path, you can run the “ls” command along with the “-R” option to confirm the creation of the directory. This option will help show the recursive directory tree that will display the contents of each directory present in the provided path.

But, if you miss the “-p” option, the terminal will display an error that the directory that you have provided does not exist. You can see the below example.

In the above example, the terminal will show an error that the dirtest3 does not exist rather than creating the parent directory.

How to Set permission to the directory while creating

If you create any directory, then by default, the directory will get the “rwx” permission but only for the user who created that directory. If you want to change the permission for the directory for all the users, then you can use the “-m” option along with the mkdir command.

In the below example, we are providing permission to the directory as “777,” that is, any user will be able to read, write, or execute that file if required.

mkdir -m
mkdir -m 777 alpha
ls -l

If you want to check the details of the created directory and their permissions, then you need to get a long list of the directories. You can run the “ls” command along with the “-l” option.

Verifying the directories

So far, you have noticed that after running the mkdir command, you will not get any feedback or the result showing if the creation is successful or not. If you want see the details of the mkdir command, then you can use the “-v” option with the mkdir command.

Читайте также:  Планшет на линуксе как установить

Considering the below example with the “-v” option, you will get the details of the command.

Now that you get the complete details of the directory creation, you do not have to run the “ls” command to creak the created directory.

How to Create Folder in Linux? [C reate Folder Linux ]

Follow the below-given steps to create a folder in Linux easily.

Step 1: In Linux, the first SSH to linux.

Step 2: enter mkdir dir1 command to build a folder with the name dir1.

Let’s take a closer look at certain cases and other applications. The syntax to create a directory in Linux is as follows:

mkdir dir1
mkdir [option] folderName
mkdir directory

By using the ls command to list the contents of the directory, you can confirm that it was created:

This will show the list of all the current directories.

mkdir’s -v (—verbose) option instructs it to print a message for each directory it creates.

mkdir: created directory ‘folder’

The file is generated in the current working directory when only the directory name is provided without the complete path. The latest working directory is the location where the commands are being executed. The cd command is used to modify the actual working directory.

To build a directory in a different region, you’ll need to specify the parent directory’s absolute or relative file direction. To construct a new directory in the /xyz directory, for example, type:

You’ll get a Permission denial error if you want to build a directory in a parent directory where the user doesn’t have enough permissions.

After executing the command, you will get the output like this:

mkdir: cannot create directory ‘/root/newdirectory’: Permission denied

You can create a parent folder using the following command:

mkdir -p dir11/dir12
mkdir -p parent1/child1
mkdir -p pictures1/memories1

This will list all the directories in the parent and child folders. To verify it, you have to follow the below-listed command.

rmdir command in Linux

Here, we are removing a folder called directory1 so that we will execute the below command:

Conclusion

This is all about how to create folder in Linux by using mkdir command. With this guide, you will be able to understand the working of the mkdir command. You can use the mkdir command along with various options that we have mentioned and explained their usage, how they impact the working of the normal mkdir command and how it changes the output. Make sure to use the right option in the right scenario to get the correct result. The options are case-sensitive, so make sure to use the right option else you will end up with the error.

You can buy linux VPS to host your website or applications and test or practice all these commands to make yourself perfect.

People Are Also Reading:

Источник

Оцените статью
Adblock
detector