Live data from Hacker News

Ask HN: Best Unix Tricks?

news.ycombinator.com

71–80 of 107 posts

Re: Ask HN: Best Unix Tricks?

#72
post #67
post #49

Earlier quoted context omitted.

Yes, it is. I recommend remapping the standard C-a to C-z, as C-a acts as "jump to the beginning of line" in couple of shells, Emacs and Emacs clones.

I'm not sure that C-z is a great choice as it's often used for job control. I use C-o myself. I suppose you could always use C-z C-z if you wanted to suspend your current foreground process.

I settled with C-z as it is easy and fast to type with one hand. However I am occasionally too fast and hit C-z z instead of C-z C-z. 'fg' solves this.

Re: Ask HN: Best Unix Tricks?

#73
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.

s/pine/mutt/ && s/lynx/w3m/ : these get old much less quickly, though YMMV.

Re: Ask HN: Best Unix Tricks?

#75
Named bookmarks and a jump-to function:

  function bookmark () {
    echo -n "Name of bookmark (no spaces): "
    read BKNAME
    if [ -e ~/.bkmarks/$BKNAME ]
    then
      echo -n "Overwrite? "
      read CONFIRM
      if [ "x$CONFIRM" != "xyes" ] && [ "x$CONFIRM" != "xy" ]
      then
          return 0;
      fi
    fi
    echo -n "Description: "
    read DESC
    ln -s `pwd` ~/.bkmarks/$BKNAME
    echo "`pwd` : $BKNAME : $DESC : `date`" >> ~/.bookmarks
  }

  function go () {
    if [ -x ~/.bkmarks/$1 ]
      then pushd ~/.bkmarks/$1; cd `pwd -P`; 
      else echo "~/.bkmarks/$1 not found";
    fi
  }

Re: Ask HN: Best Unix Tricks?

#76
A lot of the tricks here are using the shell builtins. Pretty much all the nifty stuff you can do with that are in your shell man page. If you are using bash, try typing man bash. Read it. There is also stuff in there about scripting bash. This is worth knowing, because short one liners are very nice for moving a bunch of files around or whatever else. I use one liners every day.

Make sure you read about job control in your shell. It helps understanding the why's and whats of some of the other tricks.

Learn to use pipes. On the cli, I use Bash, sed, awk, and grep a lot, and with pipes you can do amazing things relative to the windows world. It's a very useful skill.

Learn a scripting language. I tend to use perl because I can pipe into it or break into a move involved script, whichever I prefer. I personally find Python less suited for this, but I've heard that Ruby is a good sysadmin language too. You don't have to learn how to be a grand wizard, but any serious unix person should know one. It helps greatly with productivity.

Edit:

Forgot to add, learn find (man find). This, in combo with xargs, is very, very useful.

Re: Ask HN: Best Unix Tricks?

#77
Using the .ssh configuration file to save host specific options (~/.ssh/config). Never again long ssh commands to connect to your frequently connecting list.

    Host home home.example.com
      HostName home.example.com
      Port 12345
      User dryicerx
      IdentityFile ~/.ssh/somekey.pem
      LocalForward 59001 localhost:5901 
      
    Host *amazonaws*
      User root
      IdentityFile ~/.ssh/yourawskey

now on your command line, and the settings will automatically be applied.

    ssh home  
    ssh 1-2-3-4.blah.amazonaws.something.com
     
I specially love the last one if you are working with EC2, as it catches any EC2 domain name and applies the key with the root user.

Re: Ask HN: Best Unix Tricks?

#78
It's not a bash tip, but if you want something that generally makes command line navigation easier without the need for heavy amounts of customization or memorization, you might consider using the Friendly interactive shell (Fish shell).

Smart history-based autocompletion, advanced tab completion, optional cd, simplified syntax, among other features.

Not as hackerish as some of the other tips mentioned here, but it's a much lower barrier to entry than the other commonly used shells out there.

Also submitted it here: http://news.ycombinator.com/item?id=811113

Re: Ask HN: Best Unix Tricks?

#80
post #36
post #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.

That should be cmd !*. !$ would only be the final argument to the previous command, which you can also insert with alt-. (and pressing alt-. again will cycle through final arguments to earlier commands). Adding :p to the end of a history expansion (like !$:p) will cause it not to be executed, but the expanded command line will be echoed and put in your history so you can check edit it.

zsh tab-expands history expansions, so you can start editing right there
Post reply on HN