file:list_dir/1 cannot lists dot(.) and dot-dot(..) directories

5 views
Skip to first unread message

Yao Bao

unread,
Jun 29, 2021, 12:11:24 AM6/29/21
to erlang-questions@erlang.org Questions
Hello,

I'm trying to list all files in a directory, then I found file:list_dir/1, it works fine but dot(.) and dot-dot(..) is missing.

I would expect the result from file:list_dir/1 in Erlang is the same as readdir(dirp) in C.

Is there any (historical) reason why these two special directories missing from the return?

Cheers,
Yao

Vimal Kumar

unread,
Jun 29, 2021, 2:10:09 AM6/29/21
to Yao Bao, Erlang (E-mail)
Hi,

I am not sure if any programming language (let alone Erlang) does that while listing folder contents. The . and .. are mere references to current folder and the parent folder by the OS, they does not exist in real if I am not wrong.

Vimal

Nicolas Martyanoff

unread,
Jun 29, 2021, 2:31:54 AM6/29/21
to Vimal Kumar, Erlang (E-mail)
Vimal Kumar <vima...@gmail.com> writes:

> I am not sure if any programming language (let alone Erlang) does that
> while listing folder contents. The . and .. are mere references to current
> folder and the parent folder by the OS, they does not exist in real if I am
> not wrong.

Other programming languages handle it as expected, e.g. in Ruby:

Dir.entries(".")

On UNIX, . and .. are indeed no real files, but they behave as
hardlinks. Not being able to use them for example to list the content of a
directory is indeed a bug.

--
Nicolas Martyanoff
http://snowsyn.net
kha...@gmail.com

Ivan Uemlianin

unread,
Jun 29, 2021, 2:57:43 AM6/29/21
to erlang-q...@erlang.org
Python doesn't include '.' or '..' either:

>>> import os
>>> '.' in os.listdir()
False
>>> '..' in os.listdir()
False

Although you are able use them for example to list the content of a
directory:

>>> x = os.listdir()
>>> os.chdir('..')
>>> x == os.listdir()
False

Erlang's behaviour is similar:

1> {ok, Fs} = file:list_dir(D).
{ok,[<filenames>]}
2> lists:member(".", Fs).
false
3> lists:member("..", Fs).
false

4> file:list_dir("..").
{ok,[<filenames>]}

Ivan
============================================================
Ivan A. Uemlianin PhD
Llaisdy

Ymchwil a Datblygu Technoleg Lleferydd
Speech Technology Research and Development

iv...@llaisdy.com
@llaisdy
llaisdy.wordpress.com
github.com/llaisdy
www.linkedin.com/in/ivanuemlianin

festina lente
============================================================

Nicolas Martyanoff

unread,
Jun 29, 2021, 3:09:13 AM6/29/21
to Ivan Uemlianin, erlang-q...@erlang.org
Ivan Uemlianin <iv...@llaisdy.com> writes:

> Python doesn't include '.' or '..' either:
>
>>>> import os
>>>> '.' in os.listdir()
> False
>>>> '..' in os.listdir()
> False
>
> Although you are able use them for example to list the content of a directory:
>
>>>> x = os.listdir()
>>>> os.chdir('..')
>>>> x == os.listdir()
> False
>
> Erlang's behaviour is similar:
>
> 1> {ok, Fs} = file:list_dir(D).
> {ok,[<filenames>]}
> 2> lists:member(".", Fs).
> false
> 3> lists:member("..", Fs).
> false
>
> 4> file:list_dir("..").
> {ok,[<filenames>]}
>

Ah yes, my bad; . and .. should be usable as any other file, but
including them when listing directories is a matter of preference.

die...@schoen.or.at

unread,
Jun 29, 2021, 4:35:43 AM6/29/21
to Erlang-Questions Questions
Maybe there is a philosophical reason:
A process always has a current directory (maybe except some zombies).
It is a bit like "cogito, ergo sum.". If you accept this, then it is a trivial fact that you have to be somewhere,
the short form of which is ".". There is no more reason to state it every time.

".." is a similar item. Every directory has a parent (well, nearly every directory), 
so there is also no need to include that in the results.

More important is that one can use these shortcuts in functions like file:list_dir()/1.


Dieter

Attila Rajmund Nohl

unread,
Jun 29, 2021, 6:50:08 AM6/29/21
to erlang-questions@erlang.org Questions
I guess it's pretty rare that you actually want to do something with
the "." and ".." directory entries, so it makes sense to not return
them... For example, in Perl by default the read_dir function in the
File::Slurp module does not return the "." and ".." entries (there's
an option to return these).

Yao Bao

unread,
Jul 1, 2021, 1:39:00 AM7/1/21
to erlang-questions@erlang.org Questions
Hello,

By exploring the source code, I found file:list_dir/1 is implemented as a NIF, which is implemented in C.
And, under UNIX, it uses opendir() and readdir() to get the directory entries, dot(.) and dot-dot(..) is ignored by design.

The specified source code is here (efile_list_dir and is_ignored_name):

But no comment saying why dot(.) and dot-dot(..) is ignored.

Cheers,
Yao
Reply all
Reply to author
Forward
0 new messages