136dc [Solved] Edit a directory - The FreeBSD Forums
The FreeBSD Forums  

Go Back   The FreeBSD Forums > Base System > General

General General questions about the FreeBSD operating system. Ask here if your question does not fit elsewhere.

Reply
 
Thread Tools Display Modes
  #1  
Old July 18th, 2012, 17:10
hurricane_sh hurricane_sh is offline
Junior Member
 
Join Date: Mar 2010
Posts: 43
Thanks: 17
Thanked 0 Times in 0 Posts
Default 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?
Reply With Quote
  #2  
Old July 18th, 2012, 17:54
Beastie Beastie is offline
Senior Member
 
Join Date: Mar 2009
Location: /dev/earth0
Posts: 1,700
Thanks: 0
Thanked 301 Times in 245 Posts
Default

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.
__________________
May the source be with you!
Reply With Quote
  #3  
Old July 18th, 2012, 18:06
hurricane_sh hurricane_sh is offline
Junior Member
 
Join Date: Mar 2010
Posts: 43
Thanks: 17
Thanked 0 Times in 0 Posts
Default

Never got the warning. Did this many times, the only step I haven't done is saving.
Reply With Quote
  #4  
Old July 18th, 2012, 19:06
anomie's Avatar
anomie anomie is offline
Member
 
Join Date: Nov 2008
Location: Texas
Posts: 783
Thanks: 49
Thanked 113 Times in 100 Posts
Default

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.
__________________
"Do Not Engage in Useless Activity" --Miyamoto Musashi
Reply With Quote
  #5  
Old July 18th, 2012, 19:54
wblock@'s Avatar
wblock@ wblock@ is offline
Moderator
 
Join Date: Sep 2009
Location: Milky Way galaxy
Posts: 7,701
Thanks: 429
Thanked 1,757 Times in 1,456 Posts
Default

Or write a sh(1) wrapper that only runs the editor when the argument is a plain file.
Reply With Quote
  #6  
Old July 19th, 2012, 02:53
hurricane_sh hurricane_sh is offline
Junior Member
 
Join Date: Mar 2010
Posts: 43
Thanks: 17
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by anomie View Post
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.

Quote:
Originally Posted by wblock@ View Post
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!
Reply With Quote
  #7  
Old July 19th, 2012, 11:11
fluca1978 fluca1978 is offline
Member
 
Join Date: May 2010
Posts: 661
Thanks: 27
Thanked 60 Times in 57 Posts
Default

Well, vi(1) refuses to save the directory, other editors like emacs(1) automatically open an exploration mode that is read only.
Reply With Quote
  #8  
Old July 19th, 2012, 13:00
hurricane_sh hurricane_sh is offline
Junior Member
 
Join Date: Mar 2010
Posts: 43
Thanks: 17
Thanked 0 Times in 0 Posts
Default

Thanks for the info, fluca1978, that's a relief.
Reply With Quote
  #9  
Old July 19th, 2012, 14:35
purgatori's Avatar
purgatori purgatori is offline
Member
 
Join Date: May 2010
Location: Australia
Posts: 104
Thanks: 8
Thanked 6 Times in 4 Posts
Default

Quote:
Originally Posted by fluca1978 View Post
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.
__________________
http://sdf.org/ -- SDF Public Access Unix System, Est. 1987

http://gopher.floodgap.com/overbite/relevance.html -- Why is Gopher Still Relevant?
Reply With Quote
  #10  
Old July 22nd, 2012, 08:18
xibo xibo is offline
Member
 
Join Date: Dec 2010
Location: Germany
Posts: 300
Thanks: 13
Thanked 61 Times in 50 Posts
Default

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.

Last edited by xibo; July 22nd, 2012 at 09:25. Reason: use CODE, not QUOTE
Reply With Quote
  #11  
Old July 23rd, 2012, 07:00
fluca1978 fluca1978 is offline
Member
 
Join Date: May 2010
Posts: 661
Thanks: 27
Thanked 60 Times in 57 Posts
Default

Quote:
Originally Posted by xibo View Post
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,
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
cannot edit in nanoBSD razi91 Installing & Upgrading 2 February 22nd, 2012 12:32
Which file do I edit? Desreguard X.Org 2 August 19th, 2011 07:20
[Solved] Can't edit my own posting! bsdmonk Off-Topic 12 April 7th, 2010 23:07
How can I edit my own Profile Picture? Sundj Off-Topic 20 March 26th, 2010 11:33
[Solved] Cannot edit /etc/rc.conf IIjkII General 8 August 12th, 2009 11:17


All times are GMT +1. The time now is 08:24.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
The mark FreeBSD is a registered trademark of The FreeBSD Foundation and is used by The FreeBSD Project with the permission of The FreeBSD Foundation.
Web protection and acceleration provided by CloudFlare
0