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 routinely write my notes in markdown in a complex directory structure like this.


This directory structure is ever expanding and each directory has various other files apart from the *.md files. I have a shell script that recursively looks for markdown files in the parent directory doc/ and generates html and pdf files using pandoc. I also create a doc.md file (through the script) which contains a list of all the *.md files. Presently I use,

find ~/doc/**/*.md

to generate this list. I would like to sort them according to the directory structure for easy perusal like this:


If fp=/home/user/doc/linux/tmux/tmux.md is an example full path of a file, then ${${fp%/*.md}#/home/user/doc/} gives me linux/tmux. I can further process it to isolate linux and tmux etc. But I don't know the algorithm to create the directory structure. I think that this will require use of arrays to store the complete directory structure for each file and then sorting it.

If a shell script already exists to to this, please let me know. Otherwise, pointers on possible algorithm are welcome.

share|improve this question
up vote 7 down vote accepted

If all you are looking to do is produce a nice formatted output of all .md files, tree should do exactly what you want:

tree -P '*.md' /home/user/doc

Add -A for pretty lines, it also does interesting things like output to HTML/XML.

share|improve this answer
I used tree --prune -f -R -P "*.md" -H file:///home/user/doc /home/user/doc -o ~/list.html. Thank you. – devendra Mar 1 '14 at 15:13

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.