TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: Best Unix Tricks?

83 pointsby scottjacksonover 15 years ago
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 <i>be</i> that simple?<p>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.<p>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.

37 comments

jacquesmover 15 years ago
It's a great kill-ur unix tip but you should never fall for it:<p><pre><code> :(){ :|:&#38; }; </code></pre> Oh, the temptation... if you're thinking of cutting &#38; pasting that into the shell of your machine google for 'bash fork bomb' first, I've left off the final ':' to set it off.<p>Anyway, on a more serious note, after an installation procedure that was a lot of work, if you want to document the whole thing use:<p><pre><code> history | cut -d ' ' -f 5- </code></pre> To give you all the commands you executed in the last little while, cut &#38; paste that and you have a pretty good start for a how-to.<p>It's not a 'great killer unix tip', but it works for me.<p>Edit: Another one just came to mind, the ps command has a very useful option that is quite undervalued imo:<p><pre><code> ps ax --forest</code></pre>
评论 #810793 未加载
评论 #810667 未加载
评论 #810792 未加载
acgover 15 years ago
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.<p>For example:<p><pre><code> curl http://news.bbc.co.uk/ | pbcopy # copies a web-page to clipboard </code></pre> Depending on what you are doing it can be very useful:<p><pre><code> pbpaste | sort | pbcopy # sort the contents of the clipboard. </code></pre> Another useful feature of OS X is open, which uses the finder to open a file:<p><pre><code> open file.pdf open music.mp3</code></pre>
评论 #810823 未加载
评论 #811352 未加载
评论 #810844 未加载
评论 #810966 未加载
评论 #811479 未加载
silentbicycleover 15 years ago
Read man pages and "The Unix Programming Environment" by Kernighan and Pike. It's old, but conveys the design principles behind Unix, particularly how to write little tools that work well with all the others. "The Art of Unix Programming" is also good, despite Eric S. Raymond's getting on a soapbox all the time.<p>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.<p>For starters, learn to use: apropos, cron, the basics of sed and awk, the shebang, a <i>real</i> 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.<p>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.)
subbuover 15 years ago
cd - (Go back to previous working directory)<p>sudo !! (Run the last command as root)<p>:w !sudo tee % (Save a readonly file in VIM)<p>&#62; tmp.file (Empty a file)<p>cmd !$ (Run cmd with previous commands arguments)<p>They are just some of my favorites.
评论 #810821 未加载
评论 #810838 未加载
评论 #811280 未加载
评论 #811434 未加载
评论 #811022 未加载
评论 #811251 未加载
aseeonover 15 years ago
<a href="http://www.commandlinefu.com" rel="nofollow">http://www.commandlinefu.com</a>
评论 #810686 未加载
评论 #810645 未加载
jrockwayover 15 years ago
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.<p>$CDPATH is also amusing:<p><pre><code> $ mkdir /foo/bar/baz $ cd baz bash: cd: baz: no such file or directory $ export CDPATH="/foo/bar" $ cd baz $ pwd /foo/bar/baz</code></pre>
评论 #811246 未加载
评论 #810796 未加载
gcvover 15 years ago
1. Check out z: <a href="http://github.com/rupa/z" rel="nofollow">http://github.com/rupa/z</a>. It's a pretty nice productivity booster for flipping between different directories. It's quite easy to port to zsh, also.<p>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.<p>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.<p>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).<p>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.<p>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.
aarongoughover 15 years ago
One of the little things that it took me a while to find out about:<p><pre><code> echo '2^16' | bc 65536 </code></pre> 'bc' is a complete calculator language interpreter that also functions quite nicely as a simple calculator. If you need to do any heavy lifting check out: <a href="http://en.wikipedia.org/wiki/Bc_programming_language" rel="nofollow">http://en.wikipedia.org/wiki/Bc_programming_language</a>
评论 #813897 未加载
arghnonameover 15 years ago
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.<p>Make sure you read about job control in your shell. It helps understanding the why's and whats of some of the other tricks.<p>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.<p>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.<p>Edit:<p>Forgot to add, learn find (man find). This, in combo with xargs, is very, very useful.
sant0sk1over 15 years ago
I wrote a bash function yesterday which allows you to<p><pre><code> cd ... </code></pre> to move up two directories instead of having to<p><pre><code> cd ../.. </code></pre> Want to move up 4 directories?<p><pre><code> cd ..... </code></pre> Pretty useful, imo. Check it out<p><a href="http://blog.jerodsanto.net/2009/09/cd-up-up-up/" rel="nofollow">http://blog.jerodsanto.net/2009/09/cd-up-up-up/</a>
评论 #810886 未加载
评论 #810890 未加载
评论 #868420 未加载
apuover 15 years ago
readline is amazing, for bringing consistent shell-like behavior in a variety of programs, including the shell, python, mysql, etc.<p>You configure it using ~/.inputrc...here are the commands I use:<p><pre><code> set completion-ignore-case on </code></pre> Tab-completion will ignore case if it's not ambiguous...<p><pre><code> set show-all-if-ambiguous on </code></pre> ...but if it is ambiguous, this will give you a list if you hit tab again.<p><pre><code> Space: magic-space </code></pre> 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.<p><pre><code> "\e[A": history-search-backward "\e[B": history-search-forward </code></pre> 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.<p><pre><code> set editing-mode vi </code></pre> 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.
评论 #812149 未加载
dryicerxover 15 years ago
Using the .ssh configuration file to save host specific options (~/.ssh/config). Never again long ssh commands to connect to your frequently connecting list.<p><pre><code> 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 </code></pre> now on your command line, and the settings will automatically be applied.<p><pre><code> ssh home ssh 1-2-3-4.blah.amazonaws.something.com </code></pre> 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.
lallysinghover 15 years ago
Named bookmarks and a jump-to function:<p><pre><code> 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" ] &#38;&#38; [ "x$CONFIRM" != "xy" ] then return 0; fi fi echo -n "Description: " read DESC ln -s `pwd` ~/.bkmarks/$BKNAME echo "`pwd` : $BKNAME : $DESC : `date`" &#62;&#62; ~/.bookmarks } function go () { if [ -x ~/.bkmarks/$1 ] then pushd ~/.bkmarks/$1; cd `pwd -P`; else echo "~/.bkmarks/$1 not found"; fi }</code></pre>
vijaydevover 15 years ago
To copy/move/rename files with suffixes quickly:<p><pre><code> cp /home/foo/abc.cpp{,-old} </code></pre> equivalent to<p><pre><code> cp /home/foo/abc.cpp /home/foo/abc.cpp-old</code></pre>
评论 #811477 未加载
bohr99over 15 years ago
ctrl + r<p>Starts a search through your history file.
评论 #810677 未加载
评论 #811459 未加载
评论 #810859 未加载
评论 #811376 未加载
评论 #810808 未加载
decklinover 15 years ago
C-x C-e to open up the current command in $EDITOR. For previous commands, fc N M where N and M are numbers or search strings (opens the range of commands in your editor).
tfhover 15 years ago
<p><pre><code> set -o vi # vi like behaviour of the command line. </code></pre> Use "/" to search through the history, "y" to copy, "p" to paste etc..<p><pre><code> cat &#62; somefile.txt &#60;&#60;EOF # quickly creates a text file </code></pre> Type EOF in a new line to finish.
评论 #810644 未加载
fogusover 15 years ago
I use this to synch a directory with another:<p><pre><code> tar cf - * | (cd &#60;dest-dir&#62; ; tar xf - )</code></pre>
评论 #810856 未加载
评论 #811805 未加载
JoelMcCrackenover 15 years ago
screen is infinitely useful.
评论 #810879 未加载
评论 #811108 未加载
philhover 15 years ago
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).<p>Actually, I rather hope there's something better than strace for the latter. But it works.
评论 #811045 未加载
评论 #810938 未加载
papafover 15 years ago
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.
评论 #810872 未加载
rp2over 15 years ago
<p><pre><code> xargs -d '\n' </code></pre> as in<p><pre><code> find * -type f | egrep -i '\.(gif|jpg|png)$' | xargs -d '\n' identify </code></pre> I used to use find -print0 | xargs -0 but sometimes you want to insert filters in between or the file names aren't from find.
jimmybotover 15 years ago
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).<p>Smart history-based autocompletion, advanced tab completion, optional cd, simplified syntax, among other features.<p>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.<p>Also submitted it here: <a href="http://news.ycombinator.com/item?id=811113" rel="nofollow">http://news.ycombinator.com/item?id=811113</a>
apuover 15 years ago
Add this to your .bashrc to have much more intelligent tab-completion:<p><pre><code> case $- in *i*) [[ -f /etc/bash_completion ]] &#38;&#38; . /etc/bash_completion ;; esac </code></pre> In addition to the usual filename completions, this will do command-dependent completions. A small sampling:<p>ssh: tabs complete hostnames in your ~/.ssh/config file<p>/etc/init.d/*: tabs complete possible options for any script in your init.d directory (at least on debian/ubuntu)<p>most standard linux commands: type the command and then - and then tabs will show you the list of possible options for that command.<p>kill/renice/pkill: tabs show you list of process ids/names
uggedalover 15 years ago
My most useful zsh functions:<p><pre><code> # 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" }</code></pre>
评论 #810934 未加载
nuddedover 15 years ago
<p><pre><code> !!:s/sl/ls </code></pre> executes last command but replaces first occurence of sl with ls. (can also be written as ^sl^ls)<p><pre><code> !!:gs/sl/ls </code></pre> same as above, but now global replace
评论 #810635 未加载
评论 #810604 未加载
ianbishopover 15 years ago
Pipes, grep and such come in handy in many applications.<p>For example:<p>ps -e | grep processname<p>will grab the pid of any process so that you can subsequently kill it.<p>sudo !! (run last command as sudo) is great too when you forget to type sudo on a regular basis like me.
评论 #810866 未加载
评论 #811049 未加载
pbhjpbhjover 15 years ago
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.<p>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".<p>IIRC Ubuntu now defaults to disabling the "magic sysrq" key combinations so YMMV.
jdolinerover 15 years ago
grep -l textregex fileregex<p>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.
jefffosterover 15 years ago
GNU Global integrates well with the shell and allows you to do such neat stuff as "less -t func" to see the definition of func.
complingover 15 years ago
Process Substitution in bash<p>comm &#60;(sort file1) &#60;(sort file2) (comm file1 and file2 without temp files)
spahlover 15 years ago
I love the '-exec' flag for find
评论 #810851 未加载
评论 #810768 未加载
mattissover 15 years ago
Not really a trick, but something not everyone knows<p>appending &#38; to run as a background process<p>./some_app &#38;
mfukarover 15 years ago
man man
skwiddorover 15 years ago
<p><pre><code> import kremvax /net /net </code></pre> Plan9 doesn't need nat
juvennover 15 years ago
keep hacking on what not work, and diving into detail. I began my Linux life exactly one year ago, when I bought myself the 1st laptop. It's rewarding and joyfull
ZeroGravitasover 15 years ago
<p><pre><code> &#62; make me a sandwich what? make it yourself &#62; sudo !! okay</code></pre>