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?
Why
od -c directory
Doesn't WorkA 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
orhd
. 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 theod -c
orhd -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 *
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; anddone
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
orhd filename
. Useman od
orman hd
for more options.If you want to do an octal dump of all files in and below the current directory, use
find
, e.g.:See
man find
or google for a tutorial on find for more information.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?
Read More
1Source:directory of Linux commands