Live data from Hacker News

Ask HN: Best Unix Tricks?

news.ycombinator.com

91–100 of 107 posts

Re: Ask HN: Best Unix Tricks?

#91

To copy/move/rename files with suffixes quickly: cp /home/foo/abc.cpp{,-old} equivalent to cp /home/foo/abc.cpp /home/foo/abc.cpp-old

Braces in general are great for all sorts of tasks. The general principle is that it duplicates arguments, with changes. Thus,

  foo/bar/{a,b,c}
expands out to

  foo/bar/a foo/bar/b foo/bar/c
and you can also use {1..9} to expand to all the numbers between 1 and 9 (inclusive).

Here are some ways I commonly use it:

- Create lots of subdirectories at once:

  mkdir -p test/{a,b,c}/{1..4}
- Rename/copy files (as in parent comment)

- Removing lots of randomly named files, with common elements:

  rm {a,d,f,gr}*
(this will remove all files starting with those prefixes...note that it will not delete other files starting with 'g', only those with 'gr')

Re: Ask HN: Best Unix Tricks?

#92
post #24

On OS X pbcopy and pbpaste are useful for working with clipboard data. Copy data from a web-page or whatever, manipulate it on the command-line to get what you want. Putting reasonably large files in the clipboard can be useful too. For example: curl http://news.bbc.co.uk/ | pbcopy # copies a web-page to clipboard Depending on what you are doing it can be very useful: pbpaste | sort | pbcopy # sort the contents of th…

open -a finder is a pretty handy way to open a finder window to current directory.

Re: Ask HN: Best Unix Tricks?

#93
readline is amazing, for bringing consistent shell-like behavior in a variety of programs, including the shell, python, mysql, etc.

You configure it using ~/.inputrc...here are the commands I use:

  set completion-ignore-case on
Tab-completion will ignore case if it's not ambiguous...

  set show-all-if-ambiguous on
...but if it is ambiguous, this will give you a list if you hit tab again.

  Space: magic-space
Magic space is key. When you press space, it will expand out any ! commands you have on the line. If you have none, it's just a normal space. This lets you be much more bold with your use of history commands, as you can see what it expands out to before you run it. Note that this can interfere a little within other environments, such as python.

  "\e[A": history-search-backward
  "\e[B": history-search-forward
These two set incremental history search. You can type part of the command you know is in your history, and then pressing up or down will cycle through them. E.g., if you're looking for that particular find command you did a few days ago, you can type 'find' and then hit up arrow until you find it.

  set editing-mode vi
Finally, not for everyone, but for us vi users, this is awesome. You get a lot of the standard vi functionality on the command line (and anywhere else you use readline, include mysql and python). This means esc + j/k to scroll up or down through history (far faster than arrow keys), using f/F/t/T commands, d/c/a/i ways to enter edit mode, even . and , work as expected, etc.

Re: Ask HN: Best Unix Tricks?

#94
Add this to your .bashrc to have much more intelligent tab-completion:

  case $- in
    *i*) [[ -f /etc/bash_completion ]] && . /etc/bash_completion ;;
  esac
In addition to the usual filename completions, this will do command-dependent completions. A small sampling:

ssh: tabs complete hostnames in your ~/.ssh/config file

/etc/init.d/*: tabs complete possible options for any script in your init.d directory (at least on debian/ubuntu)

most standard linux commands: type the command and then - and then tabs will show you the list of possible options for that command.

kill/renice/pkill: tabs show you list of process ids/names

Re: Ask HN: Best Unix Tricks?

#95
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…

...except, when I want to execute a command once per file. :-)

If I want this I have the choice between: '-exec sed -i s/a/b/g {} \;' in find or '| xargs -IX sed -i s/a/b/g X'

Re: Ask HN: Best Unix Tricks?

#97
post #93

readline is amazing, for bringing consistent shell-like behavior in a variety of programs, including the shell, python, mysql, etc. You configure it using ~/.inputrc...here are the commands I use: set completion-ignore-case on Tab-completion will ignore case if it's not ambiguous... set show-all-if-ambiguous on ...but if it is ambiguous, this will give you a list if you hit tab again. Space: magic-space Magic space i…

  set editing-mode vi
BTW, for those who want to use it occasionally in bash, vi editing mode can be enabled interactively by running

  set -o vi
And this command will revert it back to default ("emacs") editing mode:

  set -o emacs

Re: Ask HN: Best Unix Tricks?

#98
altgr-sysreq-r, then -e -i -s -u -b (spells busier backwards). This will do a safe shut down and reboot including sync, even sometimes when ctrl-alt-del won't work. Also you can use altgr-sysrq-k like ctrl-alt-bksp to kill the window manager. Try altgr-sysrq-h from a console to see a terse help message.

Note altgr is the right hand alt key, the left one should work too but I find the combos harder to do then, and sysrq is "system request" the same key as "print screen".

IIRC Ubuntu now defaults to disabling the "magic sysrq" key combinations so YMMV.

Re: Ask HN: Best Unix Tricks?

#100
post #49

screen is infinitely useful.

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.

as an emacs junkie I use backtick for my screen control. Works fine till you try to paste blocks of SQL containing loads of said backticks - easy way to fubar a screen session.

Perhaps its emacs' fault but finding an easy to use key or key combo that isn't already in use is far too hard!

Post reply on HN