Skip to content

Commit

Permalink
Merge pull request #10 from gramtech/master
Browse files Browse the repository at this point in the history
Organised and Standardised bash_functions
  • Loading branch information
sebastienrousseau authored Mar 23, 2018
2 parents 00e31bc + 023f377 commit 4e3eb4b
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 71 deletions.
163 changes: 93 additions & 70 deletions bash/.bash_functions
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,20 @@
#
# Sections:
#
# 1. Functions, File and Folder Management
# 1. Addding and Removing Files and Folders
# 2. Compression, Decompress and Archive Functions
# 3. General Function (could be better sorted)
# 4. Network and Debugging Function
# 5. Fun Functions
#
# ---------------------------------------------------------------------------


# ---------------------------------------------------------------------------
# 1. Functions, File and Folder Management
# 1. Addding and Removing Files and Folders
# ---------------------------------------------------------------------------

# zipf: Function to create a ZIP archive of a folder
zipf() { zip -r "$1".zip "$1"; }

# numFiles: Function to count of non-hidden files in current dir
alias numFiles='echo $(ls -1 | wc -l)'

# rm: Function to make 'rm' move files to the trash
function rm() {
rm() {
local path
for path in "$@"; do
# ignore any arguments
Expand All @@ -46,7 +43,7 @@ function rm() {
}

# cd: Function to Enable 'cd' into directory aliases
function cd() {
cd() {
if [ ${#1} == 0 ]; then
builtin cd
elif [ -d "${1}" ]; then
Expand All @@ -59,22 +56,79 @@ function cd() {
fi
}

# matrix: Function to Enable Matrix Effect in the terminal
matrix() {
echo -e "\\e[1;40m" ; clear ; while :; do echo $LINES $COLUMNS $(( $RANDOM % $COLUMNS)) $(( $RANDOM % 72 )) ;sleep 0.05; done|awk '{ letters="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@#$%^&*()"; c=$4; letter=substr(letters,c,1);a[$3]=0;for (x in a) {o=a[x];a[x]=a[x]+1; printf "\033[%s;%sH\033[2;32m%s",o,x,letter; printf "\033[%s;%sH\033[1;37m%s\033[0;0H",a[x],x,letter;if (a[x] >= $1) { a[x]=0; } }}'
# mkcd: Function to combine mkdir and cd
mkcd() {
mkdir "$1"
cd "$1" || exit
}

# md: Function to create a new directory and enter it
md() {
mkdir -p "$@" && cd "$@" || exit
}

# mcd: Function to makes new Dir and jumps inside
mcd() {
mkdir -p "$1" && cd "$1" || exit;
}

# rd: Function to remove a direcory and its files
rd() {
rm -rf "$@"
}

# trash: Function to moves a file to the MacOS trash
trash() {
command mv "$@" ~/.Trash;
}

# ---------------------------------------------------------------------------
# 2. Compression, Decompress and Archive Functions
# ---------------------------------------------------------------------------

# zipf: Function to create a ZIP archive of a folder
zipf() { zip -r "$1".zip "$1"; }

# extract: Function to extract most know archives with one command
extract() {
if [ -f $1 ]; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar e $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}

# ---------------------------------------------------------------------------
# 3. General Function (could be better sorted)
# ---------------------------------------------------------------------------

# numFiles: Function to count of non-hidden files in current dir
alias numFiles='echo $(ls -1 | wc -l)'

# tree: Function to generates a tree view from the current directory
if [ ! -e /usr/local/bin/tree ]; then
function tree(){
tree(){
pwd
ls -R | grep ":$" | \
sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
}
fi

# sshKeyGen: Function to generates SSH key
function sshKeyGen() {
sshKeyGen() {

echo "What's the name of the Key (no spaced please) ? ";
read -r name;
Expand All @@ -93,7 +147,7 @@ function sshKeyGen() {
}

# filestolower: Function to rename all the files which contain uppercase letters to lowercase in the current folder
function filestolower(){
filestolower(){
read -r -p "This will rename all the files and directories to lowercase in the current folder, continue? [y/n]: " letsdothis
if [ "$letsdothis" = "y" ] || [ "$letsdothis" = "Y" ]; then
for x in `ls`
Expand Down Expand Up @@ -121,12 +175,6 @@ aliasc() {
alias | grep "^${1}=" | awk -F= '{ print $2 }' | sed "s/^'//" | sed "s/'$//"
}

# mkcd: Function to combine mkdir and cd
mkcd() {
mkdir "$1"
cd "$1" || exit
}

# tosu: Function to combine touch and osu
tosu() {
touch "$1"
Expand All @@ -138,73 +186,35 @@ size() {
stat -f '%z' "$1"
}

# md: Function to create a new directory and enter it
function md() {
mkdir -p "$@" && cd "$@" || exit
}

# rd: Function to remove a direcory and its files
function rd() {
rm -rf "$@"
}

# logout: Function to logout from OS X via the Terminal
logout() {
osascript -e 'tell application "System Events" to log out'
builtin logout
}

# countdown: Function for countdown
function countdown(){
countdown(){
date1=$((`date +%s` + $1));
while [ "$date1" -ne `date +%s` ]; do
echo -ne "$(date -u --date @$((`date +%s` - $date1)) +%H:%M:%S)\r";
done
}

# logout: Function for a stopwatch
function stopwatch(){
stopwatch(){
date1=`date +%s`;
while true; do
echo -ne "$(date -u --date @$((`date +%s` - $date1)) +%H:%M:%S)\r";
done
}

# extract: Function to extract most know archives with one command
extract() {
if [ -f $1 ]; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar e $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}

# randompwd: Function to generates a strong random password of 20 characters
# https://www.gnu.org/software/sed/manual/html_node/Character-Classes-and-Bracket-Expressions.html
function randompwd() {
randompwd() {
cat /dev/urandom | LC_CTYPE=C tr -dc [:alnum:],[:alpha:],[:punct:] | fold -w 256 | head -c 20 | sed -e 's/^0*//'
echo
}

# mcd: Function to makes new Dir and jumps inside
mcd() { mkdir -p "$1" && cd "$1" || exit; }

# trash: Function to moves a file to the MacOS trash
trash() { command mv "$@" ~/.Trash; }

# ql: Function to open any file in MacOS Quicklook Preview
ql() { qlmanage -p "$*" >&/dev/null; }

Expand Down Expand Up @@ -232,24 +242,28 @@ ii() {
}

# Show hidden system and dotfile files
function showhiddenfiles() {
showhiddenfiles() {
defaults write com.apple.Finder AppleShowAllFiles YES
osascript -e 'tell application "Finder" to quit'
sleep 0.25
osascript -e 'tell application "Finder" to activate'
}

# Hide hidden system and dotfile files
function hidehiddenfiles() {
hidehiddenfiles() {
defaults write com.apple.Finder AppleShowAllFiles NO
osascript -e 'tell application "Finder" to quit'
sleep 0.25
osascript -e 'tell application "Finder" to activate'
}

# ---------------------------------------------------------------------------
# 4. Network and Debugging Functions
# ---------------------------------------------------------------------------

## hammer a service with curl for a given number of times
## usage: curlhammer $url
function curlhammer () {
curlhammer () {
bot "about to hammer $1 with $2 curls ⇒";
echo "curl -k -s -D - $1 -o /dev/null | grep 'HTTP/1.1' | sed 's/HTTP\/1.1 //'"
for i in {1..$2}
Expand All @@ -262,7 +276,7 @@ function curlhammer () {
## curlheader will return only a specific response header or all response headers for a given URL
## usage: curlheader $header $url
## usage: curlheader $url
function curlheader() {
curlheader() {
if [[ -z "$2" ]]; then
echo "curl -k -s -D - $1 -o /dev/null"
curl -k -s -D - $1 -o /dev/null:
Expand All @@ -274,7 +288,7 @@ function curlheader() {

## get the timings for a curl to a URL
## usage: curltime $url
function curltime(){
curltime(){
curl -w " time_namelookup: %{time_namelookup}\n\
time_connect: %{time_connect}\n\
time_appconnect: %{time_appconnect}\n\
Expand All @@ -286,4 +300,13 @@ time_starttransfer: %{time_starttransfer}\n\
}

# httpDebug: Function to download a web page and show info on what took time
httpDebug() { /usr/bin/curl "$@" -o /dev/null -w "dns: %{time_namelookup} connect: %{time_connect} pretransfer: %{time_pretransfer} starttransfer: %{time_starttransfer} total: %{time_total}\\n"; }
httpDebug() { /usr/bin/curl "$@" -o /dev/null -w "dns: %{time_namelookup} connect: %{time_connect} pretransfer: %{time_pretransfer} starttransfer: %{time_starttransfer} total: %{time_total}\\n"; }

# ---------------------------------------------------------------------------
# 5. Fun Functions
# ---------------------------------------------------------------------------

# matrix: Function to Enable Matrix Effect in the terminal
matrix() {
echo -e "\\e[1;40m" ; clear ; while :; do echo $LINES $COLUMNS $(( $RANDOM % $COLUMNS)) $(( $RANDOM % 72 )) ;sleep 0.05; done|awk '{ letters="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@#$%^&*()"; c=$4; letter=substr(letters,c,1);a[$3]=0;for (x in a) {o=a[x];a[x]=a[x]+1; printf "\033[%s;%sH\033[2;32m%s",o,x,letter; printf "\033[%s;%sH\033[1;37m%s\033[0;0H",a[x],x,letter;if (a[x] >= $1) { a[x]=0; } }}'
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reedia-dotfiles",
"version": "0.2.109",
"version": "0.2.111",
"description": "Dotfiles - A set of Mac OS X configuration files - Simply designed to fit your shell life.",
"keywords": [
"dotfiles",
Expand Down

0 comments on commit 4e3eb4b

Please sign in to comment.