9 ms·
Favorite Unix Commands
- nickwoodhams 14y agoPlease comment if you have a favorite not on this list.
- asparagui 14y agoalias emacs="nano"
- deleted 14y ago[deleted]
- GrumpySimon 14y agoexport EDITOR=/usr/bin/nano
- zacharystern 14y agonooooooo!
- jlgreco 14y ago'tac'. It is 'cat', but backwards.
- SenorWilson 14y agoAre you some kind of genius?
- jlgreco 14y agoIf you enjoy 'tac', you might also enjoy 'sponge'. Spong 'soaks up' input an releases it all on one chunk so that you can do things like: cat foo | sponge foo (`cat foo > foo` does not work) That's neat and all, but what if you don't have sponge? Well, just use tac twice! cat foo | tac | tac > foo (Obviously this is rather wasteful ;))
- thristian 14y agoI cannot understand how this works. "cat foo > foo" breaks, because the shell truncates the file "foo" while it's setting up the redirection, before it launches "cat foo", and so cat has nothing to read. "cat foo | sponge foo" works because the shell is not responsible for writing to foo; it launches cat (which opens the file for reading) then launches sponge (which eventually opens the file for writing). I would thus expect "cat foo | tac | tac > foo" to fail in the same way as "cat foo > foo" because the shell is still going to open the file foo and truncate it, but experimentation shows that it does actually work. Is it because the shell launches each pipeline one at a time, so cat has read the file before "tac > foo" truncates it? Is it a race-condition or a corner-case?
- deleted 14y ago[deleted]
- jlgreco 14y agoThat's a good question. I had though it worked because the truncate would not actually happen until tac actually wrote something, but now that I look at it more I don't think that is actually the case. `cat > foo` immediately truncates foo before anything is written. Also `tac foo > foo` doesn't work, which discredits my hypothesis that tac's buffering was causing it. Furthermore `cat foo | cat > foo` does not work, but `cat foo | cat | cat > foo` does work. I suspect this behavior is actually a race condition of some sort. Edit: definetly a race condition, shown by larger files: % wc -l foo 1310720 foo % cat foo | cat | cat > foo % wc -l foo 16384 1
- thristian 14y agoI always get confused between "tac" and "rev", which is also "cat, but backwards" for a certain definition of "backwards".
- lelf 14y agodtrace :P
- codegeek 14y agoalias lsd="ls -ltrF | grep ^d" Helps me quickly list only directories. Any better alternatives ?
- gav 14y agoUsing `find` is nice to get just directories: $ find . -type d -maxdepth 1
- lneves 14y agofind . -type d edit: for the same output as your command you can use: dir -l
- williadc 14y agoDunno what "better" means, but this also works: find -maxdepth 1 -type d
- mttsn 14y agoseconded. what I have always used
- jfb 14y ago
- jfb 14y agotsort. It's pointless until you need it, but when you need it, it's totally awesome. less. Overkill, but SO MUCH BETTER THAN tail -f. Seriously. You can toggle "follow mode" without losing your place in the incoming stream.
- cjg_ 14y agoHm, can you start at the end of file without reading the entire file with less? Use case is tailing large logs.
- jfb 14y agoYes. It is CPU intensive (see auntie post above).
- ralph 14y agoYes, either type G when presented with the top of the file or pass +G on the command line. It will then read all the intervening file to calculate the line numbers, and tells you it's doing this and to interrupt it to stop. To prevent it doing this in the first place use the -n option. "less -n huge_file" and then G only reads the start and end of the file. Type G again to move to the latest end of the file if it's growing.
- Florin_Andrei 14y ago> less. Overkill, but SO MUCH BETTER THAN tail -f. Seriously. If you do this on my servers, I am going to find you, and then I am going to kill... um... your process. less with SHIFT-F takes a whole CPU core to run on a busy log file. tail -f takes almost nothing to run. Now combine that information with the reality of a whole bunch of devs who don't know it or don't care about it. :(
- mje__ 14y agomultitail[1] is also really nice for following multiple log files. It can do highlighting of lines based on regex's, and has built-in highlighting of a wide variety of log formats [1] http://www.vanheusden.com/multitail/features.php http://www.vanheusden.com/multitail/features.php
- cnvogel 14y agoWhen rewriting large amounts of ASCII text (or comments in Code), I really find "par" helpful. : Just use it in : : vi with your cursor in front of a badly wrapped block of text. : : Press !}par<enter> and the text will be : : piped to par : : which fixes up : : the : : formatting and keeps aligned frame markers : : intact magically. : : Just use it in vi with your cursor in front of a badly wrapped : : block of text. Press !}par<enter> and the text will be piped : : to par which fixes up the formatting and keeps aligned frame : : markers intact magically. : http://www.nicemice.net/par/ http://www.nicemice.net/par/ (or apt-get install par)
- satiani 14y agoOr you can just select the block of text and type gq. This will do what par does, and can be adjusted with vim's 'textwidth' and 'formatoptions' settings.
- cnvogel 14y agoIf you use tmux (or screen) you want to use byobu! https://launchpad.net/byobu https://launchpad.net/byobu https://www.google.de/search?q=byobu+tmux&tbm=isch https://www.google.de/search?q=byobu+tmux&tbm=isch It's a fancy statusbar that shows CPU speed, IP-address, hostname, ...
- vially 14y agoAnother option is tmux-powerline[1] [1] https://github.com/erikw/tmux-powerline https://github.com/erikw/tmux-powerline
- jsaxton86 14y agoYou can get a fancy status bar with screen, you just need a screenrc file: http://www.mbeckler.org/blog/2012/11/28/my-screenrc-file-circa-2012/ http://www.mbeckler.org/blog/2012/11/28/my-screenrc-file-cir...
- tkahn6 14y agoI have something similar in my .tmux.conf file Screenshot: http://i.imgur.com/u6JY0.png http://i.imgur.com/u6JY0.png Config file: https://github.com/tylerkahn/dotfiles/blob/master/.tmux.conf#L49 https://github.com/tylerkahn/dotfiles/blob/master/.tmux.conf... Works with OSX and Linux.
- zeraholladay 14y agoexport CDPATH=.:~/projects export PS1="$(uname -n)$ " alias ltr='ls -ltr' More of a procedure, always list file and edit the command before deleting it: $ ll file.txt -rw-r--r-- 1 zera staff 0 Jan 7 20:28 file.txt Ctrl-p, Ctrl-a, Ctrl-d, Ctrl-d, r,m $ rm file.txt So this is a sanity check to avoid this mistake: $ rm *.txt *Edit, one more: Ensure you overwrite a file (or aren't): $ cp -i ~/file.config /etc/
- mappu 14y agosl
- gshakir 14y agoGreat post! I think "lsof" should be there along with "netstat".
- deleted 14y ago[deleted]
- csmatt 14y agoI forgot about the DNS querying of wikipedia. Probably could've used that to entertain myself on my flights over the holidays since I'm pretty sure gogo inflight passes DNS through. Anyway, I'm a big fan of piping things into xargs. xxd is a terminal-based hex editor. It can convert both to and from hex to binary. I also use 'pgrep -lf 'partial_program_name' a lot in place of 'ps aux | grep partial_program_name'. 'pkill -9 partial_program_name' searches for and kills all processes matching the string.
- avar 14y agoIf they allow DNS out you can use their Internet for free via iodine: http://code.kryo.se/iodine/ http://code.kryo.se/iodine/ I run an iodine server for myself, you can get free Internet at quite a few loginpage WiFi routers with it in airports, cafes etc.
- hayksaakian 14y agointeresting that there's an android package. Could it turn your old android device into an HTTP tunnel?
- ragmondo 14y agossssssh ! When proxying over DNS becomes mainstream, it'll be shutdown !!
- amboar 14y ago+1 for xxd, great for debugging purposes e.g. analysing serial output from socat (also mentioned by another commentor below).
- drinchev 14y agofaucet 80 --in cat This is useful for creating a connection for receiving the input of what you get... e.g. running faucet 80 --in cat and later curl http://127.0.0.1:80 http://127.0.0.1:80 will deliver to you console : GET / HTTP/1.1 User-Agent: curl/7.21.4 (i686-pc-linux-gnu) libcurl/7.21.4 OpenSSL/0.9.8n zlib/1.2.5 libidn/1.19 Host: 127.0.0.1:900 Accept: /
- FreeFull 14y agoAlternatively you could do netcat -l -p 80
- cnvogel 14y agoJust some additional information: Faucet is part of "netpipes", which is available for most Unixoid operating systems. I guess it was inspiration to build "netcat". But netcat was a absolute terrible hack of a program, so "socat" (socket-cat?) was born. http://www.dest-unreach.org/socat/ http://www.dest-unreach.org/socat/ In case you need the functionality of bash's /dev/tcp [1] and you are pondering to install netcat or netpipes, do yourself a favor and just go for socat, it's insanely more powerful, read the manpage, read the examples! http://www.dest-unreach.org/socat/doc/socat.html#EXAMPLES http://www.dest-unreach.org/socat/doc/socat.html#EXAMPLES -- [1] but debian and ubuntu have decided to compile without it... https://bugs.launchpad.net/ubuntu/+source/bash/+bug/215034 https://bugs.launchpad.net/ubuntu/+source/bash/+bug/215034
- kungpoo 14y agoWhat a ridiculous fixed header. It's taking up 2/3 of my phone's screen.
- vacri 14y agoTry turning your phone into the portrait orientation.
- carlesfe 14y agoHere's my list, a very compact plaintext file: http://mmb.pcb.ub.es/~carlesfe/unix/tricks.txt http://mmb.pcb.ub.es/~carlesfe/unix/tricks.txt
- loudmax 14y agoI have this in my .bashrc: function lc() { if [[ "$#" -gt 1 ]]; then for DIR in "$@"; do echo -n "$DIR - " ; ls -AU1 $DIR | wc -l ; done ; else ls -AU1 "$@" | wc -l ; fi; } So, "lc /dir" will count the number of files in /dir and "lc /dir/*" will count the files in subdirectories of /dir. This is useful if you're working in an environment where you may have thousands of files in a directory, and a regular "ls -l" will lock your terminal while it eats your entire scrollback buffer.
- pooriaazimi 14y agoHere's an idea: always pipe ls throug a pager! If it's less than a page, it would just print the results (you have to set some flags on less that I don't remember). If it's more, then press d or /term-to-search :)
- crazydiamond 14y agoless -F Won't page if less than one screenful. zsh users can also do: < filename (less than character)
- Nick_C 14y agoThis might be quicker than firing up a shell script: > lc /dir" will count the number of files in /dir find /dir -type f -maxdepth 1 | wc -l > and "lc /dir/*" will count the files in subdirectories of /dir. find /dir -type f -mindepth 2 | wc -l
- SkippyZA 14y agoCtrl+l is my most used command. I need a clean terminal
- jdefr89 14y agodidn't know this hotkey existed.. nice
- Newky 14y agoI second this, I also find myself impulsively smashing enter multiple times sometimes between commands, if I am typing in a series of commands, its just so my brain can catch up with what I want to do.
- scott_s 14y agoHuh. I've always just called "clear".
- MikeCodeAwesome 14y agoSame here; and love that it doesn't clear the command line you're currently typing. Does anyone know if there's a key command for clearing the scroll buffer?
- chrismsnz 14y agoNice Ctrl+D for quick logout too.
- alphakappa 14y agoCmd + K on the OS X Terminal.
- robbles 14y agoHandy addition to the "mount | column -t" trick: You can use this within Vim to pretty-format text tables, initialization of variables, etc. Just highlight the lines in question in visual line mode (V) and type !column -t<CR> to pipe the lines through the column command.
- gailees 14y agoI think I might've just saved days of my life....thank you!
- nickwoodhams 14y agoGlad it helped you! :)
- amalakar 14y agoI find this site handy for clever commands: http://www.commandlinefu.com/commands/browse http://www.commandlinefu.com/commands/browse
- q_revert 14y agoa few nice ones here too, http://www.pixelbeat.org/cmdline.html http://www.pixelbeat.org/cmdline.html, http://www.pixelbeat.org/docs/linux_commands.html http://www.pixelbeat.org/docs/linux_commands.html always worth looking at these types of lists imo, whilst some of the commands mightn't really fit into your workflow or seem useful immediately, they're often exactly the snippets of information that can save hours at a later stage..
- overgun77 14y agoNot really a unix command, but a vi command that has been really useful, specially if you do lots of editing protected files and want to keep your custom vim configuration: !sudo tee %
- ralph 14y agoWhat vi mode is that entered in?
- emillon 14y agoNormal mode (:help :! for more information on this command).
- ralph 14y agoWhat benefit does :!sudo tee % have over doing it at the command line other than filling in the current filename for you? I suspect the original poster meant something like "gg!Gsudo tee %".
- AjithAntony 14y ago:w !sudo tee %
- sousousou 14y agoNeed to send the contents of the file to that command. Otherwise it effectively empties the file. (typo?) :%!sudo tee %
- vog 14y agoThere's a typo: The command is spelled "apropos" (not apropo).
- nickwoodhams 14y agoCorrected this typo. Thanks.
- donquix 14y agosudo !! Instant favorite
- AaronBBrown 14y agoI often alias `sudo` to `fucking` to help vent my frustrations $ rm -rf somedir rm: somedir/: Permission denied $ fucking !!
- prakashk 14y agoI have been known to use sudo when it is not appropriate to use the f-word ;)
- amboar 14y agoAlong the lines of magic shell variables, there's also '!$', which selects the last argument of the previous command. Often I'm looking at different aspects of the same file: $ file foo $ ls -lh !$ $ vim !$
- mixedbit 14y agoI don't find this one useful. [up-arrow] automatically inserts the last command, which gives a generic mechanism to correct the command and it requires no more typing than !!: [up-arrow][control-a]sudo [enter]
- t_lark 14y agogrep -r . "Some random debug msg"
- tlarkworthy 14y agogrep -r . "some random debug message" (searches for the passage recursively in all files from the directory it was executed, the main reason I like developing in linux more than windows)
- jlgreco 14y agoIf you are searching inside code specifically, you may like 'ack'. It does the same thing grep does, except it has preset 'excludes', is recursive by default, and the output is a bit prettier.
- tlarkworthy 14y agoOh thanks! I will give it a whirl
- lachenmayer 14y agoOr even 'ag' (the silver searcher). Like ack, but "better". https://github.com/ggreer/the_silver_searcher https://github.com/ggreer/the_silver_searcher
- crazydiamond 14y ago"better" or just faster since its written in C ? It is not a drop-in replacement for ack. e.g. there are some basic usages it does not support -- iirc counts (ag -c pattern).
- jlgreco 14y agoEh, faster is faster, does it matter why?
- crazydiamond 14y agoThe part about written in C was just added on. My point was "better or faster". If you say "faster is better" I'll have to agree :)
- aidos 14y agoIt took me a moment to figure out what was going on there (with the whole clippy thing). The bulk of this list is the top commands from commandlinefu which has been on HN before [0], [1]. You can even go meta and install a command to let you search commandlinefu [2]. [0] http://news.ycombinator.com/item?id=527486 http://news.ycombinator.com/item?id=527486 [1] http://news.ycombinator.com/item?id=3843373 http://news.ycombinator.com/item?id=3843373 [2] http://samirahmed.github.com/fu/ http://samirahmed.github.com/fu/
- akavel 14y agoI have a suspicion that it's actually a kind of targeted example-advertisement for the clippy thingo ;) and did somewhat work for me actually, I do now feel interested in learning more about the site
- marginalboy 14y agoOn my mobile device, it was nearly unusable...
- bsphil 14y agoMaximized on a 1080p monitor it was irritating with far too many internal scroll bars. Great list of commands though.
- DanBC 14y agoSingle user mode on an HPUX machine was, I think, the most useful to me. (http://www.unixhub.com/docs/hpux/hpux_boot.html http://www.unixhub.com/docs/hpux/hpux_boot.html) I wish I still had that machine, but I do not.
- RexRollman 14y agoI love playing with OSes and HPUX was one of the ones I didn't get to play with (not to mention Irix and AIX).
- re_todd 14y agoalias cd5='pushd .; cd ../../../../..' This lets me cd five directories at a time, and if I want to go back, I do a "popd"
- dchichkov 14y agomount -t overlayfs -o lowerdir=$HOME,upperdir=/tmp/tmpHOME overlayfs $HOME
- jlgreco 14y agoWhat is the purpose of this?
- dchichkov 14y agoVery useful, if you want to make a checkpoint (possibly for /), break things and then get back to the original state. If you'd do that for / , your own system would start behaving, like a system booted from LiveCD. And, after reboot, would return to the origanal state. Overlayfs is pretty cool actually. Allows you to overlay a lower directory with some other upper directory and mount the result somewhere. All the changes that you are making in the mounted directory would be visible to you, and would be stored in the upper directory. In the example above, after running this command, content of your $HOME would stop changing. But you still will be able to change it, with all the changed content going into tmpHome.
- dchichkov 14y agoAnd you can also use it on EC2, to reduce costs of EBS I/O.
- jcampbell1 14y agoI have never understood the love for `sudo !!`. I think `ctrl+pa sudo ` is faster, explicit, and more versatile.
- aidos 14y agoYou know, I never knew ctrl-p - that's a much nicer way of doing it.
- kami8845 14y agoI disagree. I think typing one more command "sudo !!" is much easier and straight-forward than doing 1. Ctrl+p. Go to the previous line in bash history. 2. Ctrl+a. Go to the beginning of the new line. 3. Type "sudo ". While you're typing your whole commandline shifts around as well. The latter mixes history commands, navigation commands and also modifies a previous command instead of just typing a new 7-letter command. It's also more in terms of keystrokes (though just barely).
- why-el 14y agoYou know writing out their function does not make them longer. ;) I agree with the parent because my left pinky is always on capsLock (my ctrl; I swapped caps with ctrl) so a ctrol ap is far easier than to go to shift + 1 twice(to type !). Again, this can be different in other keyboards/setups.
- kami8845 14y agoI just wanted to illustrate the increased complexity and different domains of the commands.
- fusiongyro 14y agoMy favorite thing about `sudo !!` is that when you IM it to your friend who's learning Unix they'll take that literally rather than realize they should type it into their terminal, leading to a hilarious teachable moment months later when they actually watch you using the command line.
- madao 14y agoAlt Sys Rq o - useful if you need to shut down a system and your kvm is not working. http://en.wikipedia.org/wiki/Magic_SysRq_key http://en.wikipedia.org/wiki/Magic_SysRq_key
- nitrogen 14y agoIt's probably a good idea to tErminate, kIll, Sync, and remoUnt-ro before hitting Off or reBoot.
- crazydiamond 14y agoOn zsh, "print -rC2" for printing some listing in 2 column format. The number of columns (C) can be specified. (print is a builtin). zsh's file globbing is awesome. Some simple ones: *(.) - only files *(/) - only dirs *(.m0) - files modified today *(.om[1,15]) - 15 recently modified files
- fusiongyro 14y agoRemove trash Emacs leaves lying around: $ find . -name \*~ -delete
- INTPenis 14y agoOne of my favorite aliases that wasn't listed. alias easy_search="curl -s http://pypi.python.org/simple/ http://pypi.python.org/simple/ | perl -ne 'print if s/<[^>]+>//g && $.>1'|grep"
- Zenst 14y agoin ksh set -o vi so I can use the vi editor to navigate the command line history.
- bcoates 14y agoI'm a fan of using cut or sed or awk and ending it with: | sort | uniq -c | sort -n -r | head to get a nice top 10 whatevers in the whoosit
- pajju 14y agoCheck fasd, it will blow your mind, be prepared! :) Fasd (pronounced similar to "fast") is a command-line productivity booster. I've been using this for a while, and trust me, its changed my command line workflow, and I wish, this should come as inbuilt for all POSIX shells! Here is how it works - If you use your shell to navigate and launch applications, fasd can help you do it more efficiently. With fasd, you can open files regardless of which directory you are in. Just with a few key strings, fasd can find a "frecent" file or directory and open it with command you specify. Below are some hypothetical situations, where you can type in the command on the left and fasd will "expand" your command into the right side. Pretty magic, huh? v def conf => vim /some/awkward/path/to/type/default.conf j abc => cd /hell/of/a/awkward/path/to/get/to/abcdef m movie => mplayer /whatever/whatever/whatever/awesome_movie.mp4 o eng paper => xdg-open /you/dont/remember/where/english_paper.pdf vim `f rc lo` => vim /etc/rc.local vim `f rc conf` => vim /etc/rc.conf Fasd offers quick access to files and directories for POSIX shells. It is inspired by tools like autojump, z and v. Fasd keeps track of files and directories you have accessed, so that you can quickly reference them in the command line. The name fasd comes from the default suggested aliases f(files), a(files/directories), s(show/search/select), d(directories). Fasd ranks files and directories by "frecency," that is, by both "frequency" and "recency." The term "frecency" was first coined by Mozilla and used in Firefox. Here is the Link - https://github.com/clvv/fasd https://github.com/clvv/fasd
- glazskunrukitis 14y agoI like those un- commands for easier archive extracting, e.g.: alias unbz2='tar xvjf' alias untar='tar xvfz'
- Erwin 14y agoFortunately GNU tar has gotten smarter. You can do just "tar xvf" now and it will do the right thing whether the archive is compressed or not. When creating the archive, you still have to use your favorite compression method (XZ aka LZMA, activated with J is even better, but slightly slower, than bzip).
- rbonvall 14y agoI always use dtrx (do the right extraction). Check out its features: http://brettcsmith.org/2007/dtrx/ http://brettcsmith.org/2007/dtrx/
- prakashk 14y agoI use atool (http://www.nongnu.org/atool/ http://www.nongnu.org/atool/), which is packaged in Debian and Ubuntu (and I am sure in other distributions as well). It supports all the formats I have encountered so far (tar, tar.gz, zip, rar, bz2 and others). It provides a few commands, two of which I use the most: als - list files in an archive aunpack - exatract files from an archive The best part is aunpack extracts files into a subdirectory (I hate polluting the current directory with files from a new archive which wasn't properly packaged to include a directory), and it does it only when necessary. Before I found it, I always used to create a temp directory, cd to it, unpack into it, and possibly move files around etc.
- ragmondo 14y agoI like the wikipedia DNS text lookup - I work in a "firewalled" environment where the unices are only allowed DNS queries. If only wikipedia could provide central switchboard numbers in their text fields as well ...
- gbog 14y agoesc-#, comments the current line ctrl-\, kills current process even xtail xtail, tails dirs tweaks in .inputrc to have up and down arrow do backward history search, like in ipython.
- jasonkostempski 14y agoMany years ago, before I ever used Linux in any serious fashion, I often used a password that ended with '!!'. One day I was playing around with setting up MySQL and was having a very hard time with something that seemed super simple. I don't remember exactly how I figured it out, I think I accidentally type my password at the wrong time in the command line and observed some odd behavior and, after hours, finally tracked it to the '!!' command. I decided to try a different root password for MySQL and it was smooth sailing from there. A few months later I was transferring a domain away from a small DNS hosting provider. After a week or so of waiting support told me they were having a hard time with my account and couldn't do what they needed to do to initiate the transfer. I don't have the support emails, must have been my old hotmail account, but something they said suggested they might be trying to run commands on my account with my password (I knew they stored it in plain text since it was in several emails from them and I knew they were Linux servers) and it reminded me about the '!!' issue I had with MySQL. I changed my password and they were able to move forward. I wish I had dug deeper into the issues at the time they occurred.
- tzury 14y agothat's the original http://www.commandlinefu.com/commands/browse/sort-by-votes http://www.commandlinefu.com/commands/browse/sort-by-votes