command line fun – finding mistakes

I knew that I had mistyped but it was too late.  I tried to exit out of vi in a hurry and accidentally exited but saved the file under a new name.  This wouldn’t have been so bad if the new name didn’t consist of a single control character.

My fears were confirmed when I did a directory listing.  Not only did it make an annoying sound but you could see in the file listing that the files were no longer lined up.

This isn’t the first time that my fingers got the better of me, so I simply ran the ls command and asked that it display the inode number of each of the files.  Normally that would have been fine but my control character, perhaps ^H, was actually removing the previous character displayed.

This means that my inode number was any one of ten possible values.  There are other ways to try and determine exactly what inode number a file is but before trying that I simply did a long listing using ls.

This actually did show that my inode number was 540.  I am actually a bit surprised that you cannot use this information directly to delete the file with the rm command (on Solaris, also on Linux?)

This information however can be used in conjunction with both the find and rm commands to delete this tiny mistake.

Instead of the familiar -name option for the find command there is a somewhat less used   -inum option.  As this option name implies, it will look only for files that have this inode number.

Once it is possible to find the actual file it is a simple matter of using the -exec option of find to remove the file.

find . -inum 540 -exec rm {} \;

Extra credit method

There is another way to find an inode number using just variations of the find command.

find . -name “*”  -ls

This will just do a directory listing of the files under the current working directory and run a similar directory listing at the same time.  Deleting the file is done using the same find and remove as described above.

This entry was posted in programming and tagged , . Bookmark the permalink.