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

For organizational purposes I would like a certain filesystem object which most of the time appears as a file, on occasion, to appear as a directory.

The basic idea is that generically for cp, mv, find, updatedb/locate, I would like the object to appear as an individual file. For other applications such as vlc, rythmbox, feh, and okular, I would like it to appear as a directory. Can someone suggest a mechanism for doing this?

To illustrate let me give an example:

I will give the object I have in mind the name _my_books_on_m4_programming.arch_. For the purposes of moving/copying or find/locate this object looks like a file, but when I want to read a book in the object it looks like a directory ie: for the command okular my_books_on_m4_programming.arch/beginner/m4_for_dummies.pdf.

I have thought of two and a half possible solutions but would need help with either one.

  • The first is to modify my linux to use a special attribute of the file system. If the attribute is set then I look for an environmental for example SPECIAL_OBJECT_IS_FILE. If it is set I treat it as a file. If it is not set I treat it as a directory. The problem with this approach is that I do not see how to implement it without modifying virtually the whole system.

  • The second technique would be to store the files in an archive (e.g. tar, zip, rar) which I mount (rw) to some directory. To treat the object as an archive refer to the original archive, to treat the object as a directory refer to the mount point. Is this possible?

  • The half technique is simply a twist on the second instead of mounting an archive I create someking of link to it. Is anything like this possible?

I can't believe that no-one else ever wanted to do a similar thing so I suspect that there is some way. Suggestionss?

Ok. I confess I had trouble coming up with a title for my question, so I tried to make a joke out of a very old commercial--sorry.

share|improve this question
Don't know if it fits your situation but if you alias cp='cp -r' you can copy and move everything like a file (mv moves directories just like files), while opening them will be like directories. – phunehehe Jul 22 '11 at 7:34
Is FUSE out of the question? – Tim Post Jul 22 '11 at 13:13
Please don't listen to phunehehe. Aliasing things like that is a bad habit. – user606723 Jul 22 '11 at 14:16
On unix, everything is a file, especially a directory is a file, so what doesn't work with your directory? I don't have problems in copying directories, finding them and so on. – user unknown Jul 22 '11 at 15:24
Without attempting to answer your particular question I'd suggest that Mac OS X .app share many of the properties you describe. I don't know if support is included in Darwin, but you could have a look...maybe the hard work has already been done for you. – dmckee Jul 23 '11 at 0:23

I don't quite understand why you don't want the files to appear as individual files in some circumstances. After all, if my_books_on_m4_programming.arch is a directory, you can still copy it with cp -rp, locate it with locate and so on.

FUSE (Filesystem in Userspace) provides a way to implement lightweight filesystems, and there are many FUSE filesystems that provide alternate views of existing files. Making the same file (as in, the same file path) appear as both a directory and a regular file would confuse many applications, but there are many existing solutions to make the same file appear as a directory in one location and as a file in another location.

In particular, AVFS makes archives and remote files appear as directories. I think it's the closest existing thing to what you're looking for.

$ mountavfs
$ ls my_books_on_m4_programming* 
$ tar tzf my_books_on_m4_programming.tgz 
$ ls ~/.avfs$PWD/my_books_on_m4_programming.tgz\#/
$ okular ~/.avfs$PWD/my_books_on_m4_programming.tgz\#/beginner/m4_for_dummies.pdf
share|improve this answer

You could consider using loopback filesystems.

First, create a file to contain your filesystem:

dd if=/dev/zero of=~/myfilesys bs=8K count=256

This will create an empty 2GB file. Next, you need to attach it to a loopback device:

losetup /dev/loop0 ~/myfilesys

Now, create a filesystem:

mkfs -t ext3 /dev/loop0

Finally, mount your filesystem:

mkdir ~/myfilesys.d
mount /dev/loop0 ~/myfilesys.d

When you're finished:

umount /dev/loop0
losetup -d /dev/loop0

And to re-use later:

losetup /dev/loop0 ~/myfilesys
mount /dev/loop0 /mnt/myfilesys.d

(Yes, I know you can combine the losetup and mount steps, but for illustrative purposes it's clearer to see what's going on by splitting them out).

Now, the ~/myfilesys file will look and act like a file for all commands, but when mounted, you can see the files within this filesystem in ~/myfilesys.d.

share|improve this answer
WIll this approach allow the filesystems to grow? In your example if eventually you need myfilesys.d to go up to 3 will this allow it or will you have make a new loopback fs and copy it over? – HandyGandy Jul 23 '11 at 2:27
As I've shown above, no - when you reach the defined filesize your filesystem will be 'full', however you can create the file as a 'sparse' file with dd and make it much larger. If you do this, only the actual space used is occupied on-disk even though the file appears much larger. You can even 'overcommit' your disk in this way, for example creating 10 300GB sparse files on a 1TB disk. The main catch is that you must remember to treat these files as sparse files when copying / moving them etc. – Mike Insch Jul 24 '11 at 13:51

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.