Live data from Hacker News

Ask HN: Best Unix Tricks?

news.ycombinator.com

41–50 of 107 posts

Re: Ask HN: Best Unix Tricks?

#42
nc lets you talk directly to a remote host, and strace allows you to eavesdrop on an arbitrary process' communication with a remote host (or with anything, really).

Actually, I rather hope there's something better than strace for the latter. But it works.

Re: Ask HN: Best Unix Tricks?

#43
post #16

I love the '-exec' flag for find

...except, that you should rarely use it. :)

It is much, much faster to pipe commands to "xargs". For instance:

    find . -name core -print0 | xargs -0 rm -f
Since "find" doesn't know when your command is multi-file, it has to run the command once per file; whereas, "xargs" can run it as few times as necessary. It works well for any command that accepts "a list of files".

In the example above, the -print0/-0 are a safety net to null-delimit filenames so that it doesn't matter if any of the paths contains a space.

Re: Ask HN: Best Unix Tricks?

#45
post #5

ctrl + r Starts a search through your history file.

Map and to do the same in .zsh:

  # search history on  key instead of paging previous commands:
  bindkey "\e[A" history-search-backward
  bindkey "\e[B" history-search-forward

Re: Ask HN: Best Unix Tricks?

#46

Pipes, grep and such come in handy in many applications. For example: ps -e | grep processname will grab the pid of any process so that you can subsequently kill it. sudo !! (run last command as sudo) is great too when you forget to type sudo on a regular basis like me.

will grab the pid of any process so that you can subsequently kill it

I like to use:

killall processname

Re: Ask HN: Best Unix Tricks?

#47
My most useful zsh functions:

  # a visual recursive list of all files and directories
  function tree()
  {
    find . | sed -e 's/[^\/]*\//|--/g' -e 's/-- |/    |/g' | $PAGER
  }

  # recursive text search (ack is also nifty)
  function f()
  {
    find . -name '*' | xargs grep -l $1
  }

  # recursive search and replace (ignoring hidden files and dirs), example: replace foo bar
  function replace()
  {
    find . \( ! -regex '.*/\..*' \) -type f | xargs perl -pi -e "s/$1/$2/g"
  }

Re: Ask HN: Best Unix Tricks?

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

Funny, we're pretty much opposite.

I see my 'window manager' as a neccesary evil to run browser and email clients, everything else runs in terminal windows.

Of course you could use 'pine' and 'lynx' but that gets old pretty quickly.

Re: Ask HN: Best Unix Tricks?

#50

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

could you give an example of the pushd thing you mentioned?

    $ pwd
    /home/jrockway
    $ pushd foo
    $ pwd
    /home/jrockway/foo
    $ popd
    $ pwd
    /home/jrockway
    $ popd
    error: directory stack empty
    $ pushd
    error: directory stack empty
    $ pushd foo
    $ pwd
    /home/jrockway/foo
    $ pushd
    /home/jrockway
    $ pushd 
    /home/jrockway/foo
    $ pushd
    /home/jrockway
    ...
Post reply on HN