Signup/Sign In

How Create directories in Linux using mkdir command?

Posted in Programming   MAY 18, 2023

    If you are working on a Linux operating system, it is very often that you try to create a directory and subdirectory. You can do that with a few clicks by your mouse, but sometimes when you are writing a program using a terminal, and you need to create some directories to save your files or when you just wanna look cool then you need some built-in commands such as mkdir to create a directory.

    The mkdir is a command on Linux that is used to create single or multiple directories. You can use it to create subdirectories too. Let's understand with some examples. First, Press (Ctrl+alt+ T) to open terminal.

    Creating Directory using mkdir

    If in the desktop, you want to create a directory called image, so you can just give mkdir command which is the command to create any directory, and then the name of the directory for example:

    $ mkdir image

    Press ENTER, and then it's going to create this directory called image.

    Creating subdirectory

    If you want to create a subdirectory inside this image directory, you can just write mkdir once again and the directory name which already exists and then gives the forward-slash (/) for the subdirectory for example; if you want to create a subdirectory called pics

    $ mkdir image/pics

    Press ENTER and then when you open this image directory it has a pics directory in it.

    If you want to create a subdirectory by giving the main directory name which doesn’t exist at all, this will give you an error

    For example, look at the below command

    $mkdir names/mark

    This gives an error that says no such file or directory because this name's directory doesn't exist which you gave as a parent directory, it cannot create this mark subdirectory inside the names.

    Create Directory Structure using mkdir command

    So creating the directory structure, you need to use this flag called -P to your mkdir command

    $mkdir -p names\mark

    Press ENTER here, and it's going to create the directory called names, and it doesn't give you an error and when you go inside this directory, there is one more directory called mark here. This flag creates a directory structure, and now it will not give any error.

    We can give this flag in two ways P and --parents; -p is the short version of –parents, it's going to create a parent directory for you and also a subdirectory that doesn't even exist.

    Create Multiple Subdirectories using mkdir command

    If you want to create several directory trees inside your parent directory, you have to write mkdir and then -P, and then you just give these curly brackets {} and inside the curly brackets you can provide the list of the directory, which you want to create

    For example, If you want to create a directory called John; Tom; Bob inside this names directory. Then type.

    $mkdir–p names\{ John, Tom, Bob }

    Press ENTER, and this time it will create this three directory

    For a correct directory structure, you need to give the comma without space. So this format of creating multiple directories is correct without spaces.

    Conclusion

    When you are dealing with the Linux operating system, you want to create a directory using commands of Linux. The article gives you an overview of how to create a directory in the Linux operating system, using the commands in the terminal, it also gives you a piece of information about how to create multiple subdirectories. Hope you like this article.

    About the author:
    Tags:commandscommand-linelinux
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS