2

I am using readdir() in Ubuntu to display files and directories. The weird thing is readdir() displays some files starting with "dot", and some that end at ~ . But these files are not in my specified directory.

What are these files?

I was wondering when reading names of files, will these weird files will also be mentioned by d_name or not?

enter image description here

Regards

Naruto
  • 1,710
  • 7
  • 28
  • 39

2 Answers2

3

readdir reads all files present within the folder, while ls only list non-hidden files. Try to list your files with ls -a, and you will see those files.

tomahh
  • 13,441
  • 3
  • 49
  • 70
  • but I am planning to write a program which finds files in directories and mentions their path. Is there a way to ignore them. – Naruto Oct 21 '12 at 13:08
  • 1
    Simply add a condition testing if the file you are about to print starts with a dot or not. – tomahh Oct 21 '12 at 13:08
  • 2
    @UmerFarooq `if (filename[0] != '.' && filename[strlen(filename) - 1] != '~')` –  Oct 21 '12 at 13:09
  • I thought there were some kind of system call. Anyhow I am glad i was thinking the same to put a conditional statment. – Naruto Oct 21 '12 at 13:41
1

By convention, files whose names start with a dot are hidden in Unix-like operating systems (see here).

You can of course check for the dot at the beginning of the filenames produced by readdir, and simply not return/output those.

Vlad
  • 18,195
  • 4
  • 41
  • 71
  • The shell with glob expansion and `ls` agree to hide them by not including them in `*.*` expansions or the default output of a directory. There's no other sense in which they're hidden, and `readdir()` would be useless if it did not return all the names. One possible alternative design would have two variants such as `readdir()` and `readdir_hidden()` so that you could really see everything when you need to but not by default. But minimalism in the interface indicates that the current design is better. – Jonathan Leffler Oct 21 '12 at 15:06