Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a directory exists?

Tags:

c

linux

directory

How can I check if a directory exists on Linux in C?

like image 692
user1543915 Avatar asked Sep 20 '12 10:09

user1543915


People also ask

How do you check if a directory exists or not in bash?

In order to check if a directory exists in Bash using shorter forms, specify the “-d” option in brackets and append the command that you want to run if it succeeds. [[ -d <directory> ]] && echo "This directory exists!" [ -d <directory> ] && echo "This directory exists!"

Does mkdir check if directory exists?

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.

How do you check if a directory exists or not in Python?

os. path. isdir() method in Python is used to check whether the specified path is an existing directory or not. This method follows a symbolic link, which means if the specified path is a symbolic link pointing to a directory then the method will return True.


1 Answers

You can use opendir() and check if ENOENT == errno on failure:

#include <dirent.h> #include <errno.h>  DIR* dir = opendir("mydir"); if (dir) {     /* Directory exists. */     closedir(dir); } else if (ENOENT == errno) {     /* Directory does not exist. */ } else {     /* opendir() failed for some other reason. */ } 
like image 67
hmjd Avatar answered Oct 04 '22 13:10

hmjd