Hacker News new | ask | show | jobs
by jraph 1235 days ago
My backup script, called with ./backup, and will incrementally backup my servers on the local machine using rsync and brtfs snapshots.

The command "list" to find stuff in the current directory, recursively. Basically "find | grep". Works if you have the Python module or Java package name / path, since "." will match any character, including slashes :-)

    #!/usr/bin/env sh

    if [ "$1" = "-h" ]; then
        exec cat << EOF
    Usage: $(basename "$0")^ [--] search_string

    List paths having search_string in them.
    Note: options are passed to grep.
    EOF
    fi

    if [ "$1" = "--" ]; then
        shift
    fi

    find -type f | grep "$@" | grep -v '.pyc' | fgrep -v /target/
And "fkt", which will open the files returned by list with your preferred editor (f for find, kt for kate, because that's my editor and didn't find any fitting name for this command)

    #!/usr/bin/env sh

    editor=kate

    if [ "$FORCE_EDITOR" ]; then
            editor=$FORCE_EDITOR
    fi

    help () {
        exec cat << EOF
    Usage: $(basename "$0")^ [--] search_string

    Open files with paths having search_string in it.
    \$FORCE_EDITOR can be used to give the name of the editor to use

    current default editor: $editor (edit $0 to change)
    Note: options are passed to grep.
    EOF
    }

    if [ "$1" = "" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
        help
    fi

    if [ "$1" = "--" ]; then
        shift
    fi

    if [ "$1" = "" ]; then
        help
    fi

    list -- "$@" | while read line; do
            printf "open with ${editor}? %s [Y/n/q]: " "$line"
            read confirm < /dev/tty
            ok=""
            while [ "$ok" = "" ]; do
                    if [ "$confirm" = "" ] || [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then
                            ok=y
                            "$editor" "$line"
                    elif [ "$confirm" = "n" ]; then
                            ok=y
                    elif [ "$confirm" = "q" ]; then
                            echo "bye"
                            exit
                    else
                            echo "wat? "
                    fi
            done
    done
Turns out, I use "list" dozens of time per day, I forgot a bit about fkt but I might get back to it thanks to this post because I often copy-paste results from list to open them. I had forgotten about adding this confirm prompt :-)

I'm sure there are existing, better, tools for this though.

(I've never tested them on non GNU systems, there might be GNUisms in them)