change file to directories and vice versa

It's funny to think or say but i have a question in my mind,
is there any way to change the files to directories or vice versa by some changing the in its property of
drwxr--r-- to -rwxr--r--
if it could be please suggest
Who is Participating?
alikoankConnect With a Mentor Commented:
on some systems you can see their contents by cat command.

try typing

cat mydir > test.bin

that may or may not work on your filesystem.
brettmjohnsonConnect With a Mentor Commented:
In most filesystems directories ARE files.  Some OS's even allow you to use
open(), read(), write(), etc to access the data.  However the directory's file
contents is highly structured and specific to the file system implementation.
So using write() to modify a directory file runs a very high risk of rendering
the filesystem unusable if you don't know what you are doing.

To aid the programmer in managing that structured information, the OS
provides abstracted access using opendir(), readdir(), etc.

de2ZotjesConnect With a Mentor Commented:
On unix systems the main difference between a regular file and a directory file is that you cannot use the write-systemcall on a directory file :-)

But to get back to the question: you might be able to change the properties of the directory file so it becomes a regular file, but you would destroy a large chunk of your filesystem in the process.

I can see no good reason to do this other than satisfying you curiosity (which is a good enough reason for me :-) But before you even try, make sure the filesystem you kill holds no unique data..
decentswatiAuthor Commented:
Hi de2Zotjes,
really I think the way is to change the property of directory file to make it a regular file.give me the suggestion so that I can try it.OfCourse there is no reason or need to do that but knowing the thing will make concepts clear.Help me.

Make sure you have libext2fs on your system (ldconfig -p|grep ext2fs) It is part of the e2fsprogs collection.

After that it would be something like:
(this is pseudo-code!)

#include <ext2fs.h>

main () {
err_code = ext2fs_read_inode(fs,inode_num,&inode)
inode.i_mode = inode.i_mode | LINUX_S_IFDIR
ext2fs_write_inode(fs,inode_num, &inode)

Not even close to a working program but these are the funtions you would need to use.

Again: you most likely WILL f*ck up your complete file system! dump before you try this.
All Courses

From novice to tech pro — start learning today.