Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore hidden files with opendir and readdir in C library

Tags:

c

unix

file-io

Here is some simple code:

DIR* pd = opendir(xxxx);
struct dirent *cur;
while (cur = readdir(pd)) puts(cur->d_name);

What I get is kind of messy: including dot (.), dot-dot (..) and file names that end with ~.

I want to do exactly the same thing as the command ls. How do I fix this, please?

like image 569
fwoncn Avatar asked May 10 '09 15:05

fwoncn


People also ask

What command will list all hidden and non hidden files and folders in the current directory answer should include the full command?

The "ls" command has many options that, when passed, affect the output. For example, the "-a" option will show all files and folders, including hidden ones.

What command should we use to show the all hidden directory contents?

To show hidden files, you need to include the /a:h modifier in that command. So, dir /a:h C:your-folder will do the trick. CMD also has specific commands for showing directories and folders. /a:d shows all hidden directories, and /a shows hidden folders.

Which answer can be used with is command in order to display hidden files?

The dir command is a command close to the ls command on Linux : it displays directory contents on your system. Similarly to the ls command, it can be used in order to show hidden files in a directory.


1 Answers

This is normal. If you do ls -a (which shows all files, ls -A will show all files except for . and ..), you will see the same output.

. is a link referring to the directory it is in: foo/bar/. is the same thing is foo/bar.

.. is a link referring to the parent directory of the directory it is in: foo/bar/.. is the same thing as foo.

Any other files beginning with . are hidden files (by convention, it is not really enforced by anything; this is different from Windows, where there is a real, official hidden attribute). Files ending with ~ are probably backup files created by your text editor (again, this is convention, these really could be anything).

If you don't want to show these types of files, you have to explicitly check for them and ignore them.

like image 166
Zifre Avatar answered Nov 18 '22 00:11

Zifre