Transform a directory into file or a file into directory

48,257

Solution 1

This isn't possible for any Unix or Linux I've ever touched.

Under some older Unixes, directories are files, specially marked, but still files. You used to be able to read a directory with cat under SunOS, for example. A lot of modern filesystems might have directories as B+ trees, or some other on-disk data structure. So turning a file into a directory or vice versa would always require a delete and a re-creation with the same name.

Solution 2

No there is no such command because there would be absolutely no point in it. Your question is like asking for a way to transform a box into a flat rectangle. What would you do with the things in the box (directory)? How would the rectangle correspond to the box it came from?

Files and directories serve completely different functions. While a directory is indeed a file in *nix systems, that only applies to the internals of the system and not to what you, the user, see. So, converting a file to a directory, or vice versa, will always involve deleting the file and creating the directory. The best you could hope for would be to use the same inode but again, not much point in that.

Solution 3

To those wondering why you'd want to do this, follow this thread:

https://community.linksys.com/t5/Wireless-Routers/Media-files-changing-to-folder-types/td-p/504192/highlight/true

Apparently a bug in the firmware flipped the bit in the directory entry for the file, making the OS think it's a folder instead of a file.

While I don't have the Linksys router in question, something changed all my .jpg files into empty folders. If there's someway to flip the bit (or whatever) that says an NTFS directory entry is a folder vs a file, I'd like to know about it.

One of the solutions in that 2-year long thread said if the hard drive containing the folders-that-used-to-be-files is connected to a Mac running OS/X, the Mac will see them as files again, and they can be copied to a folder on the Mac. I don't have a Mac to try that out, so if anyone knows a way to flip the bit...

Solution 4

Create a function to do what you want. (bash or other Bourne/POSIX like shell assumed)

$ dir2file() { rmdir -- "$1" && touch -- "$1";}
$ mkdir adir
$ dir2file adir
$ ls -l adir
-rw-r--r-- 1 xtian xtian 0 2014-03-04 14:59 adir
$ 

I deliberately use rmdir which will fail if adir is not a directory or not empty (no -f to force). touch will run only if rmdir succeeds.

You could put the function definition in your shell customisation file (.bashrc for bash).

Solution 5

First, gain access to the terminal and execute ls -al file with file being the file you want to convert back into an ACTUAL folder. If there are files and sub-directories listed it means your "file" was previously a folder with real content and you may continue:

Execute:

sudo chmod 700 /

OR

sudo chmod 700 file

Restart the system and regain terminal access and execute:

sudo chmod 744 /

OR

sudo chmod 744 file

REMEMBER: "file" should be replaced by file you want to convert back into an ACTUAL folder.

This is just an arbitrary way to solve this problem but it creates permission anomalies which you can either use fsck -f to fix (while un-mounted of course) or manually set new permissions as needed.

AGAIN, this is just a proven way to avoid having to delete the file that was previously a non-empty folder.

Share:
48,257
Ionică Bizău
Author by

Ionică Bizău

💻 Programmer ⚡️ Geek 🎹 Pianist & Organist 🚀 Learner 💡 Mentor 💫 Dreamer 🍏 Vegetarian 🙏 Jesus follower Website | GitHub | Twitter | PayPal Donations

Updated on September 18, 2022

Comments

  • Ionică Bizău
    Ionică Bizău over 1 year

    I wanted to create a file named test. Accidentally I run mkdir test instead of touch test.

    Is it possible to convert test directory in a file named test?

    What about converting a file named test into a directory with the same name?

    • S edwards
      S edwards about 10 years
      rm -f test && touch test except this, I really don't know
    • Ionică Bizău
      Ionică Bizău about 10 years
      @Kiwy Yeah, but I am imagining a built-in command that is designed to do this task.
  • Ionică Bizău
    Ionică Bizău about 10 years
    Interesting. If nobody comes with a solution I will mark this answer. :-)
  • X Tian
    X Tian about 10 years
    @Stephane Chazelas What does the -- do ?
  • Kevin
    Kevin about 10 years
    the -- marks the end of the flags so rmdir and touch know anything past it is a filename, even if it starts with -.
  • eyoung100
    eyoung100 over 9 years
    Interesting approach...
  • Sridhar Sarnobat
    Sridhar Sarnobat almost 5 years
    Saying there is no point to it is your opinion, not a texchnical fact.
  • terdon
    terdon almost 5 years
    @user7000 what would you do with a directory's contents when you "convert" to a file?
  • Sridhar Sarnobat
    Sridhar Sarnobat almost 5 years
    A file system stores data in a tree structure. In computer science, tree nodes that are leaf nodes can become internal nodes when you add children to them. And no, I’m not going to use a database when a file system suffices.