Linux mkdir existing directory

Is mkdir -p totally safe when creating folder already exists

I won’t see any errors even warnings. So just want to confirm, is there anything lost or changed in result of this command?

The help of mkdir says that the directories are only created when they are not present. This implies to me that when the directory exists there is nothing done.

4 Answers 4

mkdir -p would not give you an error if the directory already exists and the contents for the directory will not change.

This answer does not seem to be correct. mkdir indeed emits an error if the directory exists, unless using the -p flag.

in error, you could check for the code like this if(err.code == ‘EEXIST’) this condition will get true if the directory already exists.

A portable script will rely upon POSIX, which says of mkdir ‘s -p option:

Each dir operand that names an existing directory shall be ignored without error.

and if there are no errors reported, the -p option has done its job:

Create any missing intermediate pathname components.

mkdir WILL give you an error if the directory already exists.

mkdir -p WILL NOT give you an error if the directory already exists. Also, the directory will remain untouched i.e. the contents are preserved as they were.

When I execute mkdir -p folder I won’t see any errors even warnings.

You will see an error if the command fails. The -p flag only suppresses errors if the directory already exists.

touch x mkdir -p x mkdir: cannot create directory ‘x’: File exists 

The same issue will occur if you try to create a directory as a normal user in, say, /etc .

What the -p will suppress are errors that would be triggered when the target directory already exists

However in all cases you won’t lose anything, and nothing will be changed. In the error situations you just won’t have the directory you were expecting.

Читайте также:  Linux посмотреть файлы открытые процессом

Источник

Make Directory Only if it Doesn’t Exist Already in Linux

It’s better to be careful and prevent errors while creating directories in case there are files or directories with the same name in the same location.

To create a directory in Linux, you use the mkdir command. It stands for ‘make directory’ after all. It’s quite a simple command.

But, when you try to create a directory that already exists, you get an error like:

$ mkdir dir0 mkdir: cannot create directory ‘dir0’: File exists

This indicates that if a directory already exists, the mkdir command will not try to create or modify the already existing directory in any way.

But it also shows an error which is not what you always want especially if you are writing a bash script.

To prevent this error, use the -p flag along with mkdir command.

This way, though the directory is not created, it won’t show any error as well. Your scripts will run as usual.

You’ll get the same error if a file or link exists with the same name as the directory you are trying to create. Why? Because everything is a file in Linux. A directory is a special file that acts like an index of all the files that are inside it.

Let’s see things in a bit more detail here and discuss various ways of preventing this error.

Method 1: Use ‘-p’ option

If you look at the manpage of the mkdir command, it has the ‘-p’ flag.

The use of ‘-p’ flag in mkdir is «no error if existing, make parent directories as needed».

When you use the ‘-p’ flag, the mkdir utility will check if a directory, file, link or pipe with the same name does not exist already.

If the does exist, it will not modify your existing directory or file, nor will it show an error message.

This is very handy when you are creating custom bash scripts and don’t want its execution to stop because of this error.

The -p can also be used to create a nested directory structure. If you want to create dir1/dir2/dir3 like directory structure and some or none of the directories exist in the hierarchy.

Method 2: Check if directory already exists in bash

If you are writing bash scripts, you can use the if condition and check if the directory already exists. If it doesn’t then you create the directory.

if [ -d my_dir ] then mkdir my_dir fi

Method 3: Send it to the void

A majority of UNIX tools have two output streams, stdout and stderr. Normally, both streams, stdout and stderr get printed to the terminal. But you can redirect either the normal output stream or the error stream to another file.

Читайте также:  Linux find exe path

So, when the mkdir command throws an error to your terminal, you can redirect it to the void.

To redirect stdout, use it along with the ‘1’ numerical stream descriptor, for stderr, use the numerical stream descriptor ‘2’. You can also redirect stdin by using the ‘0’ stream descriptor.

To actually redirect the output, use the appropriate stream descriptor along with the redirection operator ‘>’

This will send the output of stderr to the ‘/dev/null’ device which discards anything that gets written to it.

This is totally safe to do. As I mentioned earlier, if the directory already exists, then it will not be modified. Only an error message will be shown. All you are doing here is suppressing that error message.

Personally, I would go with the first method i.e. using the mkdir -p command for ensuring that the directory is created only if there is no file or directory of the same name in the same location.

Источник

How To “mkdir” Only If Directory Not Exist In Linux?

How To

Linux mkdir command is used to create directories. The mkdir command creates directories that don’t exist. But in some cases, the directory may already exist where we do not want to create or recreate the directory. You may ask how can I accomplish this if the directory exists does not run mkdir command?

Directory Existince Check with -p Option

The mkdir command provides the -p option which is used to parent check. The -p option also used to create multi level directories by checking parents. We can use the -p in order to check the specified directory existence. If the specifed directory exists the command sliently skips create of the directory. First look how the mkdir command behaves by defualt if the specified directory exist.

mkdir: cannot create directory ‘data/backup1’: File exists

We can see from the output that if the specified directory exist it returns error like “cannot create directory” etc. It also prints the message “File exists”.

The -p option can be used to check the directory existence and do not create new direrctory if exist and create directory if not exist.

Читайте также:  Set domain name linux

Directory Existince Check with Bash Script

Bash can be used to check the directory existence and prevent “File exists” error. Generally bash scripts are used to check directory existence and create directory if the directory not exist. The following script checks the directory existence with the conditional.

[ -d /var/data/backup ] || mkdir var/data/backup

Supress “File Exist” Error

Also the mkdir command can be used wihtout any option. The error message can be suppressed and redirected into the /dev/null device. This do not prints the error code and error message to the standard output.

mkdir /var/data/backup >/dev/null 2>&1

Источник

Only mkdir if it does not exist [duplicate]

I can’t find the source on the fly, but for Makefiles mkdir -p is discouraged b/c there may be race conditions in concurrent execution. So, depending on what your script does and in which environment it lives the first option given is this answer should be preferred. Also, you could just mkdir product 2>/dev/null and not care.

@user1129682: The first option also has a potential race condition; the directory could be created by some other process between the -d test and the mkdir . I suspect mkdir -p makes this time window for this race condition slightly shorter.

Beside the race condition in the innecessary testing, there is also the possibility that dir does exist but is not a directory, but a file or a device entry or a unix-domain socket or a named pipe, or whatever .

@wildplasser Yes and that would already be in the coder’s option how he/she would handle the situation. One could send a message to the user and ask for input, abort, or do other things if [[ -e dir && ! -d dir ]] is valid.

if [ ! -d directory ]; then mkdir directory fi 

-p ensures creation if directory does not exist

Use mkdir’s -p option, but note that it has another effect as well.

 -p Create intermediate directories as required. If this option is not specified, the full path prefix of each oper- and must already exist. On the other hand, with this option specified, no error will be reported if a directory given as an operand already exists. Intermediate directories are created with permission bits of rwxrwxrwx (0777) as modified by the current umask, plus write and search permission for the owner. 

Источник

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