As we know "Everything in Linux" is a file, and moreover directory is just a file containing other files.
So, I don't know if this "crazy idea" possible, but it should be in someway according to the above principle.
In simple words, how could I change an existing empty file into a directory. Is it possible?
As some brain storm I thought a some modification in file metadata and make it as directory metadata should make it!!
Any information is appreciated.
UPDATE: For sure I don't want to delete a file and create the dir instead! I'm just trying to know how much the philosophy above is applicable if you can play with some file metadata.
Best Answer
Achieving the conversion
Creating a test filesystem
In order to preserve our main filesystem from any possible damage after running this experiment, we're going to create a small filesystem inside a normal file for test purposes.
Create a zero-filled file called
test
with a size of 10 megabytes:Create an Ext4 filesystem inside the file, as if it were a partition:
Creating some files and directories
Now we have a fully functional filesystem inside the
test
file, so we're going to create some files and directories inside it.Mount the newly created filesystem inside
/mnt
:Create a file and a directory:
Check the contents of the filesystem:
Output should be something like this:
Unmount the test filesystem:
Swapping the file and the folder
Run
debugfs
against thetest
file with write permission (-w
flag):Convert
file
into a folder:At the
debugfs
prompt, type this:A prompt will appear asking you a mode; type this:
Keep pressing return to leave the remaining data as-is until the prompt appears again.
Convert
folder
into a file:At the
debugfs
prompt, type this:A prompt will appear asking you a mode; type this:
Keep pressing return to leave the remaining data as-is until the prompt appears again.
To exit
debugfs
prompt, simply hit q and then returnChecking the success of the operation
Mount the test filesystem again:
Check the filesystem contents:
Now, it should show the file as if it were a directory and vice versa:
Script to calculate inode modes
View script on GitHub
Handicaps
Further reading
https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout#Inode_Table
Thanks to @tallus. He gave me a great hint: