Live data from Hacker News

Ask HN: Best Unix Tricks?

news.ycombinator.com

51–60 of 107 posts

Re: Ask HN: Best Unix Tricks?

#51

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/

In zsh, you (1) don't need to write a function, and (2) you don't need to type "cd" to switch directories.

  setopt auto_cd

  alias -g ...='../..'
  alias -g ....='../../..'
  alias -g .....='../../../..'
  alias -g ......='../../../../..'
Then you can just type "..." and go up two directory levels, and so on.

Re: Ask HN: Best Unix Tricks?

#53

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/

I have just an alias for this (without the cd) in my ~/.bashrc:

  alias ..='cd ..'
  alias ...='cd ../..'
  alias ....='cd ../../..'
  alias .....='cd ../../../../'

Re: Ask HN: Best Unix Tricks?

#54
post #35

I use this to synch a directory with another: tar cf - * | (cd ; tar xf - )

Also look at "rsync".

And I do use rsync when I want to do a differential synch.

However, the tar solution works better for large file sets, and unlike using a simple `cp -rfp`, hardlink inodes are preserved.

Re: Ask HN: Best Unix Tricks?

#55
1. Check out z: http://github.com/rupa/z. It's a pretty nice productivity booster for flipping between different directories. It's quite easy to port to zsh, also.

2. Learn to use the editor modes on your shell. If you use Emacs mode in zsh, for example, things like transient-mark mode work with all the standard Emacs keybindings, and you can easily copy and paste pieces of your command line. vi mode also supports similar bells and whistles.

3. In zsh, if you are typing a command and then realize you need to look up its man page, move your cursor back to the command and hit Esc-h (or Meta-h or Alt-h, depending on your terminal). Once you quit the man viewer, your command line will be restored.

4. In zsh, if your command line gets too unwieldy to edit in zle, just hit Esc-e (Meta-e). It'll let you edit the command line in your editor (be sure your EDITOR environment variable is set).

5. zsh has an awesome buffer stack feature. Imagine you're typing a long command (call it command A), then realize that you need to run something else first (call it command B). Don't Ctrl-u or delete anything; leave your current command alone, and hit Esc-q. You'll get a fresh prompt. Run B. Once B executes, zsh will restore the command line from A. You can do this as many times as you want.

Anyway --- go and read "From Bash to Z Shell, Conquering the Command Line" by Kiddle, Peek, and Stephenson. I've never seen a better book on using shells with maximum efficiency (and I'm still only learning). Just learning to use and extend the autocompletion features of zsh was invaluable to me.

Re: Ask HN: Best Unix Tricks?

#56
post #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 | xarg…

Perhaps Python specific but I use this a lot

  epy () { 
  	cmd="import $1 as a ; print   a.__file__.endswith('.pyc') and a.__file__[:-1] or a.__file__" 
  	file=$(/usr/bin/env python -c $cmd) 
  	echo $file
  	emacsclient --no-wait $file
  }

Re: Ask HN: Best Unix Tricks?

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

DTrace.

Re: Ask HN: Best Unix Tricks?

#58
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 safet…

Also the -n and -P options to xargs. So you would do something like

    $ find . -name 'blah*' |wc -l
    2000
    $ find . -name 'blah*' |xargs -n 250 -P 8 ./mangle
That'll spawn 8 copies of mangle, each with 250 blah files on its command line.

Re: Ask HN: Best Unix Tricks?

#59
post #53

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/

I have just an alias for this (without the cd) in my ~/.bashrc: alias ..='cd ..' alias ...='cd ../..' alias ....='cd ../../..' alias .....='cd ../../../../'

Yah that's a nice way to fix it as well, but I prefer to be able to continue using the "cd" command since it is burned into my brain.

Re: Ask HN: Best Unix Tricks?

#60
post #51

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/

In zsh, you (1) don't need to write a function, and (2) you don't need to type "cd" to switch directories. setopt auto_cd alias -g ...='../..' alias -g ....='../../..' alias -g .....='../../../..' alias -g ......='../../../../..' Then you can just type "..." and go up two directory levels, and so on.

That's pretty cool. I've shied away from zsh for some time as I'm very comfortable with bash, but a lot of evidence has been coming to me lately that makes me want to give it a go.
Post reply on HN