Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

cat the directory?

21 views
Skip to first unread message

JaceK

unread,
Oct 25, 2016, 4:16:07 AM10/25/16
to
In books I have read so far it is usually said that directory is
actually a file with special structure. The content of such file is name
of files in directory and their inodes (maybe something more but the
books weren't that specific).

One can cat regular file to see its content. Is there a command to cat a
directory?

This is pure cognitive question. It is motivated by a book by Maurice
Bach "The design of UNIX operating system". The author says (this is
about copy command):

"The old file can also be a directory. For instance,

copy . dircontents

copies the contents of the current directory, denoted by the name ".",
to a regular file, "dircontents"; the data in the new file is identical,
byte for byte, to the contents of the directory, but the file is a
regular file."

Nowadays, is there a command to cat a directory the way the author
describes it?

JaceK

Wayne

unread,
Oct 25, 2016, 5:01:33 AM10/25/16
to
On 10/25/2016 4:16 AM, JaceK wrote:
> In books I have read so far it is usually said that directory is actually a file with
> special structure. The content of such file is name of files in directory and their
> inodes (maybe something more but the books weren't that specific).

That's correct. A directory is a two-column table, more or less.
See /usr/include/bits/dirent.h for the exact definition of
struct dirent on Linux.

>
> One can cat regular file to see its content. Is there a command to cat a directory?

I don't think Linux allows that, but I believe some Unixes do (if
you are root anyway). I know I used to be able to do that, it made
a good demo. My memory (not very good anymore) was Solaris and AIX
allowed that.

However, you can come very close with just:
\ls -i | cat

The backslash turns off aliases, and the "|cat" turns off some
terminal processing as well, such as converting non-printable
characters into question-marks. Fun demo to see the difference:

touch AB^V[BS]C

(That's control-V followed by a backspace.) When you run the
above command, you will see "AC". Without the cat, you will
see "AB?C".

--
Wayne

Christian Weisgerber

unread,
Oct 25, 2016, 11:30:09 AM10/25/16
to
On 2016-10-25, JaceK <JaceK@wu_pe.pe_el> wrote:

> One can cat regular file to see its content. Is there a command to cat a
> directory?
>
> This is pure cognitive question. It is motivated by a book by Maurice
> Bach "The design of UNIX operating system". The author says (this is
> about copy command):

When you cat a directory, you can expect one of three things to
happen:
(1) You get the binary contents of the directory.
(2) You get an immediate EOF.
(3) You get an error (EISDIR).

(1) is indeed the historical behavior. You still get this on
FreeBSD.
(2) is rare. OpenBSD used to do this up to the 6.0 release, but
recently switched to (3).
(3) is the most common behavior now, because this is no longer the
1970s or 1980s.

A directory is an opaque filesystem structure. Structured access
is available with readdir(3). The exact binary contents are neither
interesting nor useful to a user. So the command to view a directory
is... ls.

--
Christian "naddy" Weisgerber na...@mips.inka.de

Casper H.S. Dik

unread,
Oct 25, 2016, 11:43:12 AM10/25/16
to
Christian Weisgerber <na...@mips.inka.de> writes:

>A directory is an opaque filesystem structure. Structured access
>is available with readdir(3). The exact binary contents are neither
>interesting nor useful to a user. So the command to view a directory
>is... ls.

Or not even available; e.g., in the case of NFS or in
the case of ZFS

Casper
0 new messages