Accessing files by inode #s

Richard A. O'Keefe ok at quintus.UUCP
Fri Jan 29 18:06:22 AEST 1988


[The task is to delete a file whose name contains strange characters.]
[The original poster thought that using Inode numbers was a good idea]
[but couldn't quite see what to do next.			     ]

In article <11525 at brl-adm.ARPA>, rwelsh at cct.bbn.com (Robert J. Welsh) writes:
> If you want to get rid of files that have control characters etc in their
> name, this should work:
> 1) ls -i            to get the inode number
> 2) set `ls -i | grep <desired Inode#>`; rm $2
> This will work in the  standard  shell, 'sh', and derivatives thereof.

I'm afraid it WON'T work if the file name contains any characters in IFS.
(In particular, spaces and tabs.)  Do the following:
	$ mkdir scratch
	$ cd scratch
	$ cat </dev/null >"foo baz"
	$ ls -i
The output I got was
	20545 foo baz
	$ set `ls -i | grep 20545`
	$ echo $2
The output I got was
	foo
A subsequence
	$ rm $2
therefore tries to delete a file called "foo".

You have similar problems using wild-cards:
	$ rm fo*
reports the non-existence of "foo" and "baz".

find(1) seems to work:
	$ find . -inum ${DesiredInodeNumber} -exec rm {} \; 

Of course, in this case
	$ rm 'foo baz'
also works.  With a BSD terminal driver, you can type in any characters
at all, so you don't need special tools or hacks.  For example, if the
file you want is ^A^B^Cfoo^Ibaz^D (^X means control-X), and if the
literal-next character is ^V, just do
	$ rm '^V^A^V^B^V^Cfoo^Ibaz^V^D'

The ultimate method, of course, is to write a C program, calling unlink().



More information about the Comp.unix.questions mailing list