Skip to content

Instantly share code, notes, and snippets.

View phx's full-sized avatar

phoenix phx

View GitHub Profile
@phx
phx / i++
Created June 13, 2013 03:15
increment a variable inside a for loop to rename all text files that start with a certain string.
#!/bin/bash
for files in old*.txt
do
((i++))
mv $files $(echo $files | sed -E 's/[0-9]+\.txt//')OLDOLD$i.txt
done
@phx
phx / base64
Created June 13, 2013 04:39
encode image to base64 text string using native openssl package in osx. encoding with this method is more widely-available than the regular base64 package, because it doesn't first require installing xcode or building gnu coreutils from source.
#!/bin/bash
openssl base64 -in pudding.png > pudding.txt
@phx
phx / pbpaste >> gui clipboard
Created June 13, 2013 22:52
(osx version) when copied to ~/.bashrc or ~/.bash_profile, will create 'Ctrl + ]' keyboard shortcut, allowing *current* (not necessarily last) terminal command to be copied to the gui clipboard via pbpaste.
#!/bin/bash
bind '"\C-]":"\C-e\C-u pbcopy <<"EOF"\n\C-y\nEOF\n"'
# i might try:
# $ echo 'bind \'"\C-]":"\C-e\C-u pbcopy <<"EOF"\n\C-y\nEOF\n"\'' >> ~/.bash_profile
@phx
phx / remote auto ssh key exchange
Created June 13, 2013 22:59
will automatically exchange ssh keys with remote server, allowing automatic future password-less access to the remote location from current location. edit as needed if remote location doesn't run sshd on default port.
#!/bin/bash
printf "enter username@ip-address: " ; read UIP ; ssh $UIP "mkdir .ssh 2>/dev/null ; echo $(cat $HOME/.ssh/id_rsa.pub) >>.ssh/authorized_keys ; chmod 700 .ssh ; chmod 600 .ssh/authorized_keys" ; echo "hostname=$(ssh $UIP hostname)"
@phx
phx / remove single-line, consecutive duplicates
Created June 19, 2013 05:15
uses a few different commands in this specific instance to target a phrase that is repeated again on the same line. i was not going for accuracy, but for speed. there is probably a lot that can be eliminated from this, including the initial `cat` command, which could possibly be replaced by input redirection.
#!/bin/bash
cat tags.txt | cut -d' ' -f2- | grep -o -v '\ +.+' | tr ' ' '-' | grep -o -E '[a-zA-Z0-9\-]+[a-zA-Z0-9]$' | grep -v '^-' | tr '[A-Z]' '[a-z]' | tr '-' ' ' | sort -u > newtags.txt
@phx
phx / remove extra whitespace
Created June 19, 2013 06:12
remove extra whitespaces
#!/bin/bash
# removes all "extra" whitespace (leaves normal spaces)
cat "web.txt" | awk '{$1=$1}1'
# removes only leading and trailing whitespace
cat "web.txt" | awk '{gsub(/^ +| +$/,"")}1'
# this perl command should do the same thing
cat "web.txt" | perl -lape 's/^\s+|\s+$//g'
@phx
phx / rsync over ssh
Created June 19, 2013 20:04
recursive rsync over ssh on non-standard port, with update, compression, progress bars, and more.
#!/bin/bash
# -a -- recursive, preserving date/time/ownership (`man rsync`)
# -u -- update (only replace/add files that are newer or non-existent on the target)
# -v -- verbose output
# -z -- compression
@phx
phx / sed multiline replace
Created June 20, 2013 07:08
sed work in progress
#!/bin/bash
cat "hello.txt" | sed -E '/src=".+"/ {
r boobs.txt
d
}
'
@phx
phx / mp3-to-m4r iphone ringtone creator
Created June 21, 2013 04:55
(for use with jailbroken iphones): this is one of those things that may sound like a scenic route to most people who have "an app for that," but for command line ninjas, this is must-have knowledge. this one-liner will dump an mp3 file to wav, then convert the wav to m4r, which can be easily copied over to the iphone's file system via scp/sftp/etc.
#!/bin/bash
mplayer -vo null -vc null -ao pcm:fast:file=file.wav file.mp3; faac -b 128 -c 44100 -w file.wav
@phx
phx / sed newline
Created June 23, 2013 04:44
(bsd sed): target 3 sections of regex pattern, and further target one specific section to copy to new line.
#!/bin/bash
nl=$'\n'; cat "hello.txt" | sed -E "s/(<img.+src=\")(.+)(\".+)/\1\\$nl\2\\$nl\3/g"