4

I'm reading the Unix Programming Environment book. I came across the od command. The command works for the file contents but not for the directories when I run it. However, this command seems to work on directories when demonstrated by the authors of the book.

When I run the following command :

od -c .

I get an error message that says :

od: .: read error: Is a directory
0000000

Why am I getting this error?

4 Answers 4

4

Why od -c directory Doesn't Work

A directory points to zero, one, or a bunch of files, it isn't really a file itself so it can't be processed by od or hd. Most Unix-like programs process files, not whole directories.

What to Do

You can list the contents of the directory with ls and then use the od -c or hd -b command to dump each file you want to examine. By doing them one-by-one you will know what each file contains.

(od -c file1 file2 stuffs file1 and file2 together and then dumps the whole thing. That makes it heard to find where the first one ends and the next one begins.)

You could also write a little script to give you, one by one, the filename of each of the files in a directory and then dump it in octal or hex. Here's an example:

 for f in *; do echo $f; od -c $f; done | less

for f in * assigns each file in the current directory to $f in turn; echo $f provides the name of the file on your terminal preceeding each file dump; od -c $f produces the dump; and done says to go on with the next file or finish if there aren't any more. |less says take all of the above and send the output through the "less" program which let's you look at it bit by bit rather than having everything roll off the top of the screen.

A script like this is interpreted by a program called by a "shell". When you are typing commands into the terminal you are talking to a "shell". By default, Ubuntu uses the "bash" shell.

If you would rather look at hex output you can use od -t x1 filename or hd filename. Use man od or man hd for more options.

3
  • If you want to do an octal dump of all files in and below the current directory, use find, e.g.:

    od -c `find -type f`

See man find or google for a tutorial on find for more information.

3

To be able to read directories as files, as shown in the book the Unix Programming Environment (page 51) isn't possible in recent releases of Linux.

As mentioned in this answer (When did directories stop being readable as files?) on the Unix & Linux sub-site this seems to be a break from the POSIX standard which allows directories to be read as files as long as they are opened in read-only mode.

Also relevant: How does one inspect the directory structure information of a unix/linux file?

1
od [options] [files] od --traditional [file] [[+] offset [[+] label] ]

Dump the specified files to standard output. The default is to dump in octal format, but other formats can be specified. With multiple files, concatenate them in the specified order. If no files are specified or file is -, read from standard input. With the second form, using the --traditional option, only one file can be specified.1

Read More

1Source:directory of Linux commands

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .