Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
rm -f test && touch test except this, I really don't know – Kiwy Mar 4 '14 at 14:23
@Kiwy Yeah, but I am imagining a built-in command that is designed to do this task. – Ionică Bizău Mar 4 '14 at 14:24
up vote 8 down vote accepted

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.

share|improve this answer
Interesting. If nobody comes with a solution I will mark this answer. :-) – Ionică Bizău Mar 4 '14 at 14:54

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.

share|improve this answer

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).

share|improve this answer
@Stephane Chazelas What does the -- do ? – X Tian Mar 4 '14 at 18:31
the -- marks the end of the flags so rmdir and touch know anything past it is a filename, even if it starts with -. – Kevin Mar 4 '14 at 18:54

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:


sudo chmod 700 /


sudo chmod 700 file

Restart the system and regain terminal access and execute:

sudo chmod 744 /


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|improve this answer
Interesting approach... – eyoung100 Nov 20 '14 at 22:26

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


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...

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.