Mkdir

From Unix SME

Command:

    mkdir


Description:

    Use the mkdir command to create one or more directories or 
    subdirectories. It takes an argument as a list of paths to the 
    directories you want to create. A directory is created within the current or working directory unless you 
    specify an absolute path name to another location in the file 
    system. You can specify the permissions for the new directories 
    with the -m Mode flag. You can use the umask subroutine to set the 
    default mode for the mkdir command.


Syn/Syp:

    mkdir [OPTION]...DIRECTORY... 


Options:

    -m or --mode
  • Sets file modes or permissions for the created directories.
  • The syntax follows that of the chmod command.
  • Use this option to specify permissions, such as read, write, and execute, for the new directories.
    -p or --parents
  • An option that allows the creation of parent directories as necessary.
  • If the specified directories already exist, no error is reported.
  • Useful for creating a directory hierarchy without errors.
    -help
  • Displays help-related information for the mkdir command and exits.
  • Use this option to get assistance on how to use the command and its various

features.

    -version
  • Displays the version number and additional information about the license for mkdir, providing details about the software version installed.
  • Use this option to check the installed mkdir version.
    -v or -verbose
  • Enables verbose mode, displaying a message for every directory created.
  • When used with the [directories] argument, it shows the names of the created directories.


Use Case:

    With mkdir you can set permissions and create multiple directories at once. 


Examples:

Example 1 -

  • To create a single directory, use the following syntax:
    mkdir [directory]
  • Ex:
    mkdir jasmins_art
  • This command creates a directory named "jasmins_art" in the current location.


Example 2 -

  • To create a directory with verbose using mkdir, use the following syntax:
    mkdir -v [directory]
  • Ex:
    mkdir -v jasmins_brushes
    mkdir: created directory 'jasmins_brushes'
  • This command creates a directory named "jasmins_brushes" and sees verbose at the same time.


Example 3 -

  • To create multiple directories at once, use the following syntax:
    mkdir [directory1] [directory2] [directory 3]...
  • Ex:
    mkdir jasmins_art jasmins_brushes jasmins_paints
  • This command creates three directories named "jasmins_art", "jasmins_brushes", and "jasmins_paints" in the current location.


Example 4 -

  • The mkdir command also supports absolute and relative paths
    mkdir /jasmins_art/jasmins_sketches
  • This command creates a directory structure with "jasmins_art" as the parent directory and "jasmins_sketches" as its subdirectory.


Example 5 -

  • If you encounter a "permission denied" error while creating a directory, you may not have permission to create directories in that location.
  • To resolve this you can give root access to the user by using "sudo" command.
    sudo mkdir jasmins_pencils


Example 6 -

    mkdir --help
  • It displays help-related information on the command mkdir and exits


Example 7 -

    mkdir --version
  • It displays the version number, some information regarding the license, and exits


Example 8 -

  • To use option -p, use the following syntax:
    mkdir -p [directories]
  • An option that enables the command to create parent directories as necessary. If directories exist, no error is specified.
  • Ex:
    mkdir -p jasmin/jasmins_art/jasmins_finished_art
  • If the first and second directories do not exist, due to the -p option, mkdir will create these directories for us. If we do not specify the -p option, and request the creation of directories we will get an error code.


Example 9 -

  • To set the file modes to read, write, and execute, use the following syntax
    mkdir -m a=rwx [directories]
  • Ex:
    mkdir -m a=rwx jasmins_art
    mkdir –m777  jasmins_art
  • The above command specifies that the directory created gives access to all the users to read from, write to and execute the contents of the created directory.

Example 10 -

  • To set the file mode to just read, use the following syntax
    mkdir -m a=r [directories]
  • Ex:
    mkdir -m a=r jasmins_art
  • The above command specifies that the directory created gives access to all the users to read from the contents of the created directory.