Easy hosts file editing on OSX…
A nice little app called Gas Mask that resides in the “notification widgets” portion of the menu bar. It allows you to quickly load /etc/hosts and modify it. When you save it, it prompts for your system password. Easier than terminal + vi
Gas Mask can be found here.
How to find yesterday’s date with bash…
I have a process that generates files daily. Rather than having to go delete them myself, I wanted to automate the task of removing the files. After all, the more the computer does for me, the less I have to do, right?
5 Mins of googling and I found this site. I am blantantly copying and pasting the post, this is NOT my post.
Date of yesterday in bash?
I recently had to hack a small shell script that would read files in a directory structure generated based on the date, something like 2007/08/16. The trick was that the script would look at yesterday’s file or files generated a few days ago.A quick search on info and here’s the magic command
FILE=”…$(date -d ‘yesterday’ +%Y/%m/%d)”
Interestingly, you can also use things like 3 days ago, next Monday, 2 months etc. Cool!
UTF8 Woes With Ruby 1.9
One of my projects at work is to consume “halfhose” from Gnip, which is half of the full Twitter firehose. Lots of fast data. Lots of UTF8. When working with the code, I kept getting “ruby 1.9: invalid byte sequence in UTF-8″. Now, being that I am consuming Twitter, it should already be UTF8. You can’t blindly do a .toutf8 on the string, as that actually tries to re-encode the already properly encoded UTF8.
So, I went googling and found this post which linked to this which has this snippet of code:
ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
valid_string = ic.iconv(untrusted_string + ' ')[0..-2]
This block of code did the trick. It basicly removes invalid UTF8 characters. I can’t say that I took the time to fully understand it, I just know that it works.
Daemon framework for Ruby: Forever
A light weight and simple micro framework for easy Ruby daemons.
Found at https://github.com/DAddYE/foreverb
How big are those files in that directory on my *nix system?
Ah, the command line. It can be awesome and a pain in the ass at the same time. In a GUI file manager, you can just highlight the files you want and get a total size of the selected files. On a *nix command line? Not so simple.
find . -iname '*.jpg' | sed "s/ /\\\\ /g" | xargs du -ksb | cut -f1 | xargs echo | sed "s/ /+/g" | bc
That does it
Breaking this monster down gives us the following.
First cd to the directory you want to check in.
cd Pictures
List the files. find finds files.
find . -iname '*.jpg'
./Picture3 001.jpg
./Picture3 002.jpg
...
./Picture4 099.jpg
List the files, excaping spaces. sed allows you to perform actions against the string being passed to it.
find . -iname '*.jpg' | sed "s/ /\\\\ /g"
./Picture4\ 001.jpg
./Picture4\ 002.jpg
./Picture4\ 003.jpg
...
./Picture4\ 099.jpg
List the files, getting file size. xargs allows you to take a line of input and execute it. du is used to get the disk usage size.
find . -iname '*.jpg' | sed "s/ /\\\\ /g" | xargs du -ksb
696009 ./Picture4 001.jpg
675879 ./Picture4 002.jpg
666862 ./Picture4 003.jpg
...
658225 ./Picture4 099.jpg
Cut the file size out of the listings. cut -f1 gives us the first field using ‘space’ as a delimeter.
find . -iname '*.jpg' | sed "s/ /\\\\ /g" | xargs du -ksb | cut -f1
700696
702453
703594
...
696009
Take all of our lines of file sizes and concatenate them to a single line.
find . -iname '*.jpg' | sed "s/ /\\\\ /g" | xargs du -ksb | cut -f1 | xargs echo
700696 702453 703594 ... 696009
Replace the spaces in our single line with ‘+’.
find . -iname '*.jpg' | sed "s/ /\\\\ /g" | xargs du -ksb | cut -f1 | xargs echo | sed "s/ /+/g"
700696+702453+703594+ ... +696009
Now take the output above and use bc to calculate it. bc is a simple command line calculator.
find . -iname '*.jpg' | sed "s/ /\\\\ /g" | xargs du -ksb | cut -f1 | xargs echo | sed "s/ /+/g" | bc
54631548
How big are all the jpg files in my pictures directory? 54,631,548 bytes.
So, command line? Pain in the ass to get something so simple, but pretty awesome that there are so many different, tiny programs for *nix that can work together to get you the results you want.
SSH Keys Made Simple
I have backup routines that I have setup on my laptop and my file server. I have Cygwin installed on both since both machines are Windows (hey, I love Linux too, shut up!). I use rsync over ssh to copy files from my laptop to the file server and then again use rsync to sync all the shared files from 1 500GB drive to another in the same machine (raid 1 didn’t work out so well for me, even with a Promise Raid card installed). Having to type my password in 4 times to backup my laptop was annoying, so I decided to setup ssh keys between the laptop at the file server.
Fairly simple setup:
- Log into your shell (doesn’t matter if its Cygwin or a standard Linux distro)
- Change to the .ssh directory
$ cd .ssh - Generate the private/public key pair (use defaults, including the password, unless you wish to still type in a password on each connect)
$ ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/[username]/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/[username]/id_dsa.
Your public key has been saved in /home/[username]/id_dsa.pub.
The key fingerprint is:
aa:d3:81:89:31:13:01:7c:80:d8:e5:4d:90:b1:25:a6 [username]@[hostname] - Transfer the file to the remote machine you wish to use the key to log into
$ scp id_dsa.pub administrator@192.168.111.45:./id_dsa.pub - SSH to the remote machine
$ ssh [remoteusername]@[remotehostname] - Touch the authorized_keys2 file, just in case it doesn’t exist yet
$ touch ~/.ssh/authorized_keys2 - Set permissions on the authorized_keys2 file so that only the owner can read/write to it
$ chmod 600 ~/.ssh/authorized_keys2 - Copy the contents of the public key into the authorized_keys2 file
$ cat id_dsa.pub >> ~/.ssh/authorized_keys2 - Delete the public key file, just because it’s safer that way!
$ rm id_dsa.pub - Logout of the remote server
$ exit - SSH back into the remote server and test to see if it asks you for a password (assuming you did not enter a password while generating the keys earlier)
$ ssh [remoteusername]@[remotehostname]
Now you can ssh from your machine to the remote machine without using passwords but still keeping the remote machine secure.
jQuery Alternating Row Colors, Only Better!
So, today, I was charged with adding some alternating colors to some results lists in the project I am working on. Simple enough, right?
$('#tableid tbody tr:odd').addClass('tableodd');
Super simple, yet that wouldn’t work, because there are 2 table rows per result. I did some googling and found this, which was pretty close to what I needed. In the end, I modified the code found on that page a bit and ended up with the following at the end of the function that actually populated the results table.
var classNames = {
0: '',
1: '',
2: 'tableodd',
3: 'tableodd'
}
$(‘#tableidtbody tr’).not(‘[th]‘).each(function(index) {
$(this).addClass(classNames[index % 4])
});
Works great. If you need to add any additional rows to your result, then just add another row to classNames and change the index % 4 to how ever many items you have in your classNames array.
Hope someone else will find this useful.
Update your status everywhere at once!
So, I started playing with Twitter. I also have a Facebook and a Myspace. Updating statuses can be fun, but annoying when you have to log into a bazillion different sites all at once.
Enter Ping.fm. You can update your statuses from one place! You can do it via their website, AIM, Yahoo, MSN and even by texting. It’s pretty sweet. Check it out!
I’m a nerd…
A few weeks ago I went to my brother-in-law’s to hang out. I brought my laptop “just in case”. We ended up sharing internet on my Sprint Mogul with WiFiRouter to the laptop (wirelessly if you couldn’t figure out the ‘wifi’ part) and then used Windows Internet Connection Sharing to share the ethernet port to the XBox 360. We were on XBox live over Sprint!
So, to recap… Sprint Mogul -> WfiRouter -> Laptop -> XBox -> Playtime!!!
WTF, Microsoft!
So, because the office was closed today, I had the option to work at home so I didn’t lose 8 hours pay. Great idea! So, I make sure to copy the code and database backup of my project before I left work the other night. Get up today to start working.
Sweet! Oh ya, I got to install Visual Studio 2008. No problem, I’ll grab my copy of my file server and install it. LOLWUT? Can’t copy file 67 of 71? Fine, I go find another copy. 2 hours later, it’s installed.
Time to install Sql 2008 Express. Run setup. Wut? .Net framework 3.5 needs to be installed. I thought that came with Visual Studio 2008? Anyway, download it and install it. Rerun Sql Express setup. Now I gotta have Windows Installer 4.5. WTF? Just install! So, download that and install it. Run Sql Express setup AGAIN. OMFG! I need Visual Studio 2008 Service Pack 1 installed! FINE. Download it, run it. Wow, it took 50 minutes to install the damn service pack. Funny, installing THE ENTIRE DAMN VISUAL STUDIO APPLICATION BUNDLE TOOK 20 MINUTES, but a service pack takes 50?
Nice going Microsoft. What should have taken no longer than an hour has now taken me damn near 5.