Command line tools for the novice
andymatthews.net
andymatthews.net
..plus Ack is 4 or 5 times faster than grep.
Ack is implemented in Perl, which has a full featured regex engine, but is not generally known for being super fast. It's going up against GNU grep, which is a highly tuned regex engine, which is very fast. The ack guys make the argument that ack is faster because it doesn't search stuff you, the developer, don't care about, which in my estimation is horseshit. Try it yourself, I threw a regex at my email spool from the 90s which is about 128M. I made sure it was in disk cache first and then did:
chunky:~$ time egrep -c -i '(d|m|j)ingle' RMAIL
23
real 0m3.129s
user 0m3.075s
sys 0m0.051s
chunky:~$ time ack -c -i '(d|m|j)ingle' RMAIL
RMAIL:23
real 0m26.208s
user 0m26.060s
sys 0m0.121sA lot of people do want/need PCRE, which ack relieves the pain point for.
chunky:~$ otool -L /usr/bin/grep | grep pcre; grep --help | grep perl
/usr/lib/libpcre.0.dylib (compatibility version 1.0.0, current version 1.1.0)
-P, --perl-regexp PATTERN is a Perl regular expression
Many users choose ack over grep for the same reason they choose htop over top, tmux over screen, zsh over bash, and postgres over mysql -- the perception that one is significantly better than the other. This advocacy often comes from folks whose use and understanding of either tool is superficial at best.Once the standard commands are learnt then by all means use ack if you understand the trade-offs and can fall back to the normal way when on another system or when ack's insufficient.
As described here ( http://blog.sanctum.geek.nz/default-grep-options/ ), you could use GREP_OPTIONS, but then you run into the problems here ( http://brainstorm.ubuntu.com/idea/24141/ ) -- scripts that use grep without clearing GREP_OPTIONS will fail.
Do you have an alias? Or another script that wraps grep?
find . -type f -name \*.[ch] | grep -v \\.git | xargs grep something
Normally my directories aren't going to have .bzr, .git, .hg, and .svn in the same directory tree, but if they did it wouldn't be that much harder. alias nocrap="egrep -v \\.\(git\|bzr\|hg\|svn\)"
find . -type f -name \*.[ch]| nocrap | xargs grep whatever
This is how I do it because I'm lazy. I suppose I could tell find to exclude directories so it wouldn't have to recurse into them, but the list of directories and files in my source tree is almost always in buffer cache anyway, so I never really worry about it.(Edited to use ✳ to workaround unescapable mark-up.)
#! /bin/sh
exec egrep --color "$@"
(Not a bash, my shell, alias because they're unavailable in many contexts.)egrep is AKA grep -e. I don't use GREP_OPTIONS because it pollutes those that don't expect it, as you pointed out.
At the top of a particular source tree I may have a ./g that knows more about what's interesting.
#! /bin/bash
find \( -name .svn -o -name tags \) -prune -o -type f -exec egrep "$@" {} +
It checks -name before -type because the latter needs a stat(2).For other things I do ad hoc queries using grep, find, xargs, etc.
That doesn't seem like a good comparison to me. I use htop over top because it has features that top doesn't have (the funky coloured ASCII graphs). I use tmux over screen because unicode seems to work without any effort on my part. I use bash because I have never looked at zsh in detail and don't care enough. I use postgres because I like its license better than mysql and its not controlled by any one company.
So its not at all about the perception that one is significantly better than the other, at least for me, its about specific features (license is a feature). Or in the case of zsh vs bash, where there are no specific features, I use bash because it seems to be the default on most distros.
No, that's not how it works with grep. The shell is expanding the glob * to all the things in the current directory that don't start with a dot and that's what grep sees. It then reads each of them in turn or if they're a directory it descends into them recursively; the * plays no further part in the descent.
$ mkdir x
$ cd x
$ echo aa > .y
$ mkdir y
$ echo aa > y/z
$ echo aa > y/.z
$ grep -r a *
y/.z:aa
y/z:aa
Notice that hidden files in the top-level (.y) don't show up, but those in subdirectories (y/.z) do.It's kind of a gotcha. Fortunately one rarely finds dotfiles in subdirs.
grep will run through all those files, if any, and recurse through all the directories, if any. grep recursing down a directory will process all the files/subdirs in that directory. (which includes . files).
If the shell can't expand the * , grep will receive a literal * and try to open a file/directory named * .
The reason there are files that start with a dot is that ls(1) and globs starting with an asterisk ignore them by default. ls needs -a (all) to show them. Some shells, but not bash, provide a globbing syntax for the recursive expansion you think you require here.
But if, as normal, you want to grep recursively down from the current directory then that's what you should specify; just pass grep one path to search, dot, the current directory.
grep -r foo .
This is very basic knowledge of the Unix command line for a programmer and I highly recommend the classic text _The Unix Programming Environment_ by Kernighan and Pike to learn the philosophy behind Unix. It has nothing on window systems, ssh, etc., nor should it have as that's modern-day noise that hides the essence. It and Kernighan and Ritchie's _The C Programming Language_ were the key books every starting-out Unix programmer read. http://www.amazon.com/exec/obidos/ASIN/013937681X/mqq-20Is anyone in a similar situation? If you switched, was it worth it?
Also just for the record zsh is installed by default on many OSes, including OSX, so you might be surprised to learn that some of your servers already have it.
Minor tip - you can use the same .aliases file for both shells, I just source it in .bashrc and .zshrc on my dev machine. So if I ever need to switch to bash for some reason, my most used aliases are still available.
If you switch, don't forget oh-my-zsh [1].
Is available on *nix systems as part of Plan 9 from User Space: http://plan9.us
I've tried living in iPython and eshell, and while I prefer python or lisp as a programming language to almost anything, neither is nearly as quick and dirty as sh for doing stuff with stdin/out from a pipeline of tools. I don't quite know what a better language than sh would look like, but a man can dream.
Also, universe, if you're going to get on fixing the sh problem, can we restructure how the shell operates entirely? Don't do argument expansion! This is one area where the Unix Haters got it really right.
'ls *.jpg'
is passed expanded to 'ls 1.jpg 2.jpg 3.jpg' BEFORE ls is run. This choice means that if a program wants to use another style of regular expression (ie: pcre), it must be enclosed in quotes on the shell, ie: "ls | grep -P '.*jpg'"W3C: http://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contr...
I'll take a look when I've got a few more moments to see where various of the W3C's recommendation levels fall on my own assessment of readability.
Speaking as a 40-something with decent vision, the site isn't impossible to read, but it's most definitely annoying. For someone with compromised or old eyes, it would be much worse.
For crazy power users, I'm a huge fan of vim+tmux+zsh combo as described in http://www.drbunsen.org/text-triumvirate.html
You can run tmux in iTerm2.
GUI (Mac OS X): MacVim, ClipMenu, YummyFTP, Quicksilver, iTerm2, SourceTree, FileMerge, Chrome dev tools.
GUI (Linux): GVim, Glippy, FileZilla, Synapse, Gnome terminal, SmartGit/SmartSVN, Meld, Chrome dev tools.
I don't use any advanced features of iTerm, though, I just use it over Terminal.app because it supports 256 colors. We have not been updated to Lion or Mountain Lion at work so Terminal.app is still limited to 16 colors for me.
1. [Autojump](https://github.com/joelthelion/autojump), first implementation and what z was based on, written in python. Functionality similar to z.
2. [Fasd](https://github.com/clvv/fasd), feature-rich fork of z, support for files, support for more shells and platforms (BSDs, Android).
Also, I keep seeing iTerm2 get mention. You mentioned split panes and what not, which are not critical when I have that with tmux (as well as the tabs). And while before Lion, Terminal's color support was weak, that's changed. So, I really wonder what the big reason for using iTerm2 is over Terminal?
Tmux is not only a window manager, it allows me to:
* login only once into my VPS and run all kinds of things in parallel
* leave a session at work and come back to it at home
* run processes in the background with textual feedback
Its precursor, screen, works similarily with a little less bells and whistles.
tmux is like screen, only it has more features.
Which is really why I asked. What does iTerm2 offer that I can't get from tmux and current terminal? People always refer to iTerms ability to do multiple windows/tabs. For me, this is useless if it's just available client side.
There is an amazing amount of freedom being able to just shut down, move to another computer, and be able to pick up exactly where I left off.
Honestly, anyone who puts code into a blog post without even bothering to RUN IT THEMSELVES first should get out of the business of writing even slightly technical posts. Feel free to review food without tasting it first instead; at least then it's subjective so you can't be wrong. Here, the "/usr/bin/python: No module named SImpleHTTPServer" you'd get if you had arsed yourself to actually check your code to be valid easily clues the reader into your carelessness.
In addition to all that, there's the audacity of posting a post for the "novice" to hacker news when the post is clearly neither relevant for any hacker who has performed any real work or has any real intelligence (they would already know all the mentioned tools) and neither is news for all the tools mentioned are fairly old and well known. You might as well just call us all stupid and dilute the front page further with crap.
I appreciate you taking the time not only to read the post, but to offer your opinion and corrections to my article. I've updated those two mistakes you pointed out.
As I understand it, SimpleHTTPServer offers basic functionality to the user, which is all I use it for...a quick way to view pages using AJAX requests. I attempted to use http.server instead, but as I'm not running Python 3.0 I'm not able to use that option. Since Lion ships with Python 2.7, and many people are using that OS, I felt safe leaving the reference in place.
I do run the server function myself, many times a day in fact, but I made the mistake of typing it in manually rather than copying and pasting from my .zshrc file.
This post isn't intended for hackers, thus the title "...for the novice". The comments I've received about the article thus far, most likely from people not as advanced as yourself, have been positive and appreciative for the information. I'd love to hear some of the tools that improve your workflow daily...would you care to share them?