I have a C program in Linux, which collects directory entries (of type 'struct dirent*') from a struct DIR and displays them. Now, my goal is to recursively traverse through the file system so I need to identify Directories and Files seperately.
DIR* cwd = opendir(".");
struct dirent* directoryEntry = NULL;
while((directoryEntry = readdir(cwd)) != NULL) {
printf("%s ", directoryEntry->d_name);
}
closedir(cwd);
I know you could say that we can identify files if there is some extension ".mp4" or ".txt" at the end. But files without any extension may also exist.
Is there any attribute of struct dirent* which tells whether it is a file or a directory?
DT_DIR
here: man7.org/linux/man-pages/man3/readdir.3.html – Jabberwockyd_type
, fall back onlstat()
– Shawnftw()
might also be of interest. – Shawn