7/3/2013 - 9:24 AM

bash helpers

bash helpers

# SYSTEM: open a file as admin with a special app
sudo open -a /Applications/Utilities/ /usr/local/mysql/data/Mathiass-iMac.log

# SYSTEM: Create a symbolic link
ln -s /Path/to/original/folder New-folder-name

# SYSTEM: show long running clock
clear; while true; do echo -e \\b\\b\\b\\b\\b\\b\\b\\b`date +%T`\\c ; sleep 1; done

# SYSTEM: Set ENV var
export FOO=42

#SYSTEM: Find content in files of folder
grep -rinHo "my string"

# SYSTEM: copy files by regex pattern. E.g. copy only jpegs with odd numbers
find . -type f | grep -i "[13579].jpg" | xargs -I{} cp {} ../test_cp/{}
find ./src/folder -type f | grep -i "regexp" | xargs -I{} cp {} ../target/folder/{}

# SYSTEM: find big folders
du --max-depth=7 /* | sort -n

# SYSTEM un tar files
tar -zxvf yourfile.tar.gz

# SYSTEM: Run a script (eg.g imagemagick convert ) for a bunch of files by regex

for main in `seq 1 36`; do
  ls | grep -e "Uebung$main[A-C]*.jpg" | xargs echo | xargs -I "{}" sh -c "convert -delay 75 -resize $SIZE {} $PREFIX$main.gif"
  echo "$PREFIX$main.gif created"

# list the file/folder sizes
du -sh $PWD/*

# meassure time a script uses
sleep 3;
echo "$ELAPSED_TIME sek"
# REDIS: Redis delete by wildcard
~/local/redis/redis-cli KEYS "prefix:*" | xargs ~/local/redis/redis-cli DEL

# GM (GraphicsMagick): place a image inside another image.
# place it to position x:200px, y:200px with a width of 50px
gm convert -page +0+0 ./source.jpg -resize 50 -page +200+200 ./placement.png -mosaic ./output.jpg

#Docker: set machine ip to env
export DOCKERLOCALHOST=`docker-machine ip default`

# MISC: convert all m4a files to mp3 for all subfolders (brew packages: ffmpeg, rename)
# convert to mp3
find . -name "*.m4a" -exec ffmpeg -i {} -acodec libmp3lame -ab 320k "{}.mp3" \;
# rename
find . -name "*.m4a.mp3" -exec rename -s ".m4a.mp3" ".mp3" {} \;
# delete m4a files
find . -name "*.m4a" -exec rm {} \;

# pipe to multiple copmmands
echo "test" | tee >( cat - > testa.txt ) | cat - > testb.txt

# write the output of multiple commands to a file
{ echo "line 1"; echo "line 2"; ls } > cmb.txt

# query a prometheus metric every 60 seconds and generate a csv
echo "first_metric,second_metric" > ~/Downloads/my_prom_log.log; \
while true; do \
	curl -s http://my-service/metrics | \
	grep "^first_metric\|^second_metric" | \
	awk -F " " '{print $2}' | \
	paste -s -d, - >> ~/Downloads/my_prom_log.log; \
sleep 60; done
# Using SCREEN 
# create a session with name
screen -S sessionname
# detach from session
# > Ctrl+A,D
# list sessions
screen -ls
# reconnect to session (id and name see screen -ls)
screen -r sessionid.sessioname
# NODE: run grunt with debugger
node-debug $(which grunt) task

# NODE.js: fix LIFECYLE Error
#   ld: library not found for -lgcc_s.10.5
#   clang: error: linker command failed with exit code 1
cd /usr/local/lib
sudo ln -s ../../lib/libSystem.B.dylib libgcc_s.10.5.dylib

# Start node with a defined obj-memory limit.
# This means a limit for the generated data inside the process.
# You have to add the used memory of the runtime and code to get the real memory usage.
# Default is 1,4GB -
# e.g.: 100b
node --max-old-space-size=100 index.js
#NPM list the latest versions of dependencies
echo ".dependencies"; jq ".dependencies|keys|.[]" package.json -r | xargs -I{} npm info {} -j | jq "{(.name):.version}" -c -r;echo ".devDependencies"; jq ".devDependencies|keys|.[]" package.json -r | xargs -I{} npm info {} -j | jq "{(.name):.version}" -c -r;
# NETWORK: find a process listening to a specific port
sudo lsof -i TCP:$PORT | grep LISTEN

# NETWORK: limit network conncetion
ipfw pipe 1 config bw 64KByte/s # Pipe erstellen, die nur 64kB/s durchlässt
ipfw add 1 pipe 1 src-port 80 # Pipe an den Sourceport 80 binden
ipfw delete 1 # Pipe wieder löschen

#NETWORK: do a port scan via netcat
nc -zv localhost 3000-3999
# GIT: rename a git branch
git branch -m oldname newname
git push origin newname
git push origin :oldname

# GIT: remove a remote tag
git tag -d __tagname__
git push origin :refs/tags/__tagname__

# GIT: create github repo from folder
cd <localdir>
git init
git add .
git commit -m 'message'
git remote add origin <url>
git push -u origin master

# GIT: reset to head
git fetch origin
git reset --hard origin/master

# GIT: change the latest commit message
git commit --amend -m "New commit message"

# GIT: compare two branches
git diff --name-status master..branchName
# CRON: crontab header to run node and grunt within a cron job