Skip to content

Instantly share code, notes, and snippets.

@jangxx
Last active January 8, 2020 12:59
Show Gist options
  • Save jangxx/b292ed317007636a720e748faa0e5579 to your computer and use it in GitHub Desktop.
Save jangxx/b292ed317007636a720e748faa0e5579 to your computer and use it in GitHub Desktop.
A small bash script which outputs the contents of a directory as a tree wrapped in LaTeX itemize environments
#!/bin/bash
if [ -z "$1" ]; then
cat <<HELP_USAGE
$(basename $0) <directory>
Prints a tree of all files in the specified directory wrapped in a LaTeX itemize environment.
HELP_USAGE
exit
fi
function recurse_dir {
dir="$1"
echo "\begin{itemize}"
for file in $(find "$dir" -mindepth 1 -maxdepth 1); do
name=$(basename $file)
#escape filenames for LaTeX
name="${name//&/\\&}"
name="${name//\%/\\%}"
name="${name//$/\\$}"
name="${name//\#/\\#}"
name="${name//_/\\_}"
name="${name//\{/\\\{}"
name="${name//\}/\\\}}"
echo "\item $name"
if [ -d "${file}" ]; then
recurse_dir "$file"
fi
done
echo "\end{itemize}"
}
recurse_dir "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment