Bash script, all files in directory - formatted text dump

  • I've made a very simple bash script that outputs the contents of each file within the designated directory to terminal. This can be useful for troubleshooting, code verification, sharing, ...
    To put output to a file, follow this example: me:~ me$ ./ > dump/

        for i in *
            echo "**$i - v$(date +%D)**"
            echo "\`\`\`g-code"
            more "$i"
            echo "\`\`\`"

    You can also copy the text contents, paste in the forum and have it nicely displayed in easy to read code blocks. An example output from my printer can be found here: MK3s/


  • Updated the script a bit, kinda messy but it works well 😉
    On my Mac, I place it in ~/bin/ as 'lister'

    Use from cli:
    me:~ me$ lister /Users/me/rfm/backup-dir

    Where '/Users/me/rfm/backup-dir' is the location that my target files reside.
    In this script, I set the results to be placed on my Desktop as:
    /Users/me/Desktop/ (includes the complete list of directorys and file names before presenting the contents)

    You can see a working example here on the last page - or here: dump-list.pdf where I just exported it to pdf.

    echo "### file dump - v$(date +%D)" > /Users/me/Desktop/
    echo "### Directory / File list follow:" >> /Users/me/Desktop/
    echo "" > /Users/me/Desktop/
    print_file_recursively() {
       FILE_NAME="$(basename "${entry}")"
       echo "$FILE_NAME  " >> /Users/me/Desktop/
            echo "##### ${entry#"$strip"}" >> /Users/me/Desktop/
            echo "\`\`\`g-code" >> /Users/me/Desktop/
            more "$entry" >> /Users/me/Desktop/
            echo >> /Users/me/Desktop/
            echo "\`\`\`" >> /Users/me/Desktop/
    # loop and print all file and folder recursively,
    print_recursively() {
       local indent="${2:-0}"
       echo "**${1#"$strip"}**  " >> /Users/me/Desktop/   
       echo "#### ${1#"$strip"}" >> /Users/me/Desktop/
    for entry in "$1"/*; do
       [[ -f "$entry" ]] && print_file_recursively
    for entry in "$1"/*; do
       [[ -d "$entry" ]] && print_recursively "$entry"
    # Check folder
    if [ -d "$1" ]; then
    print_recursively $PATH_FOLDER
    echo "" >> /Users/me/Desktop/
    echo "" >> /Users/me/Desktop/
    echo "### File contents follow:" >> /Users/me/Desktop/
    echo "****" >> /Users/me/Desktop/
    echo "" >> /Users/me/Desktop/
    more /Users/me/Desktop/ >> /Users/me/Desktop/
    echo "Done." 

  • Forgot to mention, if you are going to use the output for use in a code block within this forum, you'll need to do either of two things...

    echo "\`\`\`g-code" >> /Users/me/Desktop/


    echo "\`\`\`" >> /Users/me/Desktop/

    Open the output file in a text editor, and do a search/replace; search for ```g-code and replace with ```

Log in to reply