Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a directory exists in Unix (system call)

I am not able to find a solution to my problem online.

I would like to call a function in Unix, pass in the path of a directory, and know if it exists. opendir() returns an error if a directory does not exist, but my goal is not to actually open, check the error, close it if no error, but rather just check if a file is a directory or not.

Is there any convenient way to do that please?

like image 452
Jary Avatar asked Sep 30 '10 06:09

Jary


People also ask

How do I check if a directory exists in path?

os. path. exists() – Returns True if path or directory does exists.

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

Checking If a Directory Exists In a Bash Shell Script-h "/path/to/dir" ] && echo "Directory /path/to/dir exists." || echo "Error: Directory /path/to/dir exists but point to $(readlink -f /path/to/dir)." The cmd2 is executed if, and only if, cmd1 returns a non-zero exit status.

How do I check a directory?

Use the ls command to display the contents of a directory. The ls command writes to standard output the contents of each specified Directory or the name of each specified File, along with any other information you ask for with the flags.

How do I find the location of a directory in Unix?

Type echo $PATH at the command prompt and press ↵ Enter . This output is a list of directories where executable files are stored.


3 Answers

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.)

like image 127
Jonathan Leffler Avatar answered Oct 04 '22 03:10

Jonathan Leffler


You can make use of the stat system call by passing it the name of the directory as the first argument. If the directory exists a 0 is returned else -1 is returned and errno will be set to ENOENT

EDIT:

If the return value is 0 you would need an additional check to ensure that the argument is actually a directory and not a file/symlink/char special file/blk special file/FIFO file. You can make use of the st_mode field of the stat structure for this.

like image 25
codaddict Avatar answered Oct 04 '22 04:10

codaddict


If you don't really care about type of this filesystem object, access(name, F_OK) checks for exsistence of something with this name. If you need to be sure this is directory, use stat() and check type with S_ISDIR() macro.

like image 26
blaze Avatar answered Oct 04 '22 02:10

blaze