Live data from Hacker News

Ask HN: Best Unix Tricks?

news.ycombinator.com

1–10 of 107 posts

Ask HN: Best Unix Tricks?

#1
HN, what are your killer Unix tips? I've been using and learning about Unix both at university and at home for almost a year, and I was wondering -- what are the things that, when some wizard showed them to you, you wrote down as soon as you could so you wouldn't forget them? Is there some trick that saves you a buttload of time? Some script that makes your job way easier? Some command that made you smack your forehead in frustration when you found out it could be that simple?

Note: I'm personally looking for tips for working in bash (specifically OS X stuff if that makes any difference), but if there's some amazing thing that only works in zsh under Debian or something, feel free to suggest those too -- it's sure to be useful for someone.

I had a quick search for previous topics on this, but all I found was http://news.ycombinator.com/item?id=103725 which had a couple of badass things in the discussion.

Re: Ask HN: Best Unix Tricks?

#2
It's a great kill-ur unix tip but you should never fall for it:

    :(){ :|:& };
Oh, the temptation... if you're thinking of cutting & pasting that into the shell of your machine google for 'bash fork bomb' first, I've left off the final ':' to set it off.

Anyway, on a more serious note, after an installation procedure that was a lot of work, if you want to document the whole thing use:

    history | cut -d ' ' -f 5-
To give you all the commands you executed in the last little while, cut & paste that and you have a pretty good start for a how-to.

It's not a 'great killer unix tip', but it works for me.

Edit: Another one just came to mind, the ps command has a very useful option that is quite undervalued imo:

    ps ax --forest

Re: Ask HN: Best Unix Tricks?

#8

  set -o vi # vi like behaviour of the command line.
Use "/" to search through the history, "y" to copy, "p" to paste etc..

  cat > somefile.txt 
Type EOF in a new line to finish.

Re: Ask HN: Best Unix Tricks?

#9
This is more a general hint than a single tip. Years ago I tried to improve my Unix skills by doing everything at home from the command line -- burning CDs, playing music, copying trees etc. I learnt a lot but this exercise was much less convenient and more mentally demanding than using a gui. Recently I have become much more lazy.

Re: Ask HN: Best Unix Tricks?

#10
post #4

!!:s/sl/ls executes last command but replaces first occurence of sl with ls. (can also be written as ^sl^ls) !!:gs/sl/ls same as above, but now global replace

Similarly, !! refers to the last command, so you can write "sudo !!" to execute the last command as root. Helpful for apt-get, among other things.

!$ refers to the arg to the last command, so you can write:

  $ ls -l /foo/bar
    # .oO(aha, it's a stale link i don't want)
  $ rm !$
  rm /foo/bar
  $
Post reply on HN