Skip to content

Instantly share code, notes, and snippets.

@firedynasty
Last active September 3, 2021 05:13
Show Gist options
  • Save firedynasty/a61e91a7c1814cd51bedb52d163459d6 to your computer and use it in GitHub Desktop.
Save firedynasty/a61e91a7c1814cd51bedb52d163459d6 to your computer and use it in GitHub Desktop.
Bash to open multiple files
function opens(){
      if [[ $# -eq 0 ]]; then
      elif [[ $# -eq 1 ]]; then
        open "$(find . -type f -maxdepth 2 -not -path '*/\.*' | sort | head -$1 | tail -1)"
      else
        hello_var=$(echo -n "$1")
        minus_var=$(echo -n "$2")
        count_=0
        for i in {$hello_var..$minus_var}
        do
            second_var="$(find . -type f -maxdepth 2 -not -path '*/\.*' | sort | head -$i | tail -1)"
            array=( .csv .docx .scpt .xlsx .ipynb .rtf)
            for i_2 in "${array[@]}"
            do
                if [[ $second_var =~ "$i_2" ]]; then
                    let count_++
                fi 
            done
            if [ $count_ -eq 0 ];then
                open -a "google chrome" "$(find . -type f -maxdepth 2 -not -path '*/\.*' | sort | head -$i | tail -1)"
            fi
            count_=0
            echo "($i) " "$(find . -type f -maxdepth 2 -not -path '*/\.*' | sort | head -$i | tail -1)" 
            sleep 1
        done
    fi
}

function f() {
    if [[ $# -eq 0 ]]; then
        find . -maxdepth 2 -type f -not -path '*/\.*' | sort > /Users/username/desktop/macbook_pro_scripts/echo_files/open_test.txt
        cat -b /Users/username/desktop/macbook_pro_scripts/echo_files/open_test.txt
    else

``
    fi
}

The Function f will display the files in the current directory up to 2 levels in a list with numbers.

Then you can choose to open the files based on the number with "opens 1 5",

Which means open the first file to the fifth file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment