Since it seems like more and more people are taking advantage of mSysGit and Cygwin to take advantage of Git, I thought I’d take a few minutes to talk about the utilities and tips that I use to help make things easier on myself when using my Cygwin environment.
Sometime in the last two years, I went completely over to Cygwin and Bash for my command line environment on Windows. I’ve been an avid FreeBSD, then Gentoo and then (after having kids and not a lot of time) Ubuntu user since about 2000/2001. In that time there are a few utilities that I’ve come to love that Windows has always left me wanting for.
1. SSH
SSH (secure shell) is an encrypted networking protocol that can be used for a myriad of things. At it’s most basic, you can think of it as a secure, encrypted version of telnet.
You need to have an SSH daemon (think windows service) installed and running in order to connect. You can find a decent tutorial here. As well, you will need a client. I vastly prefer Putty.
Not only can you login and access your computer remotely (via the command line interface) but there are a ton of options that it opens up for you. One of the most useful, in my opinion, is SSH Tunneling. Imagine being away from home, but needing to remote desktop into your home computer (for something you couldn’t do via command line right?) but you don’t have an RDP port open on your firewall. Well, simply set up a tunnel in putty:
What this does create a tunnel from port 9000 on the client machine (the one you are using) to port 3389 on the host daddicant (in this case, my main computer). Now, if I want to remote desktop into daddicant, despite the fact that I don’t have the firewall open for RDP, I pull up remote desktop and….
When I connect to localhost:9000, it will work as if connected to daddicant:3389 via the magic of SSH Tunneling. I really appreciate this from the point of view of not needing to manage my external firewall aside from providing an open SSH port.
2. SCP
Now that you’ve got the SSH daemon up and running, you can easily start copying your files back and forth. Have a zip file you need at home? Just use:
scp file.zip mendicant@beigesunshine.com:/home/mendicant/
After entering your password (or if you’re up to it, a pubkey file, you can google that one or ask me to post about it later) it will connect and copy your file over. You can also use sftp in a similar fashion.
3. Screen
So, now we’re connected remotely, but what happens if A) I want to use multiple consoles or B) what if I’m on my way home but want to keep my session open?
Both are solved via the Screen utility. Start it using “screen”.
Now that you’re at the prompt there are a bunch of keyboard shortcuts you can use, most beginning by pressing Ctrl-A followed by another key. There is a tutorial here, but here are my most used commands.
Ctrl-A C : Create a new console. You will now have two consoles running.
Ctrl-A [0-9] : Switch to your opened consoles.
Ctrl-A D : Detach from screen. This is the most useful because now that you’ve got a console running, you may have a long running process going which you don’t want to disturb when you log off. So just detach!
When you are ready to reattach, just log back in and type “screen –x”. This will re-attach screen to the already running one where your process is still happily chugging away and has been, even when you were logged off!
4. tail
You can use tail to show the last 10 lines of a file (or a configurable number using the –n switch). But the most useful part of tail is the –F command. Using “tail –n 100 –F mylog.txt” will show the last 100 lines of mylog.txt, but it won’t exit. Instead, it will continue to listen to the file and display anything that is written to the file to the screen. This means that you can have a log file scrolling on your screen as it gets written without having to have an app open refreshing. Try this:
1. Open screen.
2. touch test.txt
3. echo “line 1” > test.txt
4. tail –f test.txt # YOu should see it output “line 1” to the screen
5. Ctrl-A C to open a new screen
6. echo “line 2” >> test.txt
7. Ctrl-A 0 to go back to the original screen, you will now see “line 2” on the screen as well!
You could do the same with the –f option of tail, but the –F will reconnect if the file is truncated, meaning those of you with rolling logs would be better served using –F.
5. Finding files
Can’t find that .docx file your wife was working on?
find /cygdrive –iname “*.docx” –type f
will find all of the .docx files on your system. the –iname means Case Insensitive Search. The –type f means only find files.
I use this a lot to remove .svn files via:
rm –rf `find ./ –iname “.svn” –type d`
6. History Search
This is probably the biggest reason I use bash over powershell, cmd or other windows command line utils. Open your ~/.inputrc file and add the following lines:
"\e[A":history-search-backward
"\e[B":history-search-forward
These lines will add back and forward searching to your up and down arrow keys (you could probably bind them just as easily to Ctrl-J and Ctrl-K for you VIM junkies out there).
Anyway, to test go to your prompt and type “source ~/.inputrc” to make sure the changes have taken and then type “cd” and press your up arrow key. This will get you cycling through all your commands that have started with “cd”.
7. Ctrl-U
This one is a small one, but if you need to clear everything that’s before your cursor, type Ctrl-U and it will clear the prompt. Similar to using “esc” in cmd. Hint: This also works in some other apps, such as in the address bar of Opera.
That’s all that I can think of for right now. I’m sure there’s a lot more but those are some of the biggest ones. Let me know yours and I’ll update with the best ones!