Linux create directory with path

mkdir – Creating directories with the Linux command mkdir

The Linux command mkdir (make directory/make directories) can be used to create new directories.

Basic Usage

Creating directories in the current working directory

The most basic usage of mkdir is the following:

This will create a new directory with the given name in the current directory. Therefore, if your username is “me” and in the terminal you are using you are currently in your home folder (/home/me) then running the command

will create a the directory /home/me/backup.

Creating directories using relative or absolute paths

You do not need to be in a directory do create a new directory in it. You can, for example, use relative or absolute paths to create them.

Let’s assume you are in your home folder (/home/me) and you just created the folder backup (/home/me/backup). And now you want to create a folder named “old” in it. Then you can either call mkdir with a relative path:

or with the absolute path:

Note, however, that if you would call

and the directory “backup” would not exist already in the current folder, then you would get an error. This is, because calling mkdir like this:

will create the directory in the directory only if already exists. Otherwise, mkdir will quit with an error message. This is the default behavior of mkdir.

Options

There are a few options which can be quite useful when creating directories.

-p, –parents

Under “Basic Usage”, I mentioned that the path you want to create a new directory in, needs to exists or otherwise you will get an error. You can use the option “-p” or “–parents” to change that behavior. With this option, mkdir will create all the directories that don’t exists already, not just the last one. If you would call

in your home directory and the directory “backup” would not exist already, then the -p options will tell mkdir to create this directory as well instead of quitting with an error message. This option is therefore very useful if you need to create a directory, but you are not sure if the directory you want to create it in already exists. Even if you know that this directory does not exist yet, you can use the -p option to reduce the number of necessary calls.

If you know that there currently is no backup (or are not sure) and you want to create the folder backup/old, then calling

Читайте также:  Rufus создать загрузочную флешку linux

could result in an error if the directory “backup” would not exist. This is a problem especially in scripts. Without the -p option you would have to make two calls to mkdir; one to create the “backup” directory (to ensure it is there) and one to create the directory “old” itself:

When creating deeper directory structures, more than two calls might be needed. Think about creating the folder “backup/server/2011/11/30″ in your home folder (/home/me). You would have to create every folder separately:

mkdir backup mkdir backup/server mkdir backup/server/2011 mkdir backup/server/2011/11 mkdir backup/server/2011/11/30

With the -p option you just need to call mkdir once:

mkdir -p backup/server/2011/11/30

and everything will be created for you.

It is also worth noting that using the “-p” option will prevent errors in case the directory already exists. Without the “-p” option, running

would result in the following error message in case the directory backup already exists:

mkdir: cannot create directory `backup': File exists

will prevent the error message from being printed. Running mkdir with the “-p” can therefore be interpreted as “make sure the following directory exists”.

-v, –verbose

When using the “-v”/”–verbose” option, mkdir will ouput a message for the directory it created.

will therefore either output

mkdir: created directory `backup'

if the directory did not exist already or print the previously mentioned error message in case the directory was already there. When combining the “-v” option” with the “-p” option, mkdir will output a message for every directory it created.

mkdir -p -v backup/server/2011/11/30
mkdir -pv backup/server/2011/11/30

would create the following output if none of the directories would already exist:

mkdir: created directory `backup' mkdir: created directory `backup/server' mkdir: created directory `backup/server/2011' mkdir: created directory `backup/server/2011/11' mkdir: created directory `backup/server/2011/11/30'

If, for example, the directory “backup/server” already existed, then mkdir would only need to create the last three directories and therefore the output would be:

mkdir: created directory `backup/server/2011' mkdir: created directory `backup/server/2011/11' mkdir: created directory `backup/server/2011/11/30'

Using this option therefore provides feedback about which directories already existed and which had to be created. Because the “-p” option suppresses error messages about already existing directories, you won’t get error messages about the fact that “backup” and “backup/server” could not be created, because they were already there. The messages from mkdir about which directories have been created would therefore only implicitly tell you which directories existed beforehand.

-m, –mode=MODE

Using the “-m” option you can set the permission on the directory you want to create. If you want to create a directory which should be fully accessible by everyone, you could type

It is important to note that when combining this option with the “-p” option, the mode only affects the last directory. Calling

mkdir -p -m 777 backup/server/2011/11/30

would therefore create all the directories necessary, but the permissions specified with the “-m” option will be only assigned to the directory “30″ and not to the other directories. Their permissions are instead determined by the current umask.

Читайте также:  Tar and split linux

It is also worthy to note that the permissions of the directory “30″ would not be changed in case it already existed. This is especially important to point out, because the “-p” option will prevent all error messages about already existing directories. Therefore, running this command does not give you any feedback about the permissions the directory “30″ has after running the command. If it already existed, the permissions will be left untouched and if it didn’t exist, the permissions will be set as defined by the “-m” option.

Therefore, if you are not sure if the directory already existed, but want to make sure that the permissions are set correctly, you could either run the command with the -v option (which will then tell you if the directory “30″ was created) and then use chmod if necessary or – if you are writing a script – you should just run chmod afterwards instead of using the “-m” option:

mkdir -p backup/server/2011/11/30 chmod 777 backup/server/2011/11/30

Источник

How To Create Directories/Folders In Linux?

How To Create Directories/Folders In Linux?

Directories or Folders are used to store files and folders. Directories can be created from GUI by using a desktop environment and application or command-line interface commands. In this tutorial, we will learn how to create directories in Linux. This tutorial can be used for all major Linux distributions like Ubuntu, Debian, Mint, Kali, CentOS, Fedora, RHEL, SUSE, etc.

mkdir Command Syntax

The mkdir command the standard command used to create directories. The mkdir command has the following syntax.

  • OPTIONS is used to set different attributes about the directory creation. This options is optional.
  • DIRECTORY is single or multiple directories which will be created. This option is required.

List Existing Directories

Before starting to create new directories and folders we can list existing directories and folders to get their names. Also after creating the directories and folders created directories and folders can be listed too. The ls command is used to list directories and folders.

Create Diretory

Even the mkdir provides different features we will start with a simple example where we will create a single directory with the specified name. The new directory name will be “data” for the following example.

We can list the created directory with the ls command like below.

The absolute path or full path can be used to create a directory that is a more reliable and flexible way. By default, the given directory will be created in the current working path but by using the relative path directories can be created in other paths. In the following example, we will create a directory named data in the “/var/log“.

Читайте также:  Linux проверка целостности данных

Relative paths can be also used to create directories. Relative path is set according to the current working directory or given path specifier. In the following example we will create the directory named “data” in the parent directory.

Create Multiple Directories

The mkdir command can be also used to create multiple directories in a single command. Just provides the directory names by separating them with spaces.

Also, the mkdir command can be used to create multiple directories with their absolute or full path. This is a more reliable and flexible way to create directories.

Create Non-Existin Multi-Level Directories

Multi-level directories can be created with the mkdir command even some intermediate directories do not exist. These non-existing directories and child directories will be also created by using the -p option. Just provide the child and parent directories you want to create. In the following example, the directory year and 2020 do not exist but will be created too.

Set Permissions During Directory Creation

By default when a directory is created the umask value will be set as permission. Also, the chmod command can be used to change given single or multiple directories permissions but the mkdir command also supports settings permission during creation. The -m option can be used to set different than default permissions by providing the numerical representation of the permissions. In the following example, we will create a directory named “newdir” which has 777 permission.

Alternatively multiple created directories permissions can be set with the -m option like below.

mkdir -m 777 newdir data backup

“cannot create directory: File exists” Error

The “cannot create directory: File exists” error occurs when the given directory is already exist. So the new directory can not be created and overwritten.

“cannot create directory: No such a file or directory” Error

The “cannot create directory: File exists” error can occur in different situations where any of the parent directories in the given path do not exist and so the given directory can not be created because of the parent directory. This error can be solved by creating parent directories too by using the -p option which is described previosuly.

mkdir -p /home/ismail/Music/Rammstein/2000

Create Directory with GUI

The GUI tools like File Manager etc. can be used to create directories. But it has limited functionalities according to the mkdir command. For example multiple directories can not be created with GUI. Below we will create a new directory by right-clicking and selecting the “New Folder“.

In this step, we will specify the directory name we want to create in the current path. The last step is clicking on the “Create” button.

Источник

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