Live data from Hacker News

Ask HN: Best Unix Tricks?

news.ycombinator.com

11–20 of 107 posts

Re: Ask HN: Best Unix Tricks?

#11
My tip: pushd without an arg pushes the current directory onto the stack, and then switches to what popd would have. Hence, repeated invocations of pushd let you cycle between two directories without thinking.

$CDPATH is also amusing:

  $ mkdir /foo/bar/baz
  $ cd baz
  bash: cd: baz: no such file or directory
  $ export CDPATH="/foo/bar"
  $ cd baz
  $ pwd
  /foo/bar/baz

Re: Ask HN: Best Unix Tricks?

#12
cd - (Go back to previous working directory)

sudo !! (Run the last command as root)

:w !sudo tee % (Save a readonly file in VIM)

> tmp.file (Empty a file)

cmd !$ (Run cmd with previous commands arguments)

They are just some of my favorites.

Re: Ask HN: Best Unix Tricks?

#13
post #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.

Or just omit the <<EOF and press Control-D to finish (assuming you have not done anything strange with stty).

Re: Ask HN: Best Unix Tricks?

#17
post #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.

Or just omit the <<EOF and press Control-D to finish (assuming you have not done anything strange with stty).

touch somefile.txt

Re: Ask HN: Best Unix Tricks?

#19
post #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…

Further to filtering the history: I use "script" to record terminal sessions. See "man 1 script".

Re: Ask HN: Best Unix Tricks?

#20
post #6
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

I have that one aliased as sl2ls ...

Why wouldn't you just alias sl to ls? It'd be much less typing.
Post reply on HN