\\\ This site is under construction \\\

../

- Bash Scripts -

A list of useful bash scripts I have created

YYYY-MM-DD

- randomstring.sh - 2024-11-09

#!/bin/bash

symbols='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVYXWZ~#$&_+-=/\'
count_symbols="${#symbols}"
if [ -z "$1" ]; then
    echo "No length specified.";
else
    ((length = "$1"))
    for i in $(seq 1 "$length") ; do
        string+="${symbols:RANDOM % count_symbols:1}"
    done
    echo "$string" | tr -d '\n' | xsel -i -b # xsel is a dependency, or not if you don't need it
    string=""
    echo "$length" "RND Character String Was placed in clipboard, have fun :)";
fi
            

Full credit to Choroba for the very clever method, my implementation is quite lazy in contrast

Usage: ./randomstring.sh *NumberOfCharacters*

What it does: outputs a random string of characters defined by a positional argument to your clipboard.

Note: xsel is a clipboard tool, it can be removed if you want standard output to the terminal.

- fzfdirectorydumper.sh - 2024-09-22

#!/bin/bash

filenum="$(find -maxdepth 2 -type f | wc -l)";
finalfilenum="$((filenum-1))";
printf "%s" "$finalfilenum" " Files found.";
printf "\nPlease use the Shift+Tab keys to select all files.";
sleep 4; clear;
fzf="$(fzf -m -q / | tr '\n' ' ')" && mv $fzf "$(pwd)";
cd "$(pwd)";
            

Usage: Put the script in the directory containing the folders you want to extract the files from, then run

./fzfdirectorydumper.sh

What it does: for every folder, it goes 1 folder depth in to fetch files & moves them into the script directory.

Note: fuzzy finder (fzf) is a dependency. If you have spaces in filenames, use my other script below.

- directorydumper.sh - 2024-11-09

#!/bin/bash

workingdir="$(pwd)" && foldercount="$(ls -l | grep -c "^d")" && movecount="0"
[[ "$foldercount" == 0 ]] && echo "No folders found." || echo "$foldercount" "Folders found. Searching files...";
while [[ "$foldercount" -ne 0 ]]; do
    cd "$(ls -d */ | sort -r | cut -f1 -d'/' | sed ""${foldercount}"q;d")";
    [[ "$(ls -A)" ]] && mv --backup=numbered * "$workingdir" && ((movecount++))
    cd ..; ((foldercount--));
done
[[ ! "$movecount" == 0 ]] && echo "Finished!" && movecount="0" || echo "Your folders don't have any files to transfer!";
            

Usage: Put the script in the directory containing the folders you want to extract the files from, then run

./directorydumper.sh

What it does: for every folder, it goes 1 folder depth in to fetch files & moves them into the script directory.

Note: This script works with any folder or file with spaces or special characters, go nuts!

If you have duplicate file names, they will have ~NUM~ concatenated to the end to avoid data loss.

I recommend using this if your folders only contain files. Folders will get transfered but it's not optimized for that.

In case of the latter happening, run the script until everything has been moved into your working directory.

- webcamviewer.sh - 2024-11-12

#!/bin/bash

videolist="$(ls /dev/ | grep video | wc -l)";
if [[ "$videolist" != "0" ]]; then
    echo "Found "$videolist" cam feeds: "$(ls /dev/ | grep video | tr '\n' ' ')"";
    for ((i = 1 ; i < "$videolist" + 1; i++ )); do
        if [[ -z "$(v4l2-ctl -l --device /dev/"$(ls /dev/ | grep video | tr '\n' ' ' | cut -d " " -f $i)")" ]]; then
            printf "$(ls /dev/ | grep video | tr '\n' ' ' | cut -d " " -f "$i") ";
            printf "has no controls according to v4l2-ctl, it's probably broken..\n";
        fi
    done
    printf "Which one would you like to try?\nNumber: "; read videochoice
        [[ -z "$videochoice" ]] && printf "Using first feed.\n" && videochoice="1"; ((videolist++))
        if [[ "$videochoice" =~ ^[0-9]+$ && "$videochoice" -gt 0 && "$videochoice" -lt "$videolist" ]]; then
            camfeed="$(ls /dev/ | grep video | tr '\n' ' ' | cut -d " " -f "$videochoice")"
            printf "What resolution do you want?\n1. 1920x1080\n2. 1280x720\n3. 640x480\n4. 360x360\n";
            printf "Number: "; read rc
            case $rc in
                1) rc="1920x1080" ;;
                2) rc="1280x720" ;;
                3) rc="640x480" ;;
                4) rc="360x360" ;;
                *) rc="1920x1080" && printf "Using biggest resolution.\n" ;;
            esac
            mpv --demuxer-lavf-o=video_size="$rc",input_format=mjpeg av://v4l2:/dev/"$camfeed" --profile=low-latency
        else
            printf "Invalid input.\n";
        fi
else
    printf "No feed found in /dev/\n";
fi
            

Usage: connect your webcam or camera, then run ./webcamviewer.sh

What it does: checks for video feeds in /dev/ and asks which one to open with mpv and what resolution.

Note: mpv is a dependency, you should tweak the script to your liking if you want to use another viewer.

This script assumes you have v4l2-ctl installed, you can remove the entire for loop if you don't.

- worldbackup.sh - 2024-08-30

#!/bin/bash

i="0"; o="0"; p="0";
                    
# The reasoning behind the sleep commands is to not give the CPU heart attacks by letting the script run loose
# Remember, a path starts with a / and typing a folder name like home/user/Documents/world will NOT work.
                    
echo "- * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * -"
echo "| Note: You can type full paths or a folder, but the folder needs   |"
echo "| to be in the current/working directory                            |"
echo "- * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * -"
                    
while [ "$p" -eq 0 ]
do
    while [ "$i" -eq 0 ]
    do
        echo "World Not Defined."
        sleep 0.5;
        while [ "$o" -eq 0 ]
        do
            printf "%s" "World Folder/Directory: ";
            read WorldDirectory
                    
            if [ "${WorldDirectory#*"/"}" != "$WorldDirectory" ]; then # POSIX substring parameter expansion
                IsWorldDirPath=1; # User entered a path
                if test -d "$WorldDirectory"; then
                    printf "Valid Path :D ";
                    ((o++)) # Ignore loop
                else
                    echo "Invalid Path.. Trying again"
                   sleep 0.5;
                fi
            else
                IsWorldDirPath=0; # User did not enter a path
                if test -d "$WorldDirectory"; then
                    printf "Valid Folder :D "
                    ((o++)) # Ignore loop
                else
                    echo "Invalid Folder.. Trying again"
                    sleep 0.5;
                fi
            fi
        done
        echo "Using" $WorldDirectory;
        sleep 0.5;
        ((i++)); # Ignore loop
    done
                    
    ((i--)) # Reset state to 0
    ((o--)) # Reset state to 0
                    
    while [ "$i" -eq 0 ]
    do
        echo "Backup Not Defined."
        sleep 0.5;
        while [ "$o" -eq 0 ]
        do
            printf "%s" "Backup Folder/Directory: ";
            read BackupDirectory
                    
            if [ "${BackupDirectory#*"/"}" != "$BackupDirectory" ]; then
                IsBackupDirPath=1; # User entered a path
                if test -d "$BackupDirectory"; then
                    printf "Valid Path :D ";
                    ((o++)) # Ignore loop
                else
                    echo "Invalid Path.. Trying again"
                    sleep 0.5;
                fi
            else
                IsBackupDirPath=0; # User did not enter a path
                if test -d "$BackupDirectory"; then
                    printf "Valid Folder :D "
                    ((o++)) # Ignore loop
                else
                    echo "Invalid Folder.. Trying again"
                    sleep 0.5;
                fi
            fi
        done
        echo "Using" "$BackupDirectory";
        sleep 0.5;
        ((i++)); # Ignore loop
        echo "----------------------------------"
    done
    echo "$WorldDirectory" "will be copied over to" "$BackupDirectory" "every day with the yyyy-mm-dd format."
    echo "Time is according to your locale, using the 12-hour clock with AM PM indicators."
    sleep 0.5;
    printf "%s" "Sounds Good? : "; read Answer
                    
    if [ "$Answer" == "yes" ] || [ "$Answer" == "Yes" ] || [ "$Answer" == "YES" ] || [ "$Answer" == "yeah" ]; then
        if [ "$IsWorldDirPath" == 0 ]; then # If not a path
            printf "Concatenating World Folder To Form a Path. . . "
            FullWorldPath="$(echo "$(pwd)""/"$WorldDirectory)"
            sleep 0.5;
            if test -d "$FullWorldPath"; then
                echo "Success !";
                ((o++)) # Ignore loop
            fi
        fi
                    
        if [ "$IsBackupDirPath" == 0 ]; then # If not a path
            printf "Concatenating Backup Folder To Form a Path. . . "
            FullBackupPath="$(echo "$(pwd)""/"$BackupDirectory)"
            sleep 0.5;
            if test -d "$FullBackupPath"; then
                echo "Success !";
                ((o++)) # Ignore loop
            fi
        fi
                    
        if [ "$IsWorldDirPath" == 1 ]; then # If a path
            echo "Valid World Path :D";
            FullWorldPath="$WorldDirectory"
            ((o++)) # Ignore loop
        fi
                    
        if [ "$IsBackupDirPath" == 1 ]; then # If a path
            echo "Valid Backup Path :D";
            FullBackupPath="$BackupDirectory"
            ((o++)) # Ignore loop
        fi
                    
        ((p++))  # Ignore loop
    else
        echo "Process Interrupted, Trying Again.";
        ((i--)) # Reset state to 0, Loop continues
        ((o--)) # Reset state to 0, Loop continues
        sleep 0.5;
    fi
done
cd "$FullBackupPath"
while :
do
    CurrentDate="$(date +"%F_%r" | tr -d " ")";
    mkdir "$CurrentDate"
    cp -r "$FullWorldPath" "$CurrentDate"
    echo "Backup done at: "$CurrentDate"";
    sleep 1d
done                    
            

Usage: ./worldbackup.sh

What it does: it backups a folder every day, that's fundamentally it.

Note: I used this script before to backup my minecraft server, 90% is error checking before the backup process.


DO NOT run my scripts with elevated privileges, I will beat the shit out of you am not a security expert.