Checking If a Directory Exists in Unix (System Call)

Checking if a directory exists in Unix (system call)

There are two relevant functions on POSIX systems: stat() and lstat(). These are used to find out whether a pathname refers to an actual object that you have permission to access, and if so, the data returned tells you what type of object it is. The difference between stat() and lstat() is that if the name you give is a symbolic link, stat() follows the symbolic link (or links if they are chained together) and reports on the object at the end of the chain of links, whereas lstat() reports on the symbolic link itself.

#include <sys/stat.h>

struct stat sb;

if (stat(pathname, &sb) == 0 && S_ISDIR(sb.st_mode))
{
...it is a directory...
}

If the function indicates it was successful, you use the S_ISDIR() macro from <sys/stat.h> to determine whether the file is actually a directory.

You can also check for other file types using other S_IS* macros:

  • S_ISDIR — directory
  • S_ISREG — regular file
  • S_ISCHR — character device
  • S_ISBLK — block device
  • S_ISFIFO — FIFO
  • S_ISLNK — symbolic link
  • S_ISSOCK — socket

(Some systems provide a few other file types too; S_ISDOOR is available on Solaris, for example.)

How do I check if a directory exists in a Bash shell script?

To check if a directory exists:

if [ -d "$DIRECTORY" ]; then
echo "$DIRECTORY does exist."
fi

To check if a directory does not exist:

if [ ! -d "$DIRECTORY" ]; then
echo "$DIRECTORY does not exist."
fi

However, as Jon Ericson points out, subsequent commands may not work as intended if you do not take into account that a symbolic link to a directory will also pass this check.
E.g. running this:

ln -s "$ACTUAL_DIR" "$SYMLINK"
if [ -d "$SYMLINK" ]; then
rmdir "$SYMLINK"
fi

Will produce the error message:

rmdir: failed to remove `symlink': Not a directory

So symbolic links may have to be treated differently, if subsequent commands expect directories:

if [ -d "$LINK_OR_DIR" ]; then 
if [ -L "$LINK_OR_DIR" ]; then
# It is a symlink!
# Symbolic link specific commands go here.
rm "$LINK_OR_DIR"
else
# It's a directory!
# Directory command goes here.
rmdir "$LINK_OR_DIR"
fi
fi

Take particular note of the double-quotes used to wrap the variables. The reason for this is explained by 8jean in another answer.

If the variables contain spaces or other unusual characters it will probably cause the script to fail.

How can I check if a directory exists?

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. */
}

Bash: how to check if directory exists?

Using Bash, this will give you the behavior you're looking for:

if [[ -d /opt/directory1 ]] || [[ -d /opt/directory2 ]] ; then
echo "SUCCESS"
else
echo "FAIL"
fi

Note the usage of -d on both conditions.

How to check directory exist or not in linux.?

With bash/sh/ksh, you can do:

if [ ! -d /directory/to/check ]; then
mkdir -p /directory/toc/check
fi

For files, replace -d with -f, then you can do whatever operations you need on the non-existant file.

check if directory exists and delete in one command unix

Assuming $WORKING_DIR is set to the directory... this one-liner should do it:

if [ -d "$WORKING_DIR" ]; then rm -Rf $WORKING_DIR; fi

(otherwise just replace with your directory)

C faster way to check if a directory exists

You could call mkdir(). If the directory does not exist then it will be created and 0 will be returned. If the directory exists then -1 will be returned and errno will be set to EEXIST.

Test if directory exists or not

Passing variables to shell script

  • You have to instruct you script that DIRECTORY is a variable, passed as first script argument.
  • You have to enclose your variable into double-quotes in order to ensure spaces and special characters, like empty variables, to be correctly parsed.

Sample:

#!/bin/sh

DIRECTORY="$1"

if [ -d "$DIRECTORY" ]; then
echo "Directory '$DIRECTORY' exists"
else
echo "Directory '$DIRECTORY' does not exists"
fi
echo "$DIRECTORY"

Other sample:

#!/bin/bash

DIRECTORY="$1"
FILE="$2"
if [ -d "$DIRECTORY" ]; then
if [ -e "$DIRECTORY/$FILE" ]; then
printf 'File "%s" found in "%s":\n ' "$FILE" "$DIRECTORY"
/bin/ls -ld "$DIRECTORY/$FILE"
else
echo "Directory '$DIRECTORY' exists, but no '$FILE'!"
fi
else
echo "Directory '$DIRECTORY' does not exists!"
fi
echo "$DIRECTORY/$FILE"

Checking if a file is a directory or just a file

You can call the stat() function and use the S_ISREG() macro on the st_mode field of the stat structure in order to determine if your path points to a regular file:

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int is_regular_file(const char *path)
{
struct stat path_stat;
stat(path, &path_stat);
return S_ISREG(path_stat.st_mode);
}

Note that there are other file types besides regular and directory, like devices, pipes, symbolic links, sockets, etc. You might want to take those into account.



Related Topics



Leave a reply



Submit