Git ignore

I recommend adding this .gitignore file as soon as you create a new Git repository:

Octocat’s .gitignore: Some common .gitignore configurations

As you might know, Git has a pretty complex ignore system, especially if you want to start ignoring files that are already in the repo. Adding this .gitignore file to your repo immediately after you create it might save you some headaches down the line.

Advertisements

Setting up swap space on a Linux server

Recently, I set up an EC2 host. It was a smooth and relatively quick process.

However, the problem with the default image is that it doesn’t come with swap space. You can verify this by running:

swapon -s

I found this tutorial online that walks you through creating a swap file, setting it up, and using it as your swap space:

All About Linux Swap Space

That post doesn’t mention it, so I’ll say it here: you need to use sudo in front of each of those commands. Another thing missing from that tutorial is that as I was setting this up, the shell gave me this warning:

$ sudo swapon /swapfile
swapon: /swapfile: insecure permissions 0644, 0600 suggested.

So, I had to do this:

$ sudo chmod 0600 /swapfile

At the very end, you can check whether you were successful using the swapon -s command:

$ swapon -s
Filename Type Size Used Priority
/swapfile file 1048572 0 -1

Removing empty lines using `sed`

Consider this snippet from a file:

50442


5838


1843


167356


131671

It’s a list of integers (the full file has 122 of them), but the delimiter is three newline characters. I needed to get rid of the empty lines in this file.

I thought of it as replacing three \n characters with one, so it seemed like a good place to use the sed command.

However, according to this FAQ for sed, it’s impossible to use \n in a sed command to strip out newlines.

5.10. Why can’t I match or delete a newline using the \n escape sequence? Why can’t I match 2 or more lines using \n?

The \n will never match the newline at the end-of-line because the newline is always stripped off before the line is placed into the pattern space. To get 2 or more lines into the pattern space, use the ‘N’ command or something similar (such as ‘H;…;g;’).

Sed works like this: sed reads one line at a time, chops off the terminating newline, puts what is left into the pattern space where the sed script can address or change it, and when the pattern space is printed, appends a newline to stdout (or to a file). If the pattern space is entirely or partially deleted with ‘d’ or ‘D’, the newline is not added in such cases. Thus, scripts like

       sed 's/\n//' file       # to delete newlines from each line
       sed 's/\n/foo\n/' file  # to add a word to the end of each line

will never work, because the trailing newline is removed before the line is put into the pattern space. To perform the above tasks, use one of these scripts instead:

       tr -d '\n' < file              # use tr to delete newlines
       sed ':a;N;$!ba;s/\n//g' file   # GNU sed to delete newlines
       sed 's/$/ foo/' file           # add "foo" to end of each line

Basically, the utility reads the input line-by-line, so it never sees the newlines anyway. It looks like we can’t replace multiple newlines with a single newline after all.

However, a regex trick I found was to detect empty lines and delete them:

sed /^$/d FILE

This regex looks for any line that ends immediately after it begins, then deletes that line. It worked perfectly for my needs.

Using an older version of Java JDK in Mac OSX

This StackOverflow thread gives instructions on how to How to Revert to Java 1.6.

I ran this command and got this result:


prem$ /usr/libexec/java_home -v '1.6*'
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

That directory doesn’t seem to exist according to ls:


prem$ ls /Library/Java/JavaVirtualMachines
./ ../ jdk1.7.0_67.jdk/

However, running the given command to change the JDK actually seemed to work:


prem$ export JAVA_HOME=`/usr/libexec/java_home -v '1.6*'`
prem$ java -version
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-466.1-11M4716)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-466.1, mixed mode)

It’s still pretty mysterious to me how this works.

Noisli

Just a quick productivity tip today.

Noise is probably my #1 distractor. I find it very difficult to concentrate in noisy environments. Moving from Seattle to New York, it has definitely become even more of a problem.

But ambient noise, with some soft instrumental music layered on top, is the best way to protect myself from noisy distractions. Here’s what I use for ambient noise:

Noisli

This page is one of the pinned tabs on my browser. I like to mix thunder, rain (at a low volume), and the crackling fire. The rain noise gives the right amount of noise at the right frequencies. Plus, the thunder and fire give it some texture. I find this combination far superior to simple white noise, which actually gives me a headache.

I haven’t used the visuals or the text editor, so I can’t speak for their effectiveness. But I recommend that you try out Noisli’s ambient noise features.

.bash_profile madness on OSX

I ran into some issues setting up my terminal correctly on OSX. Namely, whenever I’d open a new tab on Terminal, my ~/.bashrc script didn’t get executed. So, I had no syntax highlighting, and I had none of my aliases.

It turns out that ~/.bashrc isn’t designed to be run for Terminal. This StackExchange answer has an explanation of why:

Why doesn’t .bashrc run automatically?

Terminal opens a login shell. So I followed their advice and sourced ~/.bashrc in ~/.bash_profile

Linux utilities: `watch` and `df`

I’ve been using these two tools a lot lately:

df – Display the amount of free space on your disks

watch – Run a command repeatedly, refreshing once every 2 seconds by default

My work involves a large amount of logging, so disk space needs to be monitored fairly closely. I use these utilities in conjunction with each other to keep track of how quickly I’m running out of disk space.

I find this script useful, too. However, because it recursively runs on every level below the working directory, it can take a while.