Reputation: 83
I currently have a big big directory filled with many sub-directories with identically named markdown files in each:
/big-directory/sub-directory-name-1/sub-directory-name-1.md
/big-directory/sub-directory-name-2/sub-directory-name-2.md
/big-directory/sub-directory-name-3/sub-directory-name-3.md
I would like to end up with this:
/big-directory/sub-directory-name-1/sub-directory-name-1.md
/big-directory/sub-directory-name-1/index.html
/big-directory/sub-directory-name-2/sub-directory-name-2.md
/big-directory/sub-directory-name-1/index.html
/big-directory/sub-directory-name-3/sub-directory-name-3.md
/big-directory/sub-directory-name-1/index.html
I'm trying to write a shell script on OS X that will run multimarkdown on each file, but I'm having difficulty figuring out how to make the loop run over the subdirectories without manually putting them all into the script. Thanks in advance for any help.
Upvotes: 2
Views: 746
Reputation: 338
I encountered the same problem. After reading this question and several others, plus a lot of trial and error, I came up with the following solution:
#!/bin/bash
for dir in `find /big-directory -type d`
do
if [ -e $dir/file.md ]; #test if dir contains file
then
echo "converting $dir/file.md to HTML..."
markdown $dir/file.md > $dir/file.html
fi
done
I know that it's not strictly necessary to check whether the file exists, but otherwise, the script will throw an error every time it finds a directory that does not contain this file.
Upvotes: 0
Reputation: 22512
To get a list of all the leaf directories below /big-directory, you can use the answer in this question.
Then, you can construct a while loop like this:
find /big-directory -type d | sort | awk '$0 !~ last {print last} {last=$0} END {print last}' | while read dir
do
# Do something useful with $dir
echo $dir
done
That should give you a starting point.
Upvotes: 2