display | more...

Every so often you will find a file on a Unix system which has a very bizarre filename, consisting of one or more untypeable characters. Sometimes you can simply replace those characters with the wildcard ? which matches any one character, or replace a consecutive group of them with the * which matches zero or more of any character, but there is in fact a more reliable way to remove a single file.

I will provide examples as typed in cygwin which, for the purposes of this explanation, behaves as Unix would. (This is not true in all cases.) My example file also has a quite straightforward name, which does not invalidate the example at all.

First, do a ls -i in the directory. This will show you inode numbers for all files listed. The file we are interested in is called 'foobar'. Pretend it's got a name which is much harder (perhaps impossible) to type.

$ ls -i
 258452 TeXmacs-fonts-1.0/  62444571 foobar   191011 texmacs/

Now we know the inode number, 62444571. You can remove (or do something else to) files by inode using the find command. Find has a flag called -exec which will execute a command against all files found. For this purpose is it used thusly:

$ find . -inum 62444571 -exec rm '{}' \;

What does this mean? We can find out by looking at the synopsis in the manpage for find:

SYNOPSIS
       find [path...] [expression]

Our path is ".", or the current directory. Using the -inum flag in our expression means "find files with this inode number". Our expression uses the -exec flag to execute what comes after it. The token {} is replaced with the escaped name of the file which we have found, and the ; token ends the exec statement.

Note that we have enclosed the {} token in ticks, or single quotes. This is because the { and } symbols have special meaning to the shell. Specifically, in the bourne shell and its derivatives they are used to group commands or functions into a single function. We have also escaped the semicolon using the backslash, because semicolon is used to separate commands or functions which are on the same line as if they were on separate lines. The two are frequently used together.

Having deleted our file (ostensibly) we do an ls in the directory to list its contents and ensure that it has gone away.

$ ls
TeXmacs-fonts-1.0/  texmacs/

The file is gone! Had this been a real emergency, it would be over now.

An alternate method of removing these files is to use rm -i which asks you interactively which files you would like to delete. Hence in a directory with a small number of files it is possible to say "rm -i *" and rm will ask you which files you would like to remove. In a directory with a larger number of files, it is best to use the above method, which involves a more complicated command line, but less hitting of y or n followed by return.


References:

  1. Manual Page: find(1) from Cygwin.
  2. Chatterbox Message from mkb suggesting rm -i

Log in or register to write something here or to contact authors.