Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

Ok. After trying to formulate this question a couple of times, it turns out that this will be a long one. Anywho:

Consider this file structure:


if you do

cd photos

you get

ants/    animals/    profile_pic.jpg

However since the directories vacation and animals only contain one file each, it could arguably be more useful to get

ants/pic1.jpg    ani../dog.png    profile_pic.jpg

directly, as opposed to having to visit each of animals and vacation. On tab completion however, as in cd a you would want suggestions of the directories instead.

So I know bash completion is handled by the /etc/bash_completion script (I don't wanna go there) and the readline library, but this doesn't really affect ls and certainly doesn't help if you want to extend functionality to file managers such a nautilus. Therefore I was hoping some of this could be tackled using a custom filesystem over fuse. A initial investigation based on this tutorial was somewhat depressing though, as the fuse file system seems to in no way differentiate between my different use cases (readdir, opendir, releasedir every time).

To formulate a question: In what way could a similar functionality be approached, if it's even possible without major revision in the kernel? Where should I look for more information?

Thanks for you time!

EDIT: To clarify, bash is a useful example, but in the end I want this to be the same through nautilus and every other program accessing the files, and I'm gonna write a file system anyway for other issues.

share|improve this question
This will obviously break some programs. – BatchyX Nov 9 '12 at 20:58
Some things are better left in user space, such as ls -R, or find. How do you know someone else doesn't want to see only the directoryies? – Keith Nov 9 '12 at 21:45
@BatchyX You are of course right. There will definitely be issues. – fickludd Nov 10 '12 at 14:11
@Keith So the problem I'm experiencing is that the FUSE API doesn't seem to support reading subsets of a directory. The entire thing is always read, and filtering is done by the program. This is robust, but limits what you can do with a fuse-based file system. Don't missunderstand: the design is good for it's intended usage, but I'm almost outside this usage =). – fickludd Nov 10 '12 at 14:29

The easiest way to accomplish this would be to write a bash script to do this for you. I'm fairly certain you are not going to want to code this into the Kernel or Filesystem :)

Logic like this should work:

Descend into directories... Check if one file ... print folder/file... else print top folder

share|improve this answer
I see what you mean, but this is not actually functionality that I want only in bash, but globally. Clarified the question a bit. – fickludd Nov 9 '12 at 20:47

Unfortunately, I cannot post a comment, so I have to write an "answer" (I still don't understand this decision, but enough of the ranting). Since you've already discarded the bashoption and hinted towards a FUSEsolution, I'd like to give you a couple of "more or less viable" ideas while you implement your mp3fs:

  • Enhancing struct stat with a new type, let's say st_nfiles. Each time you create()/symlink()/hardlink() a file, you will be updating this entry in the respective FUSE operations. Figuring out if there is only one entry in the referred directory, will then merely be a lookup and comparison of (struct stat) dirp->st_nfiles with 1. You would need to LD_PRELOAD a library to your user space that hijacks and extends the necessary printf calls to output the stat results with this change.

  • Misusing the existing (struct stat) dirp->st_nlink in your FUSE operations code of getattr(). The drawback could be issues with tools that depend on st_nlink; if I remember correctly, findis one of them. This also needs a global LD_PRELOAD library to fiddle with the output.

  • On the fly parsing in the FUSE operations code of getattr(), similar to what you would do in pure userspace. This code snippet should give you the idea.

Not sure if this is want you wanted. I wished I could have added this as a comment, rather than a half-backed answer. Maybe you want to remove the bash tag from this question, since you've indicated that this won't suitable for you, since you want tools like nautilus to behave the same way.

share|improve this answer
Thanks a lot, now I'm learning stuff again =). I wasn't actually familiar with the LD_PRELOAD trick (not frequent c user), but that might prove really useful. In the end the problem still seems to be that when doing cd i want the directory from readdir, but when doing ls listing i want the file. Tricky! – fickludd Nov 11 '12 at 19: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.