Edit a directory

When I input a vi command, I often didn't finish the file name and edited a directory instead. Although I haven't saved the edit yet, it's very close for many times. It makes me very worried. So, I'm wondering what I can about it if I edited a directory accidentally. For example, if I run
Code:
#vi /var/db/mysql/
Changed the file and saved it. I think I will lose access to all mysql data, is there an easy way to get the directory back?
 
I don't think you could ever destroy the filesystem this way. And vi can't "edit directories".
All you should normally get when trying to edit a directory is this:
Code:
Warning: <some directory> is not a regular file

And when "saving":
Code:
Error: <some directory>: Is a directory.
 
What FreeBSD version? Are you using nvi or vim? Both should barf out similar warnings about this.

A directory is really just a special type of file, as you can see with:
$ od -c directory_here

Even if you somehow managed to "edit" the directory, the files would still be there, presumably accessible directly by inode. (If they didn't have any other hard links, you'd have to do some work to access them again, though.)

More to the point: use your shell's tab completion, and slow down when you're editing. Give your eyes and brain a few seconds to engage before you start typing. :)
 
anomie said:
What FreeBSD version? Are you using nvi or vim? Both should barf out similar warnings about this.
FreeBSD 8.2, the default vi command. It seems the same on all FreeBSD versions I have used.

wblock@ said:
Or write a sh(1) wrapper that only runs the editor when the argument is a plain file.
Sounds like a great idea, I will do some homework.

Many thanks for all your help!
 
fluca1978 said:
Well, vi(1) refuses to save the directory, other editors like emacs(1) automatically open an exploration mode that is read only.

It's sorta read-only. By default, Emacs' dired allows the user to carry out file/directory operations (move, copy, delete, etc.), but you have to activated wdired mode to edit the directory in the same way you'd edit a text file -- although even then, you can only operate on the filenames, and not all the permissions, filesize information, etc.
 
I always thought directories could not be written with normal write operations.

open(2), ERRORS section:
Code:
[EISDIR]           The named file is a directory, and the arguments spec‐
                   ify it is to be modified.
 
xibo said:
I always thought directories could not be written with normal write operations.

open(2), ERRORS section:
Code:
[EISDIR]           The named file is a directory, and the arguments spec‐
                   ify it is to be modified.

You are right, what happens is that programs like emacs open an interface to make chanegs not to the directory-file but to the directory structure as you would do using the command line,
 
Back
Top