Ask HN: Best Unix Tricks?
HN, what are your killer Unix tips? I've been using and learning about Unix both at university and at home for almost a year, and I was wondering -- what are the things that, when some wizard showed them to you, you wrote down as soon as you could so you wouldn't forget them? Is there some trick that saves you a buttload of time? Some script that makes your job way easier? Some command that made you smack your forehead in frustration when you found out it could be that simple?
Note: I'm personally looking for tips for working in bash (specifically OS X stuff if that makes any difference), but if there's some amazing thing that only works in zsh under Debian or something, feel free to suggest those too -- it's sure to be useful for someone.
I had a quick search for previous topics on this, but all I found was http://news.ycombinator.com/item?id=103725 which had a couple of badass things in the discussion.
106 comments
[ 2.9 ms ] story [ 193 ms ] threadAnyway, on a more serious note, after an installation procedure that was a lot of work, if you want to document the whole thing use:
To give you all the commands you executed in the last little while, cut & paste that and you have a pretty good start for a how-to.It's not a 'great killer unix tip', but it works for me.
Edit: Another one just came to mind, the ps command has a very useful option that is quite undervalued imo:
http://www.hiraeth.com/alan/tutorials/courses/Unix-Tools.pdf
and Powers / Peek' oreilly book "Unix Power tools" on google books
================================
IBM had a good series:
http://www.ibm.com/developerworks/linux/library/l-11sysadtip...
https://www.ibm.com/developerworks/linux/library/l-10sysadti...
http://www.linuxformat.co.uk/wiki/index.php/58_Cool_Hacks
http://www.ibm.com/developerworks/aix/library/au-productivit...
!$ refers to the arg to the last command, so you can write:
Starts a search through your history file.
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.
$CDPATH is also amusing:
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.
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.
I find myself using the first two on an almost hourly basis.
Efficient because additional exec's are removed, for the most part find can perform the unlink itself. Safer because a replacement version for rm cannot be inserted into the user's path.
Before -delete was added to find I always found perl faster:
or The last case does not actually call rm but is hard-wired to use unlink().It may still be faster.
It is much, much faster to pipe commands to "xargs". For instance:
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.
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'
Perhaps its emacs' fault but finding an easy to use key or key combo that isn't already in use is far too hard!
http://tmux.sourceforge.net/
For example:
Depending on what you are doing it can be very useful: Another useful feature of OS X is open, which uses the finder to open a file:The 4.4 BSD supplementary books (PSD, USD, SMM, etc.) are full of major documentation, with chapters on vi, awk, cc, yacc, gprof, etc. They're very old-school. (OpenBSD installs them with its default documentation, I think FreeBSD does as well.) Some parts will be obsolete, but the big ideas are the same. For example, it covers lex and yacc better in about ten pages each than the O'Reilly book could in twenty times that. Same with make, and several other major parts in the Unix toolchain.
For starters, learn to use: apropos, cron, the basics of sed and awk, the shebang, a real editor, screen or tmux, tar, gzip, and find. Read man pages, see where they lead you. Try to automate stuff, and go spelunking in the infrastructure for whatever Unix distro you use.
I also found that I learned a LOT by getting away from stuff that assumed Linux. Soooo much Linux newbie stuff is written by people who only know Linux (and may use GNOME for most things, really). You don't really learn Unix that way. Also, watch out for people who assume that bash is the standard shell. (A pet peeve of mine.)
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.
I like to use:
killall processname
However, the tar solution works better for large file sets, and unlike using a simple `cp -rfp`, hardlink inodes are preserved.
http://blog.jerodsanto.net/2009/09/cd-up-up-up/
Usage: up {x} where x is the number of directories you want to go up (e.g., 3)
function up() { local arg=${1:-1} local dir="" while [ $arg -gt 0 ]; do dir="../$dir" arg=$(($arg - 1)); done cd $dir >&/dev/null }
Actually, I rather hope there's something better than strace for the latter. But it works.
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.
print the names of all the files satisfying fileregex with text satisfying textregex. Nice if you need to find out where a function is defined.