Skip to content

Instantly share code, notes, and snippets.

@JorianWoltjer
Last active January 28, 2023 10:47
Show Gist options
  • Save JorianWoltjer/35cd89f18ca50fc5e2e41d687f9f8b15 to your computer and use it in GitHub Desktop.
Save JorianWoltjer/35cd89f18ca50fc5e2e41d687f9f8b15 to your computer and use it in GitHub Desktop.
# A collection of bash functions I have in my .bashrc file for automating quick things
# Variables
export MANPAGER="sh -c 'col -bx | bat -l man -p'" # Read manpage formatted in color using https://github.com/sharkdp/bat
export me="$USER:$(id -g)" # Simple "user:group" string useful for setting `chown` to yourself
# Aliases
alias bashrc="nano +119 ~/.bashrc && source ~/.bashrc" # Open .bashrc file at the bottom, and reload it when done
alias pls='sudo $(history -p !!)' # Run previous command with sudo
alias untar="tar -xvf"
# Make splitting the terminal in Windows Terminal start in the current directory
wt_duplicate() {
echo -en '\e]9;9;"'
wslpath -w "$PWD" | tr -d '\n'
echo -en '"\x07'
}
export PROMPT_COMMAND="wt_duplicate 2>/dev/null"
# Change working directory into a Windows path for WSL
cdw() { # cdw "C:\Windows\System32"
if [ $# -eq 0 ]; then # Go to windows home directory
cd /mnt/c/
path=$(cmd.exe /C "echo %USERPROFILE%" | tr -d '\r')
else # Go to specified directory
path="$1"
fi
cd $(wslpath "$path")
}
# Use hexyl to show the first and last 10 lines of hex of a file
ham() { # ham file.bin
lines=${2:-10}
size=$(stat --format="%s" "$1")
offset=$((16 - $size % 16))
head "$1" -c $(($lines*16)) | hexyl | sed '$d'
echo "..."
tail "$1" -c $(($lines*16-$offset)) | hexyl -o $(($size+$offset - $lines*16)) | sed '1d'
}
# Get the length of the standard input, or the first argument
len() { # echo "example" | len; len "example"
if [ $# -gt 1 ]; then
echo "Error: Only one argument is allowed (len 'something with spaces')"
return 1
elif [ -z "$1" ]; then
read -r input
echo ${#input}
else
echo ${#1}
fi
}
# Convert masscan -oX output to more nmap like file. Useful for chaining tools that expect an nmap format
masscan-convert() { # masscan-convert out.xml
sudo chown $USER:$(id -gn) $1
tmpfile=$(mktemp)
sed '/<!-- masscan v1.0 scan -->/d' $1 > ${tmpfile}
cat ${tmpfile} > $1
rm -f ${tmpfile}
}
# A few very short functions that are almost aliases
# Start explorer in current WSL directory
xp() { # xp /tmp
explorer.exe ${1:-"."}
}
# Show help of command if no manual page found
man() { # man ffuf
/usr/bin/man $1 || $1 -h
}
# Clone a git repository and move into the directory it created
clone() { # clone https://github.com/user/repo.git
git clone "$1" && cd "$(basename "$1" .git)"
}
# Wait for port to open, checks every second
waitport() { # waitport 10.10.10.10 80
echo -n "Waiting"
while true; do
nc -z $1 $2
[[ $? == 1 ]] || break
sleep 1
echo -n "."
done
echo -e "\nPort $2 is open on $1"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment