Find all scroll files not matching a pattern:
grep -rL "endSnippet" *.scroll
I'm going to start sticking my Sublime Text settings and plugins here.
"goto_anything_exclude_gitignore": true,
This setting is new devBuilds in ST4 (Build 4130 released on March 23, 2022). Set it to true and any file that matches your .gitignore
patterns will not show up in ST's autocomplete, navigation, et cetera. Found this one on StackOverflow.
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.
git clone ssh://breck@example.com/home/breck/project
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
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.
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
Ever want to non interactively execute commands on a remote server?
ssh breckyunits.com 'df -h'
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
Just read an awesome overview of the command line
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
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.
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
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
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
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
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
Understanding and mastering bash history can:
Here's a terrific guide on using Bash History effectively.
I just came across a neat command line tool, ngrep, via this site.
Sample usage:
ngrep -q -W byline "^(GET|POST) .*"
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
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
Where DISKNUMBER = disk1 or disk2 for example. Add that leading "r".
Back in Disk Utility, unmount/eject the disk.
Done!
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
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.
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.
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.
Here's my setup. This is kind of for my reference but you can also check it out if you're interested.
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.
Just create a new A Record with * as the host and your IP in the points to field.
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.
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++:
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
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:
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.
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.
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.
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.
find . -type f -name ".DS_Store" -exec rm {} \;
The file is located at:
windows/system32/drivers/etc/host
Mac hosts file:
/private/etc/hosts
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:
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.
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 FileWriterTestClass with a test method named testFileWriter that you want to run.
Do this:
phpunit --filter testFileWriter FileWriter_Test_Class tests.php
From your rails app's root:
cat tmp/pids/server.pid
kill -9 {pid}
Add this to your /etc/defaults/etc/ssh_config file to prevent SSH servers from disconnecting you after inactivity:
ServerAliveInterval 60
.bundle
db/*.sqlite3*
log/*.log
*.log
tmp/**/*
tmp/*
doc/api
doc/app
*.swp
*~
.DS_Store
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
Add the -R
option to ls
to recursively list the contents of a directroy.
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
One of my most frequent commands is:
git commit -am "message"
I made a simple alias to save keystrokes:
alias gca="git commit -am "
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.
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.
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 "lowercasetable_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".
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:
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.
#!/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.
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 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 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.
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++.
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:
Edit #2:
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.
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:
The main plugin I've used so far is Explorer. I'll update this in the future with other ones.
That's it!
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.
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:
cd -
To go back to the last directory you were in. Basically an "undo" for the cd command.
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.
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.
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.
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?
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.
Of course there are benefits to having multiple directories, otherwise this style wouldn't be so popular.
It's nice and neat to have your files tucked away into nicely labeled directories.
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?
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.
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:
📁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:
📁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.
The optimal solution is to not put your stuff into drawers, but to:
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
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.
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.
Display the current unix timestamp:
date +%s
Handy way to view it from the command line.
nullData = new Array( );
myChart.setDataArray(nullData,"null");
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.
Change the owner of a file or folder:
chown newowner filename
Change the group of a file or folder:
chgrp newgroup filenameorfoldername
It's very simple thanks to pipes.
mysql -u username -p < sqlfile.sql
Make sure your sql file has a "use database" line.
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.
#!/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
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
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
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
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.
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.
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.
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.
git init
git status
git add *filename*
git commit -m *"your message about what you changed and why"*
git push
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.
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.
$ FIVE=5
$ echo $FIVE
5
$ echo "The number five is $FIVE"
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.
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
Use the read command to accept input.
echo "What is 1+1?"
read ANSWER
echo "Your answer is $ANSWER"
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.
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
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
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.
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
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).
Aliases are another great time saver.
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.
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.
Place these aliases in your HOME/.bash_profile file and they will be available each time you login to your machine.
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
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:
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.
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 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.
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".
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.
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.
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.
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!
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.
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)
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:
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.
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
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.