Live data from Hacker News

Ask HN: Best Unix Tricks?

news.ycombinator.com

101–107 of 107 posts

Re: Ask HN: Best Unix Tricks?

#101
post #24

On OS X pbcopy and pbpaste are useful for working with clipboard data. Copy data from a web-page or whatever, manipulate it on the command-line to get what you want. Putting reasonably large files in the clipboard can be useful too. For example: curl http://news.bbc.co.uk/ | pbcopy # copies a web-page to clipboard Depending on what you are doing it can be very useful: pbpaste | sort | pbcopy # sort the contents of th…

open -a finder is a pretty handy way to open a finder window to current directory.

You can just pass the directory, no need to specify finder.

  open .
  open /Applications

Re: Ask HN: Best Unix Tricks?

#102
post #85
post #24

On OS X pbcopy and pbpaste are useful for working with clipboard data. Copy data from a web-page or whatever, manipulate it on the command-line to get what you want. Putting reasonably large files in the clipboard can be useful too. For example: curl http://news.bbc.co.uk/ | pbcopy # copies a web-page to clipboard Depending on what you are doing it can be very useful: pbpaste | sort | pbcopy # sort the contents of th…

You can also specify the app to open using the -a flag and specifying the full name of the app (with correct spacing and capitalization), e.g.: open -a 'TextEdit' foobar.txt open -a 'TextMate' foobar.txt

I also find it useful to have multiple instances of certain applications sometimes:

  open -na safari

Re: Ask HN: Best Unix Tricks?

#103

One of the little things that it took me a while to find out about: echo '2^16' | bc 65536 'bc' is a complete calculator language interpreter that also functions quite nicely as a simple calculator. If you need to do any heavy lifting check out: http://en.wikipedia.org/wiki/Bc_programming_language

As a follow-up to that: It might seem basic but understanding pipes (the '|' symbol) is very useful! The pipe symbol 'sends' the output of one command to another. Examples:

  tail -n 500 log/production.log | less (display the last 500 lines of your production logs one page at a time)

  cat config/routes.rb | wc (count the number of words and lines in a config file)

Re: Ask HN: Best Unix Tricks?

#104
post #26
post #16

I love the '-exec' flag for find

Me too. Find also has -delete, so you don't have to type -exec rm -rf '{}' ';'

It's worth noting that -delete is more efficient than the above, and indeed safer.

Efficient because additional exec's are removed, for the most part find can perform the unlink itself. Safer because a replacement version for rm cannot be inserted into the user's path.

Before -delete was added to find I always found perl faster:

  find2perl scratch -type f -size 0 -eval 'unlink' | perl
or

  find2perl scratch -type f -size 0 -exec rm {} \; | perl
The last case does not actually call rm but is hard-wired to use unlink().

It may still be faster.

Re: Ask HN: Best Unix Tricks?

#106

I wrote a bash function yesterday which allows you to cd ... to move up two directories instead of having to cd ../.. Want to move up 4 directories? cd ..... Pretty useful, imo. Check it out http://blog.jerodsanto.net/2009/09/cd-up-up-up/

Can't remember where I found this, but this function allows you to go up any level of dirs without specifying an infinite number of aliases.

Usage: up {x} where x is the number of directories you want to go up (e.g., 3)

function up() { local arg=${1:-1} local dir="" while [ $arg -gt 0 ]; do dir="../$dir" arg=$(($arg - 1)); done cd $dir >&/dev/null }

Post reply on HN