In the past year, I’ve been using the terminal (recently started using zshell which I recommend checking out if you haven’t heard of it) a lot more and along the way I’ve found a couple commands which I wish someone had told me about sooner.
This command concatenate’s the contents of a text file. This is perfect for when you need to quickly check a file, but don’t want to open it up in an text editor like vim or TextMate
I use OSX’s spotlight utility a lot to quickly open up applications or find files, so when I started using the terminal more I quickly wanted a way to search my disk for files. Thankfully, people smarter than me already made it a part of bash. You pass it a query and it outputs a list of full path results. Perfect for searching your entire drive, but sometimes you just want to find something within the current directory (and it’s subfolders). For that, we go to my next favorite
find [path] -iname "[filename]"
find [path] -type "[filetype]"
Example: find . -iname “abc*” will return a list of files in the current directory (including subdirectories) who’s name start with “abc” (case-insensitive and the asterisk is a wildcard character which simply means the rest of the filename doesn’t matter). This is one of the those functions that are really powerful, but have so many options that it can be a bit intimidating to learn. The two cases above are the way I use it most of the time.
grep [query] [filename]
The last of the useful searching utilities is grep. This is used to search for a string within a file.
I just came across this one recently — it turns out bash as it’s own calculator built it. It’s only good for simple math and for that I usually use either use Ruby’s irb or Wolfram Alpha, but it’s good to be aware of it just in case. One helpful hint to save some googling later: to change the displayed precision, use the command scale=x where x is the number of digits you want visible after the decimal.
echo “Hello, Bob!” | sed ‘s/Bob/Gregg/’
Ok, so this particular example isn’t very useful, but it demonstrates both the pipe ( | ) and the sed function which can both be pretty neat. The above example will output “Hello, Gregg!”. The pipe takes the output from echo and passes it to sed. sed then replaces the first instance of “Bob” with “Gregg”. Your imagination is the limit with what you can do with this one.
Strictly a ‘neat’ utility that displays a formatted calendar. It’s good if you need to check a day of the week or something.
So that’s it. It’s a short list of some bash utilities that help me do my thing. Here’s a site which goes through some other nifty tips and tricks. Have any lesser-known utilities I missed that you find indispensable?