Programming Notes to Self

Helpful one liners, and more, that I may need later

Git Clone over SSH

July 8, 2021 β€” Using Git with your own serversβ€”without a hub like GitHub or GitLabβ€”is easy. This is the straightforward, quick and dirty way with no new concepts to learn. There are more advanced ways (like -bare), but I find 4 times out of 5 I just need a quick way to sync repos between machines.

Cloning from your server to your local machine

git clone ssh://breck@example.com/home/breck/project

Pushing to your server from your local machine

The one annoying thing here is your push will be rejected if your are pushing the branch that is currently checked out on the server. You can quickly check out a temporary branch, push main, and then check main back out on the server.

git push

Article source

Unpause

June 9, 2021 β€” After a decade buried in the digital attic, I recently uncovered this old blog and upgraded the content to use Scroll. It was a fun restoration project and interesting to me personally to see what types of technologies I was using a decade ago. It was fun to read that I was still uncertain if I would switch to a Mac. IIRC that Macbook Air would last me until 2016! It was followed by another Air, and then around the end of 2020 I upgraded to the current crop of machines, typing this on an M1 MacBook Pro.

Anyway, I also just remembered how nice and convenient it was to post these small snippets I need in my day-to-day coding work on a fast, ad free site. So decided to restart this blog.

Why not just start a fresh one? That might be a better idea. For now I plan to reuse this one. Perhaps split them later, if that makes sense.

If I do keep with it, this post is just to acknowledge that that's no bug, there was a 3,444 day gap between posts.

Article source

Git Ignore Global

January 4, 2012 β€” Create a global .gitignore file to ignore common pains:

vim ~/.gitignore_global .DS_Store* Icon? Thumbs.db git config --global core.excludesfile ~/.gitignore_global

Thanks to github

Article source

Execute commands on remote server via ssh

November 7, 2011 β€” Ever want to non interactively execute commands on a remote server?

ssh breckyunits.com 'df -h'

Article source

Cool Way to Duplicate a Finder Window in the Current Directory

October 14, 2011 β€” Sometimes I want to rearrange a directory. An easy way to do it is to use Cinch and have 2 windows side by side. It's a pain to open 2 windows to the same directory though, until I found this little script

Article source

Great Overview of Linux Command Line

October 11, 2011 β€” Just read an awesome overview of the command line

Article source

Find big files in Linux

October 6, 2011 β€” Edit: found a better way, use ncdu https://dev.yorhel.nl/ncdu

How to find files larger than a certain size:

find /etc -size +100k

Disk free space:

du

Article source

ack and ackmate

August 26, 2011 β€” So one common problem I have is writing code at a macro level---refactoring things in multiple files. Notepad++ used to make this easy with it's fast and powerful find/replace in folder feature. Textmate's built in find/replace in project is painfully slow.

Grep & Sed don't cut it either. I've been using some custom written perl scripts for my needs. But today I discovered ack & AckMate. Problem solved. Really tight integration with TextMate (though the default 4 key shortcut is terrible--need to change that).

This is probably something I would have learned faster had I been doing more pair programming with senior devs.

Article source

nohup and disown

August 22, 2011 β€” Just read a good guide on how to keep commands running when you exit ssh:

Disown, nohup : Bash Commands.

Basically:

nohup ./mycommand &

Adding the ampersand to the end of a command in Nix executes the command in the background and gives you back your shell. However, if you quit your terminal the process will be killed. The nohup command takes care of that.

What is nohup? It's another obfuscated Nix low level utility that is an abbreviation for "No Hangup Signal", in other words: if the terminal sends a hangup signal, normally that kills all running processes, but this says ignore that.

Wikipedia article on nohup.

Article source

New shortcuts

August 14, 2011 β€” Gmail:

gi - go to inbox c - compose j/k - go up and down, enter go to a page. gt - go to sent

Mac:

ctrl+a - home/start of line ctrl+e - end

Article source

Apache, SSL and self signed certificates

August 14, 2011 β€” Just read an excellent guide about how to create self-signed certificates.

Here's what it boils down to:

# The cool thing about SSL is it's just 2 text files. A public key and a private key. # They have a "pem" and a "crt" extension, but they are just plain text. Neat. # Okay, first step is to create a private key "pem" file. # every time you run this the random output pem file will be different: openssl genrsa -des3 -out private_key.pem 1024 # (enter passphrase) # Next, generate a "signing request". openssl req -new -key private_key.pem -out private_key_certificate_signing_request.csr # (for common name enter the full domain name: # (the extra options aren't necessary--just hit enter) # Next remove the password from the private_key. Otherwise you'd have to enter # that password at all sorts of inconvenient times.....be careful because # if someone gets this unencrypted ket, you'll need to get a new cert..so make your pem readable # only by root! cp private_key.pem private_key.pem.original openssl rsa -in private_key.pem.original -out private_key.pem # now you can generate your public self signed certificate: openssl x509 -req -days 365 -in private_key_certificate_signing_request.csr -signkey private_key.pem -out self_signed_public_certificate.crt # now copy the private key (pem file) and the public key ( crt file ) to your web server. mkdir /etc/httpd/certs cp self_signed_public_certificate.cert /etc/httpd/certs cp private_key.pem /etc/httpd/certs # you can delete the CRT file. you don't need that crap anymore. you got the cert/public key and the pem/private key. thats all you need rm private_key_certificate_signing_request.csr # if you don't have mod_ssl installed you need to install it. on fedora: yum install mod_ssl #now edit the apache config to let apache with mod_ssl know where the keys are: SSLEngine on SSLCertificateFile /etc/httpd/certs/self_signed_public_certificate.crt SSLCertificateKeyFile /etc/httpd/certs/private_key.pem

Article source

EC2 Command Line Tools

August 13, 2011 β€” Step 1. Download them. http://aws.amazon.com/developertools/351

Step 2. Set Java path in your .bash_profile:

export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Home/

Step 3. test Java path is correct:

$JAVA_HOME/bin/java -version

Step 4. set ec2 path in your .bash_profile:

export EC2_HOME=/Users/breck/Downloads/ec2-api-tools-1.4.4.1 export PATH=$PATH:$EC2_HOME/bin

Article source

zip a directory in linux

zip -r backup mydir/ zip -r backup .

Article source

Recursive Sed

August 5, 2011 β€” As far as I can tell, sed doesn't have a recursive mode. Here's a little script that uses perl to simulate a recursive sed:

#!/bin/bash perl -e "s/$1/$2/g;" -pi $(find . -type f)

I put this in a file named ~/rsed

Then in my .bash_profile:

alias rsed="~/rsed"

Now I can use it like so:

rsed old new

Article source

netstat

August 1, 2011 β€” Every wonder what's listening on what ports?

netstat -lp

Article source

Bash History

July 17, 2011 β€” Understanding and mastering bash history can:

  1. Save you a lot of time, keystrokes, and errors.
  2. Allow you to easily create shell scripts for common tasks (such as setting up a server).

Here's a terrific guide on using Bash History effectively.

Article source

Amazing SSH Tricks

July 16, 2011 β€” Just read an article that contains some amazing SSH tricks.

Article source

ngrep for packet monitoring

July 13, 2011 β€” I just came across a neat command line tool, ngrep, via this site.

Sample usage:

ngrep -q -W byline "^(GET|POST) .*"

Article source

Basic Linux Monitoring

July 9, 2011 β€” Show memory usage:

free -m

Add this to your bash_profile:

alias free="free -m" top

Show all processes

ps -a

Disk free space:

df

Article source

Creating a bootable Linux USB disk on Mac

hdiutil convert -format UDRW -o ~/path/to/new.img ~/path/to/source.iso sudo su root dd if=/path/to/new.img of=/dev/rDISKNUMBER bs=1m

July 8, 2011 β€” Where DISKNUMBER = disk1 or disk2 for example. Add that leading "r".

Back in Disk Utility, unmount/eject the disk.

Done!

Article source

Notepad++ HAML Syntax Highlighting and other custom extension mapping

July 3, 2011 β€” I want my HAML files to have PHP(or Ruby) syntax highlighting.

Settings > Style Configurator

Then click php and under "user ext" (in the bottom left) add haml

Article source

Mac Terminal Cursor Trick

July 3, 2011 β€” If you hold the "option" key while using the Mac terminal you can use your mouse and click on where you want the cursor to go. This comes in handy if you mistype a command and don't want to retype the whole thing. It also kind of negates the need for a "home" key on the terminal.

Article source

Apache Conf and Htaccess Examples

June 30, 2011 β€” Configuring Apache can be a pain. I'm going to try and collect links to great resources. The official docs are good, but they really need to add commenting so people can expand upon the resources available.

http://www.evolt.org/ultimate_htaccess_examples

Article source

Fix for "selinux 3.82" stuck while installing Ubuntu from USB Drive

June 27, 2011 β€” If you are installing Ubuntu from USB and are getting stuck at the "SELINUX 3.82" screen, you'll need to edit the selinux/selinux.cnf file on the USB stick and comment out the "vesamenu.c32" line, and remove the "ui" from the last line.

Article source

My Programming Setup

May 30, 2011 β€” Here's my setup. This is kind of for my reference but you can also check it out if you're interested.

Article source

What Happens When you Open a 1.7 GB text file?

May 27, 2011 β€” I downloaded the Verisign Master .com TLD zone file which weighs in at a hefty 1.7 GB. What happens when I open it with different programs on my Windows 7 Samsung netbook?

With Windows Notepad: You quickly get a "File is too large. Open with a different text editor." message. Graceful FAIL

With Microsoft Wordpad: Immediately got a "Not Responding". On the status bar it says "3% complete" but 3 minutes later it hasn't changed. I'm going to go ahead and CtrlAltDelete this one. Disgraceful FAIL.

With Notepad++: "File is too big" error. Graceful FAIL.

VIM in Cygwin: It appears frozen.

I will continue this post at a later date.

Article source

Wildcard Subdomains with GoDaddy

April 24, 2011 β€” Just create a new A Record with * as the host and your IP in the points to field.

Article source

Quickly Set up a Web Proxy Server

March 13, 2011 β€” Sometimes when abroad it becomes necessary to route your internet traffic through a proxy.

Here's a great article that shows how.

I also recommend the QuickProxy Firefox addon which lets you quickly toggle the proxy on and off.

Article source

A few new Tricks in Notepad++

March 12, 2011 β€” I've been on the road for a few months programming on a Windows 7 Samsung netbook. (I didn't want to bring the Macbook with me and risk breaking it or worse).

The one thing I love about Windows is Notepad++. Sorry Textmate, you just can't compete.

Here are some things I'm using a lot more now in Notepad++:

Article source

RVM Quick Reference

December 12, 2010 β€” Return to the Mac OS X default Ruby install:

rvm system

Switch to Ruby 1.9.2:

rvm 1.9.2

Create a new gemset:

rvm gemset create rails3

Switch to newly created gemset:

rvm 1.9.2@rails3

Install rails 3 on new gemset:

gem install rails -v 3.0.3

Return to default ruby install:

rvm system

Install a new version of Ruby:

rvm install 1.9.2

Article source

Forward a port on a remote server to your local machine with an SSH Reverse Tunnel

December 4, 2010 β€” Say you are running a web server locally and want to show it to someone else remotely.

You have a few ways to do this:

  1. Provide your local IP address to the person.
  2. Setup DNS to point a domain to your local IP.

But what if your local computer is behind a NAT and you don't want to configure port forwarding on your router?

One option if you have a publicly facing server already is to create a reverse SSH tunnel with that server. This allows you to forward a port on that server to a port on your local server.

For instance, on my Mac OS X machine I open terminal and type this command:

ssh -nNT -R 3333:localhost:3333 username@yourdomain.com

This will connect to my server (yourdomain.com), and forward traffic from port 3333 on that server to my local machine. Now I can tell someone to go to yourdomain.com:3333 and that would be the equivalent of them going to localhost:3333.

Notes:

Make sure to add a rule to your IPTables config to allow inbound traffic to port 3333.

Article source

Features I Miss from Notepad++ in Textmate

October 16, 2010 β€” Here are a few things I really miss from Notepad++:

If anyone nows how to replicate these things in Textmate please let me know! Thanks.

Article source

Git ignore a file that's in the repo

October 3, 2010 β€” Let's say you've got a file that's checked in like environment.rb that you want to edit for your local machine only and don't want to push the changes to your repository. It's easy, just do:

git update-index --assume-unchanged filepath

and to reverse:

git update-index --no-assume-unchanged filepath

Learn more here.

Article source

Adjusting to a Mac

October 2, 2010 β€” For the third time in my life I've gotten a Macbook. The previous two times I returned them. I'm hoping the third time is a charm.

After a week I'm finally starting to get used to it. I'm starting to really customize it and that seems to make all the difference. I've removed nearly everything from the dock, added a bunch of things to my bash profile, and a friend at work gave me a crash course in Textmate.

I've downloaded a program called Sizeup, which not only emulates the awesome "Snap" feature of Windows 7, but in fact is even better. It gives you the left/right 50% snap feature just like in Win7, but also lets you snap things into top/bottom and even snap things into quadrants. Pretty nice software.

Firefox is my favorite of the 3 browsers on the Mac. I loved Chrome on Windows because it was so fast, but this Air is so blazing fast that running Firefox or Chrome on Mac makes little difference.

I've added the Secret Preferences menu and turned "show all hidden files and folders" on. I hate not seeing everything in a folder.

I created an alias "dsdelete" to quickly recursively delete DS_Store hidden files (helpful when zipping folders for sharing).

I learned the "open ." command and changed the terminal color scheme.

I installed RubyMine and Web Developer Toolbar and Tamper Data and Charles and Dropbox. I'm still trying out VMWare Fusion and haven't decided if I want it yet.

I've slowly gotten used to the keyboard. I tried spaces but have turned it back off for now. Expose I hardly use yet.

All in all, I'm really enjoying the speed, instant on, the light weight of the computer, Ruby on a Mac (doing Rails dev on Windows is a pain), having a real terminal(cygwin is great, but native is better), and the fact that 50% of my CPU isn't dedicated to fighting viruses. I'm still developing slower on this machine but hoping that my speed will improve with time.

I'd say the odds at this point are 60-40 in favor of me sticking with it this time.

Article source

Remove all DS_Store files recursively

find . -type f -name ".DS_Store" -exec rm {} \;

Article source

Windows 7 Hosts File

August 14, 2010 β€” The file is located at:

windows/system32/drivers/etc/host

Mac hosts file:

/private/etc/hosts

Article source

Rubyful Soup

August 14, 2010 β€” I was a big fan of BeautifulSoup in Python and was excited to see a Ruby port. Sadly it's no longer being maintained.

To get v1.0.4 working with Ruby 1.9.2, I had to make the following changes to the rubyful_soup.rb file in my gems directory:

Article source

VIM and Rails

August 13, 2010 β€” Check out this guide.

filetype on " Automatically detect file types. set nocompatible " We don't want vi compatibility. " Add recently accessed projects menu (project plugin) set viminfo^=! " Minibuffer Explorer Settings let g:miniBufExplMapWindowNavVim = 1 let g:miniBufExplMapWindowNavArrows = 1 let g:miniBufExplMapCTabSwitchBufs = 1 let g:miniBufExplModSelTarget = 1 " alt+n or alt+p to navigate between entries in QuickFix map <silent> <m-p> :cp <cr> map <silent> <m-n> :cn <cr> " Change which file opens after executing :Rails command let g:rails_default_file='config/database.yml' syntax enable set ruler " Ruler on set nu " Line numbers on set ruler " Ruler on set nu " Line numbers on set nowrap " Line wrapping offset nowrap " Line wrapping off set timeoutlen=250 " Time to wait after ESC (default causes an annoying set ts=2 " Tabs are 2 spaces set laststatus=2 " Always show status line.

Article source

PHPUnit Filter Option

August 10, 2010 β€” Say you are running PHPUnit from the command line and want to run just a specific test as opposed to a whole class of tests.

For example, say you have a tests.php file that contains a FileWriter_Test_Class with a test method named testFileWriter that you want to run.

Do this:

phpunit --filter testFileWriter FileWriter_Test_Class tests.php

Article source

Kill a Detached Rails Webrick Server

August 9, 2010 β€” From your rails app's root:

cat tmp/pids/server.pid kill -9 {pid}

Article source

Cygwin SSH Client Keep Alive

August 6, 2010 β€” Add this to your /etc/defaults/etc/ssh_config file to prevent SSH servers from disconnecting you after inactivity:

ServerAliveInterval 60

Article source

Augmented Gitignore

.bundle db/*.sqlite3* log/*.log *.log tmp/**/* tmp/* doc/api doc/app *.swp *~ .DS_Store

August 4, 2010 β€” Source

Article source

Windows Flush DNS

June 30, 2010 β€” Open cmd.exe. Run:

ipconfig /flushdns

Article source

Insert a line of text to the top of a file

May 29, 2010 β€” If you have a massive text file you want to add a line to without opening the file, here's a quick solution:

sed -e '1i\text_to_insert' -i file.txt

Article source

wget don't save file

wget -O /dev/null http://address.com

Article source

Recursive ls

March 10, 2010 β€” Add the -R option to ls to recursively list the contents of a directroy.

Article source

ls color scheme

March 10, 2010 β€” found this helpful color scheme for ls. toss in your bash_profile.

alias ls="ls --color=auto" LS_COLORS="di=31;1:ln=36;1:ex=31;1:*~=31;1:*.html=31;1:*.shtml=37;1" export LS_COLORS

Article source

gca alias for faster git

March 10, 2010 β€” One of my most frequent commands is:

git commit -am "message"

I made a simple alias to save keystrokes:

alias gca="git commit -am "

Article source

Dump MySQL schema only

March 10, 2010 β€” I found this command to dump just the schema.

mysqldump -u root -pmypassword test_database --no-data=true --add-drop-table=false > test_dump.sql

You can then load the file easily.

Article source

vim macros

March 9, 2010 β€” Let's say you want to add comments to your php functions following the conventional format:

/** * Returns the sum of 2 variables. * * @param int $a * @param int $b * @return int $sum */ function sum($a, $b) { $sum = $a + $b; return $sum; }

You can record a macro to help speed up the commenting process.

Just type q followed by the letter you want to assign the macro to (in this case let's use c), type your keystrokes, then hit q again to stop recording. Now hit c to replay your macro.

You can overwrite the macro by repeating the above steps.

Bonus tip:

Bookmarks.Use m + a letter to create a bookmark in vim. Then use the backtick plus the letter to return to the location.

Keyword completion. Use Ctrl+N to complete a word.

Notes

  1. Vim macro tutorial.
  2. Another great vim resource.

Article source

pwd while in a symlink directory

March 1, 2010 β€” to get the actual directory, run:

/bin/pwd

Article source

Case sensitivity in MySQL table names and lower_case_table_names

February 27, 2010 β€” On linux, filenames and directories are case sensitive. file and File are different files.

On Windows, filenames are not case sensitive. file and File are the same file.

MySQL saves a database to disk. A "database" is saved in a directory, and each table is saved as a file in that folder.

Thus, say you transfer a database from Linux to Windows that has a table named "userBookmarks", this might cause a problem for you because MySQL on Windows will likely interpret it as "userbookmarks".

There's a setting in your my.cnf file called "lower_case_table_names".

The options are 0, 1, and 2.

1 is the default, and you shouldn't need to change it.

If you change it to 0 on Windows, you might run into problems. If you change it to 2, it doesn't seem to do much.

So what's the solution? Try to avoid capital letters in table names (use _ instead of camel case). When you can't do that, write your code for the Linux database (ie "select * from userBookmarks"), and your queries should translate fine on Windows since Windows will convert that to "select * from userbookmarks".

Article source

The BASH Programming Language

February 25, 2010 β€” I used to use Python, Ruby or even PHP to write backend programs that would automate things like server management tasks, development and editing tasks, deployment tasks, backup tasks, and so forth.

Then I learned what is basically the BASH programming language, a language very similar to Python/Ruby/PHP etc., but is perfect for writing command line programs.

Here's the core of what you need to know:

Use the .sh file extension

A BASH script should have the .sh extension, just as a Python script has the .py extension, a php script has the .php extension, and a Ruby script has the .rb extension.

Start your scripts with #!/bin/bash

#!/bin/bash

Here's your first bash script (name it hello.sh):

#!/bin/bash<br> echo "Hello World"

The *echo* command prints a string and also a new line at the end.

Running your script

Your script must be **executable**. To make it executable, you need to change the file permissions. This would make your hello.sh script executable:

chmod 777 hello.sh

To run your script, from the directory where your script is located, type this:

./hello.sh

The "./" substitutes the current directory. In other words, typing

./hello.sh

is equivalent to typing the full path to the file such as this:

/home/breck/hello.sh

Command line Paramters

Command line parameters are accessed with $1, $2, etc. Create a new script called hello2.sh. Put this in it:

#!/bin/bash echo "Hello World" echo $1 echo $2

Now run your script like this:

./hello2.sh hi breck

It should print this:

Hello World hi breck

The hi got stored in $1, and the breck got stored in $2.

Functions

Functions are pretty simple and straightforward:

#!/bin/bash echo "Hello World" echo $1 echo $2 function hi_name { echo "hi" echo $1 } hi_name $3

Notice you don't need parenthesis and other junk. Name this file hello3.sh and run it with this:

./hello2.sh hi breck conor

It should print this:

Hello World hi breck hi conor

Notice what's going on with parameter passing and scope.

That's it for now. As you can see, BASH is a very clean, simple, yet powerful programming language. Get started with it slowly and gradually build up your skills with it.

Article source

ANSI or ASCII and Unicode or UTF-8 and Newlines

February 25, 2010 β€” The key things you need to know:

ANSI (aka ASCII) and UTF-8 (aka "UnicodeTransformation Format" or just "Unicode") are ways to encode text files into binary data.

I'm simplifying things but actually what I'm leaving out is not important. There are basically 2 character sets: ASCII or ANSI and UTF8 or Unicode. It's probably easiest to just call them ASCII and Unicode.

Unicode is the new guy, and is slowly replacing ASCII.

The main problem you'll run into with ASCII is line breaks. This isn't really an ASCII issue, it's a Windows issue but it often seems like it's an ASCII issue.

Windows does \r\n, whereas Unicode just does \n.

Here's an experiment you can do using Notepad++ and Notepad.

Open Notepad++ and start a new document.

Type in:

hello<br>world

Click "Edit", "EOL Completion" and click "Windows Format" (if it's gray, that means it's already on Windows format). Now cut and paste the text into Notepad. You'll see that it comes out on 2 lines. All good right?

Now go back to Notepad++ and click "Edit", "EOL Completion" and click "UNIX Format". Now cut and paste the text into Notepad. It's on 1 line, right?

Tada!

Now when you're having line problems just use this nifty feature in Notepad++.

Notes

  1. Okay so this article turned out to be about line breaks and not about character encoding. I blame ADD.
  2. Cast of Characters- ASCII, ANSI, UTF-8 and all that
  3. ANSI versus Unicode

Article source

Cygwin Error: Could not resolve hostname X: Non-recoverable failure in name resolution

February 21, 2010 β€” I've been getting this error a lot in Cygwin on Windows 7:

Could not resolve hostname X: Non-recoverable failure in name resolution

It happens a lot when I run a deploy script or git push. Haven't found a solution yet, and am trying to track one down.

The problem is this bug is hard to reproduce. It happens only 5-10% of the time.

Edit #1: Helpful article from Microsoft on Host Name Resolution

Edit #2: Upon closer examination, it seems this error only happens with ssh:

ssh: Could not resolve hostname X: Non-recoverable failure in name resolution

That's narrowed it down a bit but still no solution. Sadly because it only happens once or twice out of 10-20 times I run the "ssh" command, it's taking longer to pin down.

Article source

Plugins for Notepad++

February 6, 2010 β€” Notepad++ is my go to editor.

The default plugins are great, but if you need more just visit the notepad++ plugins list.

To install a plugin:

  1. Download the zip file and unzip it.
  2. Copy the dll to your Program Files/Notepad++/Plugins folder.

The main plugin I've used so far is Explorer. I'll update this in the future with other ones.

That's it!

Article source

More tips on using rsync to deploy your website

February 3, 2010 β€” My post on deploying a website using rsync has gotten quite a few hits.

I thought I'd add some more tips:

rsync -rvz$1

Then you can easily do a DRY RUN. Just type:

./deploy.sh n

The n will get substituted for $1.

Troubleshooting

I've found a lot of times rsync appears to be copying the same files over and over.

There are a few things you can do to troubleshoot when rsync recopies the same files.

The options that I've found come in real handy are:

  1. The i option, which shows you why a file is being copied (all pluses means rsync thinks it doesn't exist in the destination; t means the times differ; s means the contents differ (?)).
  2. The --modify-window=1 option. This lets you ignore differences in modified timestamps. You can set 1 to a high number to ignore large differences.

Article source

Linux Go Back to Previous Directory

cd -

February 1, 2010 β€” To go back to the last directory you were in. Basically an "undo" for the cd command.

Article source

Should your source code be in one folder or should it be nested?

January 25, 2010 β€” There are two ways to organize source code files for a project: flat or nested.

Here's a simple example of a flat structure(1 directory, 4 files):

πŸ“myproject index.php theme.css helper_functions.php logo.png

Here's an example of a nested structure(4 directories, 4 files):

πŸ“myproject index.php πŸ“styles theme.css πŸ“images logo.png πŸ“ helpers helper_functions.php

Both of these structures can accomplish the same thing. Which is better?

If you've worked with me on any project before, you'll know I prefer flatter structures. I try to resist adding directories as much as possible (you always have to add them eventually, but I like to fight it to minimize this effect).

I don't know why my gut tells me to minimize directories, so I thought I'd write about it and see if any rationale emerged.

Benefits of a Flat Structure

Benefit #1 - Quicker access to files

I primarily edit files using Notepad++ on Windows 7 or vim. If you keep all your files in 1 directory, you can open them faster ("Control+O", type a few letters on Win 7, or just "vim filename"). So, a flat structure gives you quicker access to files. I've never timed this before, but am pretty positive it works.

Benefit #2 - Avoid file path issues

Dealing with paths when working with a website is often the cause of careless bugs. If everything is in one directory, it makes it painless to include things on both the server side(include 'file.php';) and the client side ('src="script.hs"'). Once you start nesting directories, it's easy to make mistakes.

Benefit #3 - Easier to bring people up to speed

When I join an in-progress project or use a new library, I like to try and wrap my head around the whole thing. I can't do that if I have to dig into different directories to see all the files. Why hide 50% of the project in subdirectories?

Benefit #4 - Avoid overwriting issues

Occasionally you'll have a structure like this:

index.php πŸ“admin index.php

You might accidentally overwrite one index with the other, or edit one index when you think you're editing the other. Doesn't happen too often, but occasionally it can be annoying. By sticking everything in one directory, you don't run into that problem as much.

Benefits of a Nested Structure

Of course there are benefits to having multiple directories, otherwise this style wouldn't be so popular.

Benefit #1 - Looks more organized

It's nice and neat to have your files tucked away into nicely labeled directories.

Benefit #2 - Easier for teams

It can be easier to divide work between team members when things are in different directories. For instance, why should the designers need their images mixed in with backend files?

Benefit #3 - A directory with too many files becomes unwieldy

I'll admit, once you have more than some threshold of files in a directory, it can quickly become hard to manage. If the files are nearly all of the same type (say, php files), then it's not as bad. But once you have 20 php files, 3 javascript files, 15 images, 4 css files, 2 bash files, 3 readmes, and 45 text files, it might be time to split things up.

My theory on why directories are bad.

Less paths create an easier mental model.

If you walk into a messy room, it can be hard to create a mental model of the contents of that room. This is the equivalent to a file structure like the one below:

Bad

πŸ“myproject a_image.png base_functions.php dog.js index.php mike.png names.csv new_logo.jpg test.php xmlfunctions.php zlib.js

It's a mess.

If you walk into a room where everything is put away(clothes in the dresser, books on a bookshelf, odds and ends in the drawers) it's easier and an improvement. A room organized like this looks like the file structure below:

Ok

πŸ“myproject index.php πŸ“images a_image.png mike.png new_logo.jpg πŸ“php base_functions.php test.php xmlfunctions.php πŸ“scripts dog.js zlib.js πŸ“data names.csv

But now you've introduced the problem that to access nearly anything, you have to "open a drawer". You also can't see everything all at once.

Best

The optimal solution is to not put your stuff into drawers, but to:

  1. Remove as much stuff as possible.
  2. Put the remaining stuff out in the open in neats piles in separate parts of the room.
  3. If, and only if, you have a ton of the same type of thing, put those into their own drawer/cabinet.

If you apply this algorithm to a sample project, you might get something like a project I just made with PHPUno

πŸ“dropdate πŸ“data 1 2 .. 578 index.php logo.png sprites.png style.css uno_controller.php uno_app.php uno_models.php uno_helpers.php

Search, don't sort

Gmail has an adage "search, don't sort". I think it applies here. Directories are a form of sorting, while autocomplete(which is integrated into practically everything nowadays) is a form of searching. By sticking everything into one directory, you enable search. Create multiple directories, and you disable search.

The Rule of Three

Here's a simple rule of thumb to help you organize your folder structure better:

In other words, the directory "icon" should never have a subdirectory in it.

πŸ“myproject πŸ“images πŸ“icons

This means that any project should have at most 9 subdirectories.

Article source

Unix date command

January 19, 2010 β€” Display the current unix timestamp:

date +%s

Handy way to view it from the command line.

Article source

Bug Workaround for JS Charts Error: Not enough data to display chart

nullData = new Array( ); myChart.setDataArray(nullData,"null");

January 16, 2010 β€” Add these 2 lines to your javascript.

Basically, if the first data set has only 1 data point, JSCharts will fire the alert. If it has 0 or 2+ data points, it won't. This fix creates an empty data series which disables this alert.

Article source

Change the file owner or group in Linux

January 8, 2010 β€” Change the owner of a file or folder:

chown newowner filename

Change the group of a file or folder:

chgrp newgroup filenameorfoldername

Article source

How to load a sql file into MySql

January 6, 2010 β€” It's very simple thanks to pipes.

mysql -u username -p < sqlfile.sql

Make sure your sql file has a "use database" line.

Article source

Shortcut for switching to a Windows directory in Cygwin

January 1, 2010 β€” In cygwin it can be a pain to change to a Windows directory like `C:\Users\breck\Documents\My Dropbox`.

I made a small bash script called `wcd` that makes it easier.

wcd

#!/bin/bash echo $1 newpath=$(echo $1 | sed 's|\\|/|g' | sed 's|:||g' | sed 's|^C|/cygdrive/c|') echo $newpath cd "$newpath"

The echos are just for debugging--I'm kind of new to bash scripting. Feel free to remove.

Make this file executable and add an alias into your bash_profile:

alias wcd="source ~/wcd"

Now you can just type(or actually copy/paste) something like this:

wcd "C:\Users\breck\Documents\My Dropbox"

...and that will take you to the Windows directory.

Notes

1. wcd gist.

Article source

Cygwin treats the Windows Clipboard like a file

December 30, 2009 β€” In Linux every thing is a file. This is great because it means the Windows clipboard is a file too!

Want to copy the current cygwin directory into your clipboard?

pwd > /dev/clipboard

Want to print the contents of the clipboard?

cat /dev/clipboard

Notes

1. Fun with Cygwin's /dev/clipboard.

Article source

New tricks in Vim

December 29, 2009 β€” I learned some new tricks with vim today.

4G - Move to line #4 mb - Mark the current spot as point b 'b - Go back to point b vim file1 file2 - open 2 files at once :e file - edit a different file :split file - split the current window and edit a different file (vsplit for vertical split) ctrl+w j/k - move down/up a window :hide - hide the current window :ls - show current buffers

Notes

1. [Vim Tips and Tricks](http://www.cs.oberlin.edu/~kuperman/help/vim/windows.html)

Article source

Use cat to combine or print the contents of a file

December 26, 2009 β€” Print the contents of a file:

cat filename.txt

Pipe the contents of the file to less, so you can view it one page at a time:

cat filename | less

In less, the 3 key commands are:

You can also do regular expression searches like in vim: /regex (n to go to next match)

Append file1 to the end of file 2:

cat file1 >> file2

Combine two files into a third file:

cat file1 file2 > file3

Article source

Run multiple commands at once

December 26, 2009 β€” Want to know how to run two or more linux commands sequentially? It's simple: use the semicolon ;.

For instance:

cd;ls

This will change the directory to your home directory, then list the contents. You can use as many semicolons as you wish.

Another option is to use && if you only want the next command to execute if the previous command executed successfully.

cd ~/myfiles; cp file.txt ~/backup/ && rm file.txt

This will only run the last command if the cp command executed successfully.

You can swap the && for || to run a command if the previous one fails.

Article source

Pipes, standard output and standard input

December 26, 2009 β€” In linux, standard input refers to the stream of bits that come from your keyboard.

Standard output refers to the stream of bits that appear on your screen(terminal).

You can change this around so that standard input comes from another source(say, a file) or so that standard output gets directed somewhere else (say, a file).

For example, say we wanted to email someone a file containing the contents of our home directory. We could do this:

ls ~ | vim -

This will pipe the output from the ls command to vim. You can then edit and save this file normally as you would in vim.

There's another type of pipe you could use for this example: greater than.

ls ~ > contents.txt

This will redirect the output to the contents.txt file.

You can also append output by using two greater than signs:

ls ~ >> some_file.txt

As you would expect, the less than sign can be used to direct a file as standard in.

Notes

Article source

The only 5 git commands you need to know

What's the difference between a great programmer with great social skills and a great programmer who does not use version control?
Nothing--they both don't exist!
If *every single great programmer* uses version control, why do some beginning programmers avoid it?
Because version control is made to seem intimidating. But version control is actually very easy!

December 23, 2009 β€” If you are new to version control, there are only 5 commands you need to memorize. Can you memorize 5 words? Of course you can.

So memorize these 5 words and you'll be practically an expert at version control:

init add status commit push

If you just start playing around with these 5 and only these 5 commands, you'll become a git master in no time.

Here's a simple practice session you can follow to start getting good.

What is git?

Git is a simple command line program like "wget" or "vim" that you install and use by typing commands. If you don't have git installed, try one of these commands:

yum install git-core apt-get install git-core sudo port install git-core

Let's say you're creating a new website for your mom and want to use version control to do it.

mkdir moms_website cd moms_website git init

This creates a git repository. Now type:

ls -a

Do you see the new ".git" directory? That's the git repository. It's basically a folder that stores the whole history of your project. Now, when you type a git command, it will do something with the files in that folder. That's all that's really going on. You never need to go into that folder manually, I was just explaining what git is doing.

Now, let's create a file and add it to your repository.

vim index.php Hello World :wq git status

This will show the presence of an untracked file, "index.php". Let's add this file.

git add index.php

You've now added the file to git, let's *commit* our changes.

git commit -m "first commit"

Now you've made your first commit.

The last command you'll need is *push*. It works like this:

git push

That will upload your repository to an online host like github so that other people can collaborate.

Create a github account and follow the instructions for creating a new repository to test out this final command.

That's it! Those are the 5 commands you'll use over and over again. Master those and slowly you'll start learning a few other helpful git commands.

Recap

git init git status git add *filename* git commit -m *"your message about what you changed and why"* git push

Article source

Use scp to download a file from a server

December 21, 2009 β€” You can easily grab a file off a server from the command line using secure copy:

scp user@domain.com:/home/user/fileyouwant.txt fileyouwant.txt

This will download the file from your server to your local computer.

Article source

Use sed to find and replace

December 20, 2009 β€” grep is to find what sed is to replace.

Say you noticed a few typos in a file. Say you typed imposible instead of impossible a few times in a file named "stuff.txt".

You can fix all occurrences using this one-liner:

sed 's/imposible/impossible/g' stuff.txt

Often it's more helpful to sed across multiple files.

sed -i 's/1.0.1/"1.0.1"/g' *.php

That's one command I used to add quotes around 1.0.1, which I forgot to do. Alas, it's not recursive.

Notes

Article source

Clear screen in cygwin

December 20, 2009 β€” Just hit ctrl+l

Article source

Bash variables

$ FIVE=5 $ echo $FIVE 5 $ echo "The number five is $FIVE"

Variable scope

December 18, 2009 β€” A shell script will launch a new shell that has a fresh scope.

$ FIVE = 5 $ vim myshellscript.sh #!/bin/bash echo $FIVE $ ./myshellscript.sh

Blank because the executed script doesn't affect the current shell. To make something affect the current shell, use the source command.

Export command.

Use the export command to change the scope of your variable to global for your shell.

To make a permanent global variable, add it to your .bash_profile like so:

MY_NAME=breck; export MY_NAME

Accept input.

Use the read command to accept input.

echo "What is 1+1?" read ANSWER echo "Your answer is $ANSWER"

Notes

1. Bash tutorial 2. Another tutorial 3. Bash parameters

Article source

How to fix an overwhelmed Gmail Accout

December 17, 2009 β€” I have used the same Gmail address as my main email for over 5 years. Since then:

As a result, my Gmail account gets inundated with email. Here are the stats:

My Gmail account had become a firehose.

This was bad for two reasons.

First, I started missing important emails. Not many, but a few per month. I had to write my own simple Bayes filter to find them.

Second, I couldn't do what most people do: use my inbox as a "todo" list. Most people immediately archive or delete an email when they're done with it. This is a great organizational trick. I wanted to do this again.

I considered starting a new email address and giving it out to important people, but that seemed like it would make my life more complex.

As it turns out, the solution was simple.

Notes

1. Thanks to this site for telling me how to archive all my gmail messages.

Article source

Seven aliases that can save you time

December 13, 2009 β€” Add these to your .bash_profile to speed up common tasks:

alias apr='sudo ./usr/sbin/apachectl restart' # restart apache alias www='cd /var/www/html/' # enter your root web directory alias hconf='vim /etc/httpd/conf/httpd.conf' # edit your apache config file alias eprof='vim ~/.bash_profile' # edit this file alias rprof='source ~/.bash_profile' # reload this file (after making edits) alias pconf='vim /etc/php.ini' # edit php.ini alias lsd="ls -l | egrep '^d'" # list only directories

Article source

List only directories using ls

December 13, 2009 β€” the "ls" command lists all files and directories. What if you just wanted to list the directories?

ls -l | egrep '^d'

Add this to your .bash_profile and now you can just type "lsd" when you want list only directories. Who said lsd was a bad thing?

alias lsd="ls -l | egrep '^d'"

Notes

1. Found this tip here.

Article source

Turn off bell in cygwin

December 11, 2009 β€” The beep on tab completion in cygwin can be annoying. Here's how to turn it off:

vim ~/.inputrc (Then add or uncomment this line) set bell-style none

Save and restart your shell.

Article source

git clone without history

December 11, 2009 β€” If you're using a library from git in another project, you may want to download it without the history. Here's how:

git clone --depth 1 your_repo_url

http://git.or.cz/gitwiki/GitFaq#HowdoIdoaquickclonewithouthistoryrevisions.3F

Article source

Learn Symbolic Links in 30 seconds

December 5, 2009 β€” Symbolic links are simple and can save you time.

Here's all you need to know:

ln -s actual_file_or_directory new_symbolic_link_name

Let's do a real example from your home directory:

ln -s /var/www/html/yourwebsiteroot/yourblog blog

Now when you login to your server you can just type "cd blog" to access the "/var/www/html/yourwebsiteroot/yourblog" folder.

Here's one more example from your home directory:

ln -s /etc/httpd/conf/httpd.conf httpd.conf

Now you can just type "vim httpd.conf" and edit your httpd.conf file quicker.

You can do all kinds of neat things with symlinks, but the simplest uses are often the ones you'll use most. (One of the cooler uses of symlinks is for deploying different versions of websites).

Notes

Aliases are another great time saver.

Article source

Create a new user in Linux

December 5, 2009 β€” Creating a new user in Linux is dead simple.

useradd bob passwd bob

This adds a user "bob" and then prompts you to enter a password.

Article source

Use aliases to save time

November 28, 2009 β€” Using aliases in Linux can save you a lot of time. Here's an example:

alias www='cd /var/www/html/'

Instead of typing "cd /var/www/html/" to enter your web directory, now you can enter "www", a savings of at least 14 keystrokes each time you use it.

This one will switch to the usr/sbin directory, restart apache, then switch to your home directory:

alias apr='cd /usr/sbin/;sudo ./apachectl restart;cd'

You can also just do:

alias apr='/usr/sbin/apachectl restart'

But the earlier example shows how you can string multiple commands together.

Bonus tip

Place these aliases in your HOME/.bash_profile file and they will be available each time you login to your machine.

Notes

  1. "At least" 14 keystrokes, because the # of typos increases as the # of characters typed increases.

Article source

Use git to get the latest source code only

November 27, 2009 β€” Say you want to get just the source code of a git repository without all the history(without the ".git" directory). Use this:

git clone --depth 1 git://github.com/breck7/brecksblog.git

Article source

How I organize my Dropbox

November 27, 2009 β€” Gmail encourages users to "search not sort". I do that. Email works great for search. For files though, sorting is often better.

I have well over 20GB of files synced onto my Dropbox. The rest of my computer is spotless(I use Windows 7 and do a fresh install every 30-60 days). But the Dropbox can get cluttered. I take a simple approach that is a hybrid of search and sort. Here's how to do it:

Put files into 2 sections:

End Result

Here's what the end result may look like:

-My Dropbox --Music --Photos --Blog --eBooks --Company Docs --Search

The first directories are the ones you access a lot and are well sorted and maintained. The last directory is a mess of random files and folders that you've collected. You can browse through it, but it's easier to just run a search on it when you need something.

This a hybrid of search and sort.

Notes

  1. All of my coding work is done in my cygwin home directory and then pushed to github. These are the only files that I don't store in Dropbox, but I push them frequently so don't worry much about losing them.

Article source

What non-web software do you use the most?

November 26, 2009 β€” Here are the programs I use everyday, every week, and every month.

What software do you use the most? What are your "must haves"?

My Machines

My primary development machine is a basic Compaq laptop with 4GB, 230GB, x2 Core Athlon, running Windows Vista/7.

My secondary machine is an Asus EEE netbook with XP which is great for working on the road, at a coffee shop, or on a plane.

Most of our servers are slices from slicehost running Fedora.

On all my development machines I run Cygwin. Programs I run in Cygwin are noted by *.

The following is a list of the programs I use on my development machines.

Daily

Weekly

Monthly

Article source

Use the linux find command to find files

November 25, 2009 β€” Say you're looking for all files starting with the word "breck". Using the linux "find" command, it's simple:

find -name "breck*"

Will search the current directory and all subdirectories for files starting with the word "breck".

Article source

Fix permission problems using chmod

November 25, 2009 β€” Permission problems driving you crazy? Here's a common command to fix them:

chmod -R 755 directoryname

Change the file permissions of all directories and files in directoryname to 755.

Article source

cron in 2 steps

November 25, 2009 β€” cron lets you schedule linux commands to execute on a regular basis.

For instance, say we want to run the command "wget http://yourdomain.com/backup.zip" each hour, that downloads a backup of one of your websites.

Step 1: Write the cron command

Here is the cron command you would need:

0 * * * * wget http://yourdomain.com/backup.zip

You can either memorize the crontab syntax, or just this simple Javascript crontab gui will easily generate the code for you.

Step 2: Append the command to your crontab file

There are 2 crontab files that get executed every minute. 1 of them is found in /etc or similar and is controlled by root only. In addition, each user can have a crontab.

To add your command to crontab type:

crontab -e

This will open vim, and you can then paste and save your crontab file.

Viola!

Notes

1. If you are using cron in cygwin, follow these steps after installing the cron package with the cygwin installer to install cron as a service:

cygrunsrv -I cron -p /usr/sbin/cron -a D net start cron

If there are problems, check the cron.log file in your home directory.

Article source

Use grep to search multiple files at once

November 22, 2009 β€” grep lets you search through multiple text files at once.

I recently removed a feature from a project and wanted to make sure I removed all references to it. With grep it was easy:

grep -r picture *

This searched all the files in my current directory as well as all the files in each subdirectory, and printed the filename and line where the word "picture" appeared.

Then I went in and changed the 2 files that needed modifying.

Had I wanted to just search the current directory (without the subdirectories) I could have done:

grep picture *.*

This would have searched through all the files in the current directory.

Had I wanted to search in just 1 specific file:

grep picture filename.txt

Had I wanted to do a case insensitive search:

grep -ir picture *

Had I wanted to exclude a directory and done a case insensitive search:

grep -ri "searchstring" --exclude="*\.svn*" *

(grep uses regular expressions)

Links

Article source

Use rsync to deploy your website

November 21, 2009 β€” rsync is a nifty little tool that can deploy a website to a testing or production server.

Here's a one line command to deploy brecksblog:

rsync -arvuz /home/user/brecksblog breck32@breckyunits.com:/var/www/html/ --exclude '.git' --exclude 'deploy' --exclude 'README' --exclude 'posts.php'

This one line will do a fast, incremental file transfer from my local directory to the web server.

I saved this file as "deploy", and then run it by typing:

./deploy

The exclude option will exclude a file or directory from being synced.

Here's an explanation about the options I use. I use rsync with these options:

rsync -arvuz /src/foo/ /dest/foo

This will copy the contents of /src/foo/ into /dest/foo. Options explained:

Notes

1. If you are using a different port for SSH, say 333 do this: --rsh="ssh -p333"

Article source

SSH into your server without a password

November 20, 2009 β€” On your server:

vim /etc/ssh/sshd_config

Make sure allow rsa and the other RSA lines are uncommented

service sshd restart

Make sure the file ~/.ssh/authorized_keys exists(if it doesn't, make that directory and touch that file).

Run this on your client computer:

ssh-keygen -t rsa

(hit enter a bunch of times)

cat ~/.ssh/id_rsa.pub | ssh YOURUSERNAME@YOURDOMAIN.com "cat - >> ~/.ssh/authorized_keys"

Bam! Login without a password!

This is my setup on Fedora VM's running at slicehost.

If you are having problems

1. Run sshd with the verbose option. -v

2. The problem is likely due to bad file permissions. The owner of the directory .ssh should be your user/group. The file permissions on it should be: 755. (I think) authorized_keys should be the same owner/group and file permissions should be: 644

Article source

Get VIM Working in Cygwin

November 20, 2009 β€” I had trouble getting VIM behaving as expected under Cygwin. Specifically, the backspace and arrow keys were behaving badly in insert mode.

The solution was dead simple. In your root directory just run:

touch .vimrc

Links

Article source

About this Blog

November 18, 2009 β€” I constantly want to share little coding tidbits, snippets, or stories on how to solve specific little coding problems. Instead of clogging up the main blog with these shorter, more specific technical posts, I decided to launch a second blog.

This is it.

Article source

Gmail icon Twitter icon GitHub icon
Built with Scroll v22.3.0