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$ ./lister.sh > dump/sys.md

    cd /PATH-TO-SDCARD-FILES/sys/
        
        for i in *
    
        do
            echo "**$i - v$(date +%D)**"
            echo "\`\`\`g-code"
            more "$i"
            echo 
            echo "\`\`\`"
        done
    

    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: https://github.com/rkolbi/RRF-machine-config-files/blob/master/Prusa MK3s/ALLFiles.md

    Cheers,
    Kolbi



  • 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/dump.md
    /Users/me/Desktop/dump-list.md (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.

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

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

    with

    echo "\`\`\`" >> /Users/me/Desktop/dump.md
    

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


Log in to reply