Adam Silva
Adam Silva

Reputation: 1047

Why do dots ("." and "..") appear when I print files from directory?

I'm printing files from two directories using C language. Here is my code:

char *list1[30], *list2[30];
int i=0, x=0;
struct dirent *ent, *ent1;

/* print all the files and directories within directory */
    while ((ent = readdir (dirSource)) != NULL) {
        list1[i] = ent->d_name; 
        i++;        
    }
    i=0;
    while((ent1 = readdir (dirDest)) != NULL) {
        list2[i] = ent1->d_name;    
        i++;
    }

    while(x != i){
        printf("Daemon - %s\n", list1[x]);
        printf("Daemon1 - %s\n", list2[x]);
        x++;
    }

I can print all the files, but everytime I print the files in a directory, the end result is this:

Daemon - . Daemon1 - . Daemon - .. Daemon1 - .. Daemon - fich5 Daemon1 - fich4 Daemon - fich3 Daemon1 - fich3

I don't understand why there are dots in the beginning. Obs.: I don't if it matters, but I'm using Ubuntu 14.04 on a pen, meaning every time I use Ubuntu, I use the trial instead of using dual boot on my pc.

Upvotes: 1

Views: 858

Answers (2)

Rmano
Rmano

Reputation: 397

Every directory in Unix has the entry . (meaning current directory) and .. (the parent directory). Give that they start with "." they are hidden files; ls normally do not show them unless you use "-a" option.

See:

[:~/tmp/lilla/uff] % ls -l
total 0
-rw-rw-r-- 1 romano romano 0 May 17 18:48 a
-rw-rw-r-- 1 romano romano 0 May 17 18:48 b
[:~/tmp/lilla/uff] % ls -la
total 8
drwxrwxr-x 2 romano romano 4096 May 17 18:48 .
drwxrwxr-x 3 romano romano 4096 May 17 18:47 ..
-rw-rw-r-- 1 romano romano    0 May 17 18:48 a
-rw-rw-r-- 1 romano romano    0 May 17 18:48 b

Upvotes: 2

Code-Apprentice
Code-Apprentice

Reputation: 83577

. and .. are two special files which are in every directory in Linux and other Unix-like systems. . represents the current directory and .. represents the parent directory.

Upvotes: 4

Related Questions