# Edit a directory



## hurricane_sh (Jul 18, 2012)

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 

```
#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?


----------



## Beastie (Jul 18, 2012)

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:

```
Warning: <some directory> is not a regular file
```

And when "saving":

```
Error: <some directory>: Is a directory.
```


----------



## hurricane_sh (Jul 18, 2012)

Never got the warning. Did this many times, the only step I haven't done is saving.


----------



## anomie (Jul 18, 2012)

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.


----------



## wblock@ (Jul 18, 2012)

Or write a sh(1) wrapper that only runs the editor when the argument is a plain file.


----------



## hurricane_sh (Jul 19, 2012)

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ã€€ï½ˆï½ï½–ï½…ã€€ï½•ï½“ï½…ï½„ï¼Ž



			
				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 (Jul 19, 2012)

Well, vi(1) refuses to save the directory, other editors like emacs(1) automatically open an exploration mode that is read only.


----------



## hurricane_sh (Jul 19, 2012)

Thanks for the info, fluca1978, that's a relief.


----------



## purgatori (Jul 19, 2012)

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.


----------



## xibo (Jul 22, 2012)

I always thought directories could not be written with normal write operations.

open(2), ERRORS section:

```
[EISDIR]           The named file is a directory, and the arguments specâ€
                   ify it is to be modified.
```


----------



## fluca1978 (Jul 23, 2012)

xibo said:
			
		

> I always thought directories could not be written with normal write operations.
> 
> open(2), ERRORS section:
> 
> ...



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,


----------

