Use case : install a specific version of capistrano to deploy stuff somewhere Let’s say you need to install capistrano 2.* to be able to use your deploy scripts. In this case, I’m running a fresh Yosemite install. Let’s say we’ll need to setup a specific ruby version with a specific gem version. To do …
Command line
Rename files replacing spaces by another character
Let’s say you have a list of files with blank spaces in their name : foie gras final 2-1.eps foie gras step 3.eps foie gras step 4.eps foie gras step 6.eps foie gras step 7.eps If you want to translate blank space to hyphen, use the following command : for file in foie*; do mv …
Remove a file starting with a dash
rm ./-the-file
PhantomJS: wait for it
When you want to take screenshots with PhantomJS, you probably need to wait before your page is completely loaded. I mean all resources are loaded and scripts executed. Use the following code to delay your screenshot : var page = new WebPage(); page.open(‘http://stackoverflow.com/’, function (status) { just_wait(); }); function just_wait() { setTimeout(function() { page.render(‘screenshot.png’); phantom.exit(); …
Mac OS X : rename terminal tabs
If you want to manually rename your tabs without using a bash built-in, just hit Shift+Command+i :
Bash: assign a multi-line string to a variable
Sometimes you need multi-lined strings for readability in your scripts. I needed it for scripts that execute SQL queries. Example of a simplified script: #!/bin/bash read -d ” QUERY <<EOF UPDATE table_name SET field1 = ‘value’, field2 = ‘value’ WHERE id = 1; EOF mysql table_name -e “$QUERY”
Find and remove files
I regularly use the find command in scripts to clean directories on my servers. The common way to use find to do this is to write something like: find ./my_dir -name ‘cache-*’ -exec rm -rf \{\} \; It works fine but it always outputs messages like this: find: ./mydir/cache-001: No such file or director which …
Quickly check hosts with ping
I needed a script for a quick health check of a bunch of servers. This is how I did it using the ping command: for((i=1;i<42;i++)); do ping -c 1 -W 3 host${i}.domain.com &> /dev/null if [ $? -ne 0 ] ; then echo “host${i} is down” else echo “host${i} is up” fi done You can …
Set Terminal tab title in Mac OS X
I wrote previously a how-to to set your iTerm tab title. I finally found a tool to do the same thing with the default Mac OS X Terminal. Check it out here, it works perfectly for me! link is dead. On my Mac OS X 10.6.6 and bash (3.2.48(1)-release), you can set your tab title …
Finding the total size of a set of files with awk
If you need to sum the total size of files in a directory or matching a pattern, an easy solution is to use awk. I needed to calculate this total for a set of javascript files, I used this command line: $ find App/ -name ‘*.js’ -exec ls -l \{\} \; | awk ‘{sum+=$5} END …