How to use the mkdir command to create a directory in Linux

In the Linux system, you can from the command line or desktop file manager to create a new directory. It allows you to create directories (also called folders) command is mkdir.

This tutorial covers the basics of using the mkdir command, including common examples.

Linux mkdir command syntax

Linux mkdir command syntax is as follows:

mkdir [OPTION] [DIRECTORY]

The command may be one or more directory name as its argument.

To create the directory using the mkdir command, you need to have write access to the parent directory. Otherwise, you will receive a permission denied error.

How to create a new directory

To use the mkdir command to create a directory in Linux, just the directory name is passed as a parameter to the mkdir command:

mkdir newdir

You can use the following ls command lists the contents to verify that the directory has been created:

ls -l
drwxrwxr-x 2 username username 4096 Jan 20 03:39 newdir

If you want to print messages mkdir to create a directory, you can pass the -v (- verbose) option.

When only a directory name, it will be created in the current working directory. The current working directory is the directory where the command is run. You can use the pwd command to display the location of the current working directory. To change the current working directory, use the cd (change directory) command.

To create a directory in another location, you need to provide the absolute or relative file path to the parent directory. For example, to create a new directory in the directory, / tmp type:

mkdir /tmp/newdir

If you try to create a user in the parent directory does not have sufficient permissions directory, you will receive a Permission denied error:

mkdir /root/newdir
mkdir: cannot create directory '/root/newdir': Permission denied

How to create a parent directory

Parent directory is the directory in a directory tree on the other directory. To create a parent directory, use the -p option.

Suppose you want to create the directory / home / linuxize / Music / Rock / Gothic. If any parent directory does not exist, you will receive an error as follows:

mkdir /home/linuxize/Music/Rock/Gothic
mkdir: cannot create directory '/home/linuxize/Music/Rock/Gothic': No such file or directory

You can use the -p option, rather than one by one to create all the missing parent directories:

mkdir -p /home/linuxize/Music/Rock/Gothic

When using the -p, mkdir will create the directory only if the directory does not exist.

For example, if you try to create a directory that already exists and does not use the -p option, mkdir will then print File exists error:

mkdir newdir
mkdir: cannot create directory 'newdir': File exists

How to set permissions when you create a directory

To set permissions, use the -m (-mode) option when creating a directory. Permission to pass the same syntax syntax chmod command.

In the following example, only the user who created it in order to access the new directory:

mkdir -m 700 newdir

If the -m option is not passed to the mdkir, the newly created directory will have permissions 775 or 755, depending on the umask value.

How to create multiple directories

To create multiple directories, set the directory name specified as a parameter, separated by a space:

mkdir dir1 dir2 dir3

mkdir command also allows you to create complex directory trees with one command:

mkdir -p Music/{Jazz/Blues,Folk,Disco,Rock/{Gothic,Punk,Progressive},Classical/Baroque/Early}

The above command will create the following directory tree:

Music/
|-- Classical
|   `-- Baroque
|       `-- Early
|-- Disco
|-- Folk
|-- Jazz
|   `-- Blues
`-- Rock
    |-- Gothic
    |-- Progressive
    `-- Punk

in conclusion

By now, you should have a good understanding of how to use Linux mkdir command. For more information about the mkdir command, see the mkdir man page .

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159881.htm