16 ms·
Time-saving tips for Linux
- xelfer 16y agoThe 'learn bash, it's available everywhere' really bit me when I moved from being a Linux Sysadmin to an AIX Sysadmin. Only ksh is available in this case. It took quite a few months to get used to.
- wladimir 16y agoTell me about it. I've always worked with Linux, so was used to bash and its conveniences. Then I got a job where they use ancient Solaris systems with csh. Luckily, tcsh is generally installed which at least supports command line completion (after having to start it manually), otherwise I'd go mad.
- jdabney 16y agoSame thing happened to me a few years ago with Solaris and Irix. I used to use bash but the best shell available at work was tcsh. Now I have so much invested in tcsh that it is difficult to change back.
- DerekH 16y agoIt's great seeing all of these commands and shortcuts, and seeing that you know a lot of them. These never cease to be useful. Even though it seems simple to someone who knows Linux, you can always impress marketing/business people by flying through Linux with these helpful commands, shortcuts, and tips.
- mattdeboard 16y agoYou've got bigger issues if you've got marketing people looking over your shoulder while you're looking through grep's output. (And I doubt they care much about your Linux wizardry, that's why they're in marketing.)
- doctororange 16y agoThe learning curve of vim was pretty steep, but once over the hump you get some massive benefits. That said, I bet I'm only using 1% of the available commands. Going back to Windows makes you really appreciate the speed of bash, vim and grep.
- baha_man 16y ago"Going back to Windows makes you really appreciate the speed of bash, vim and grep." When I have to use Windows, one of the first things I always do is to install cygwin, so I can use bash, grep, etc.
- klochner 16y agoI used cygwin for several years and always felt like a leper - nothing ever works right out of the box.
- gkatsev 16y agoI never liked cygwin much, though used it for a bit. For a long time I was a fan of portable ubuntu (http://sourceforge.net/projects/portableubuntu/ http://sourceforge.net/projects/portableubuntu/) which allows you to run ubuntu as a service inside of windows. It was pretty good but it was still not the same as running linux natively or creating a VM that shares all the harddrives and you can ssh into by setting a static IP with your router.
- Goladus 16y agoThere's a lot that does, though, and it's a huge set of functionality that windows doesn't provide natively. Even something as simple as: ipconfig | grep IP Not possible in windows. Then there's the rest of the userland tools unix people take for granted like rsync, screen, diff, tr, wget, etc. The standard shells and scripting languages all work. Plus I always set up an ssh server to run on localhost, so that I can use putty as my terminal rather than the windows' crummy cmd.exe. Yes, ultimately it's still a windows box and cygwin's distribution definitely isn't on par with ubuntu or fedora. It's not going to be cutting edge and there will be difficulties integrating with hardware and the windows environment. It's generally not a good choice for running desktop apps or servers, and will probably require some extra work to set up a serious development environment. But if you have other reasons to use windows (Outlook calendar, games, etc.), cygwin makes it a LOT more tolerable.
- baha_man 16y ago"In bash, use Ctrl-R to search through command history." Or, add these lines to your .bashrc to use Ctrl-p and Ctrl-n: bind "\C-p":history-search-backward bind "\C-n":history-search-forward
- jrockway 16y agoWhy? C-r and C-s already do each of these, and C-p and C-n are for going back and forward in history. (Personally, I use M-p and M-n so that bash in the terminal works like Emacs.)
- eeperson 16y agoDoessn't that just do the same thing as the built-in Ctrl-r and Ctrl-s? Also, Ctrl-p and Ctrl-n are already used to cycle through previous commands.
- spudlyo 16y agoYou usually can't use C-s because it's bound to stop. $ stty stop undef
- jrockway 16y agoI don't think these tips are that great. Some random criticisms: ifconfig is deprecated (use "ip" instead). There is not much value in knowing vim if you know Emacs. If you want to edit something in your terminal, well, emacsclient -t to your Emacs session. Or use mg, which is a very fast and light Emacs workalike -- just enough for editing your /etc/apt/sources.list to get Emacs installed :)
- spudlyo 16y agoYou can also use nano, which has similar key bindings and is often installed by default, which will do in a pinch until you can install emacs.
- zerokyuu 16y agoAt first I thought you meant "use 'ipconfig' instead" and I was like, "Whaaaaat?!". Then I checked the man page for "ip". Thank you. It looks rather useful. I don't mess with routing or devices too much but I have in the past and only knew about ifconfig.
- jrussbowman 16y agoFunctionally as a sysadmin, I've heard ifconfig is deprecated before yet I've never used ip. just 'ifconfig' on the command line tells me everything I need to know about what interfaces are up, their status and basic info. 'ip' just gives me the standard syntax description. The power of the basic shell is that there's usually a few hundred ways to skin a cat. And just because there's documentation saying something is deprecated as often as not it's still so widely used that in practice, it's not.
- endlessvoid94 16y agoI found out about ack awhile back -- it's like grep on steroids.
- spudlyo 16y agoI'm a big fan of ack, I love the --output feature, which is super handy. Unfortunately it is hardly ever installed when I need it. It's considerably slower than the highly tuned GNU regex code, which only really matters when you're grepping through a ton of material.
- gnosis 16y agoCould you give an example of something useful one could do with ack's --output feature? The manual's a little light on the details. All it says is: --output=expr Output the evaluation of expr for each line What expression are they talking about? A regular expression? If so, how is that different from a normal grep?
- neild 16y agoA Perl expression. You could use this for sed-like activities: # Print included files. ack --output '$1' '#include <(.*)>' You could presumably do much trickier things as well, since the --output argument can contain any Perl expression.
- betterthangrep 16y agoThat's a great example. I just replaced it as the example in the Top 10 List on the front of betterthangrep.com. Thanks.
- deleted 16y ago[deleted]
- slowpoison 16y agoOn Ubuntu, the package name is ack-grep. So use: sudo -i aptitude install ack-grep Don't go for ack, which is some Kanji character converter.
- ilcavero 16y ago"Learn what the number inside something like ls(1) or perror(3) means." you got me there, I don't know what it means and now I'm curious and can't find it on google
- deleted 16y ago[deleted]
- ominous_prime 16y agoThey're the man page sections. When something appears in more than one category, you need to specify the section in the `man` command. $ man 1 ls $ man 3 perror $ man 1 printf $ man 3 printf http://en.wikipedia.org/wiki/Man_page http://en.wikipedia.org/wiki/Man_page
- ilcavero 16y agoThanks, quite frustrating not having any keyword that allows you to google something more info at: http://en.wikipedia.org/wiki/Man_page http://en.wikipedia.org/wiki/Man_page
- pieceofpeace 16y agoYou can also do this: $ man man
- jeberle 16y agoIt's odd that his 2nd tip is "Learn Vim" and then goes on to describe all the bash/readline keystrokes in Emacs mode. $ set -o vi
- callahad 16y agoI always found modal editing most valuable when editing and navigating through large bodies of text. I'm not quite sure that it makes sense on the command line, where you're rarely dealing with more than a few lines of text, and seldom need to rearrange those particular lines. Nevertheless, I switched to zsh on Sunday, and with it, vi keybindings. It's getting better, but it still doesn't feel quite right.
- tednaleid 16y agoI agree. I'm a vim user and never took to emacs, but the non-modal "emacs" keybindings feel more natural on the command line.
- spudlyo 16y agoDo you know anyone who actually does this? Even die-hard vi folks I know can't get used to modes on the command line.
- mcantor 16y agoI use vim almost pathologically, but I gave up on set -o vi. Since there's no visual indicator of which mode you're in, it's easy to make confusing mistakes and not the right feedback. It just wasn't worth it.
- cosbynator 16y agoYou can overcome the visual indicator (at least in zsh, not sure about bash): http://unix.stackexchange.com/questions/547/make-my-zsh-prompt-show-mode-in-vi-mode http://unix.stackexchange.com/questions/547/make-my-zsh-prom... I'm a recent convert. I don't like using control sequences for anything but screen management.
- known 16y agohttp://www.cyberciti.biz/faq/ http://www.cyberciti.biz/faq/ also have some good tips
- travisglines 16y agoWhen first stumbling my way around the shell, after learning about tab completion I discovered the magic wizardry that is command reverse/history lookup (cntrl-r start typing command ... see last command you entered that matches)
- maw 16y agoYep, very handy. After finding a match, you can hit C-r again to find earlier matches.
- travisglines 16y agoI think you can keep hitting the up arrow to find earlier matches too
- klochner 16y agoquora appears broken at the moment . . . I was going to suggest: in many shells (bash, tcsh, etc), !foo will execute the most recent command from your history that matches foo, for example: > grep -lots -of -options /complicated_regex/ ./some/path/* | /pipe/filter # realize you are in the wrong directory > cd /the/right/directory > !g and similarly, !! is the most recent command, which comes in handy for "sudo !!"[1] [1] http://xkcd.com/149/ http://xkcd.com/149/
- clvv 16y agoAnother trick that I don't see people mention(excuse me if somebody here already did) is the bash history completion: \esc \tab This will complete based on your history, argument wise. Very useful when tab completion itself doesn't satisfy you.
- there 16y agoTurn on sudo(8)'s "NOPASSWD" option for yourself (see sudoers(5)). don't do that. if anything, lock it down even more by enabling tty_tickets.
- wtallis 16y agoI wouldn't use NOPASSWD on a server or laptop, but for my personal workstation, security isn't as important as ease of use. You can't make good security guidelines without regard for context.
- nupark 16y agoWhy not? My servers have no passwords at all -- they either use SSH keys or kerberos. Once you have access to a local shell, the game is over. Acquiring root is easy, between local exploits or simply piggybacking on (or sniffing) a valid, passworded sudo login.
- there 16y agoOnce you have access to a local shell, the game is over. Acquiring root is easy, between local exploits uh, maybe you should work on that.
- nupark 16y agoWork on it how? Acquiring root locally is easy because local exploits are a dime a dozen, and once you have local access you can simply piggyback on valid authentication: alias sudo="sudo and do something evil instead" Once an attacker has local access, you've lost. It's a short leap from there to root. What's more, by requiring that users supply passwords to use sudo, you're practically guaranteeing that they will reuse passwords that thy use elsewhere, exposing your network to further compromise should those passwords be acquired from a compromised server.
- there 16y agodo you know how many web/ISP hosting providers are out there that give out non-root SSH access to their customers? or how many do shared PHP hosting that make it easy to run local commands on the server? do you really think all of those servers have been rooted? here are two free shell providers: http://www.devio.us/ http://www.devio.us/ http://sdf.lonestar.org/ http://sdf.lonestar.org/ if your operating system has no local user security, you should really upgrade it or switch to a different operating system.
- geoka9 16y agoIf you are halfway through typing a command but change your mind, hit Ctrl-A, add a # at the beginning to make it a comment, and press enter. You can then return to it later via command history. A faster way would be to hit Alt-# (does the same thing with just one key combo).
- steve-howard 16y agoctrl-u/ctrl-y is good if you only want to do that for one thing at a time.
- munchhausen 16y agoOne thing that's definitely missing from that list is the "undo" feature of bash which is bound to Ctrl-/ by default. It reverts the last edit you did on the command line. (The keybinding invokes the undo action also in emacs.)
- dreur 16y agoOne thing to say: http://cb.vu/unixtoolbox.xhtml http://cb.vu/unixtoolbox.xhtml It has everything I ever needed. When something is missing send patch.
- torpor 16y agoI've got one for vim users, its one of my favourites. I call it the "poor mans cscope" (btw, if you haven't learned about cscope + vim yet, you really should..) The following vim command: :cex system('find ./ -name "*.h" -exec grep -Hn some_string {} \;' .. is a 'poor mans cscope' in vim. It will exec the system command specified, which builds a list of all locations in all header files from the cwd containing the text "some_string". This list is then inserted into the quickfix list (the list of errors usually encountered by vim users when they type 'make' and get a list of compile errors), and can be navigated as such, using ":cl", ":cn", etc. I absolutely love this quickfix hack .. it makes finding things in a large tree of code very easy. Try it!
- bingaman 16y agoQuick ssh and and start screen: function s() { ssh $1 -t screen -dRR }