Stories
Slash Boxes
Comments

News for nerds, stuff that matters

Slashdot Log In

Log In

Create Account  |  Retrieve Password

(Useful) Stupid Unix Tricks?

Journal written by *no comment* (239368) and posted by timothy on Wed Nov 05, 2008 03:48 PM
from the sed-is-underrated dept.

So the other day I messaged another admin from the console using the regular old 'write' command (as I've been doing for over 10 years). To my surprise he didn't know how to respond back to me (he had to call me on the phone) and had never even known you could do that. That got me thinking that there's probably lots of things like that, and likely things I've never heard of. What sorts of things do you take for granted as a natural part of Unix that other people are surprised at?

+ -
story

Related Stories

[+] (Useful) Stupid Vim Tricks? 702 comments
haroldag writes "I thoroughly enjoyed the recent post about Unix tricks, so I ask Slashdot vim users, what's out there? :Sex, :b#, marks, ctags. Any tricks worth sharing?"
[+] News: (Stupid) Useful Emacs Tricks? 412 comments
Count Fenring writes "Since the Vi version of this question was both interesting and popular, let's hear from the other end of the spectrum. What are your favorite tricks, macros, extensions, and techniques for any of the various Emacs? Myself, I like 'M-x dunnet' ;-)"
[+] (Useful) Stupid Regex Tricks? 516 comments
careysb writes to mention that in the same vein as '*nix tricks' and 'VIM tricks', it would be nice to see one on regular expressions and the programs that use them. What amazingly cool tricks have people discovered with respect to regular expressions in everyday life as a developer or power user?"
[+] (Useful) Stupid BlackBerry Tricks? 238 comments
Wolfger writes "Continuing the recent (useful) stupid theme: I've recently become a BlackBerry user, and I'm in love with the obvious(?) tricks, such as installing MidpSSH to access my home box remotely. But I'd like to know what more experienced Crackberry addicts can share."
This discussion has been archived. No new comments can be posted.
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More
Loading... please wait.
  • rm -rf / (Score:5, Funny)

    by Anonymous Coward on Wednesday November 05 2008, @03:50PM (#25648981)

    rm -rf /

  • Well (Score:5, Funny)

    by Anonymous Coward on Wednesday November 05 2008, @03:50PM (#25648993)

    Well.

    dd if=/dev/zero of=/dev/sda1

    • Re:Well (Score:5, Interesting)

      by FooAtWFU (699187) on Wednesday November 05 2008, @04:00PM (#25649233) Homepage
      Not quite the same, but in a similar vein, cat /dev/urandom > /dev/dsp

      Sometimes a quick white-noise machine is relaxing. Heck, I used that command in combination with 'at' to act as a makeshift alarm clock when I was just moving into my first apartment and had forgotten my only other electronic device with an alarm (my cell phone) at the office.

  • by FooAtWFU (699187) on Wednesday November 05 2008, @03:50PM (#25648995) Homepage
    (used in my company for doing the agile/extreme "pair programming" think with a remote devloper, among other things).

    screen is awesome.

  • by 140Mandak262Jamuna (970587) on Wednesday November 05 2008, @03:50PM (#25648997) Journal
    ... Generally people are surprised by the fact that you could type some strange incantations into a black window like awk grep etc and make the computer do things without touching the mouse. Yeah, some are surprised by that thing.
    • by Forty Two Tenfold (1134125) on Wednesday November 05 2008, @04:13PM (#25649571)

      I once made my friends' jaws hit the floor when I burned a cd for them - from console.

      And once I had this strange feeling that something was wrong with the CD drive of a machine I was working at in the console until I realized I was opening and closing the CD tray on a machine in another room!

    • by infinite9 (319274) on Wednesday November 05 2008, @04:42PM (#25650289)

      I do both microsoft and unix development. This has led to some interesting situations. I wrote a mathematical parser in c#/.net that could process math expressions at runtime using Regex to get tokens from the expression. The regex wasn't too bad. But after a code review, my pointy-haired manager made me comment each symbol in the regex. 40 lines of comments to describe 1 line of code.

      As a consultant, VI is my absolute favorite tool. Not on unix projects, on microsoft projects. It always happens eventually. Someone needs to modify a file in a way that screams for regex search with replace, but is a nightmare in visual studio or some other windowy editor. So I have them stand behind me while I write an long, arcane-looking regex line in VI. When I press enter, the entire file instantly morphs into exactly what they want. I can think of no better way to justify my exorbitant bill rate. lol

  • Tab (Score:5, Informative)

    by computersnstuff (799942) on Wednesday November 05 2008, @03:53PM (#25649053)
    I'm sure everyone at some point is surprised of tabbed completion.
    • Re:Tab (Score:5, Funny)

      by Anonymous Coward on Wednesday November 05 2008, @03:57PM (#25649179)
      I'm sure everyone at some point is surprised of tabbed completion.

      Woah! Got any more?



      (yes, I'm being sarcastic)
      • Re:Tab (Score:5, Insightful)

        by jcam2 (248062) on Wednesday November 05 2008, @04:14PM (#25649585) Homepage

        You'd be surprised how often I have seen experienced programmers manually type out long commands or directory paths, instead of using tab completion. Sometimes I have to restrain myself from ripping the keyboard from their hands and using tab to enter the path myself in a 10th of the time.

    • Re:Tab (Score:5, Informative)

      by Craig Davison (37723) on Wednesday November 05 2008, @04:05PM (#25649363)

      With bash, you can even get tab completion for hostnames. Try this:

      ssh user@l[tab]

      Everything after the @ is filled in from /etc/hosts.

    • Re:Tab (Score:5, Funny)

      by gEvil (beta) (945888) on Wednesday November 05 2008, @04:12PM (#25649541)
      I just tried using this in Word. Instead of finishing the word I was typing, it kept on moving the little "insertion line" thing to the right. I already filed a bug report, but do any of you have a quick fix?
  • by PingXao (153057) on Wednesday November 05 2008, @03:53PM (#25649059)

    And I've been administering Linux systems for awhile now. Step back for a moment and you'll find that "man pages" and "info" are actually a pretty awful way to distribute documentation. As a supplement they'd be fine, but as the main source of information on how to use many commands... not so much.

    • by PhilipPeake (711883) on Wednesday November 05 2008, @04:07PM (#25649423)

      This is only true because people write such terrible and incomplete manual pages.

      The original Bell Labs man pages completely described the system from the point of view of an administrator or user. The only better documentation was the source.

      The current blight of wimpy, inaccurate and incomplete man pages seems to originate from the GNU developers who insist on using the terrible "info" crap, writing huge volumes of text with no real content, and the tradition is continued by Linux developers who generally provide little or no man page documentation -- presumably in the hope that users of their software will be tempted to ask questions on various mailing lists where they can be ritually disemboweled for displaying such a lack of understanding and disturbing the peace of the cognoscenti who have much more important things to do than answer questions of mere users of their software.

  • X-forwarding (Score:5, Informative)

    by mikeb (6025) on Wednesday November 05 2008, @03:53PM (#25649079) Homepage

    I've seen Windows people go slack-jawed in astonishment as I ssh to the other side of the world and run X programs over forwarding.

    Some refuse to believe it, others shake their heads and walk away.

    • Re:X-forwarding (Score:5, Insightful)

      by BigJClark (1226554) on Wednesday November 05 2008, @03:58PM (#25649195)

      ... or even funnier, is how long (as in decades) we've been able to do that.
    • Re:X-forwarding (Score:5, Informative)

      by Chris Pimlott (16212) on Wednesday November 05 2008, @04:30PM (#25650003)

      You could easily have an entire Ask Slashdot just on ssh, perhaps the greatest unix command ever invented.

      One of it's many great uses is creating secure tunnels:

      ssh user@remotehost -L123:example.com:456

      Open a tunnel on your local machine, port 123, to example.com, port 456, via the remote host

      ssh -R lets you go in the opposite direction (tunnel from remote end to local end), but if your application supports SOCKS, it's even easier:

      ssh user@remotehost -D8080

      Creates a secure tunnel supporting the SOCKS protocol.

  • Talk / DD / Mount (Score:5, Informative)

    by p14-lda (517504) on Wednesday November 05 2008, @03:54PM (#25649105)
    People seem to be losing the ability to use all the older manual ways of doing things.

    On the older systems, talk was a great utility.

    dd, device duplicator / disk destroyer

    mount, what I can't have a desktop icon?

    also managing disk volumes and the old conventions of /opt, /u, /usr, /usr/local

    This new fangled Linux craze with all of the UI tools is feeding it. Redhat is training admins that are dependent on a given release of their enterprise software (which I am a huge fan of) but not teaching them how it works under the hood.

    How about slirp? scp?

    The one ray of hope seems to be a new generation hacking their bsd and linux based (iPhone/Android) phones and having fun in a somewhat embedded (but full blown) *nix environment.

  • cd - (Score:5, Informative)

    by JustinOpinion (1246824) on Wednesday November 05 2008, @03:57PM (#25649157)

    In terms of navigation directories efficiently, I find that "cd -" is often forgotten (changes directory to your previous directory). I personally find it very useful, and couldn't live without it!

  • One word: (Score:5, Funny)

    by MMC Monster (602931) on Wednesday November 05 2008, @03:57PM (#25649169)

    Showers

  • Job control. (Score:5, Informative)

    by Craig Davison (37723) on Wednesday November 05 2008, @03:59PM (#25649221)

    fg, bg, kill, Ctrl-Z, &. Learn it. Know it. Live it.

    Even if they do know about job control, I've seen people look for a background job with ps, and then kill it using the PID. In most shells you can just do kill %, e.g. kill %1

  • by James Youngman (3732) on Wednesday November 05 2008, @04:18PM (#25649699) Homepage

    Assuming you already know the simple stuff like how to use shell quotes correctly, what you can do with ps and top, ...

    1. Using awk '$3 ~ /foo/ { bar }' to grep just one column of a file
    2. reset
    3. find . -blah -exec quux \+
    4. Adding : to the front of complex commands you just typed but realise you don't want to execute yet so that they get into your shell history
    5. Meta-T in Bash for swapping arguments
    6. find . -printf X | wc -c for counting files (since find |wc -l would miscount files with newlines in the name)
    7. set, shift and implicit shell loops (for without in)
    8. "${foo:-bar}" and similar
    9. "${x%%.ext}.newext"
    10. comm -3 <(sort long) <(sort short)
    11. unalias rm
  • by pieleric (917714) on Wednesday November 05 2008, @04:22PM (#25649805) Homepage

    When I pop up with my laptop to discuss with a colleague, after a while I might do on their computer:
    xhost +mylaptopname

    and on my laptop I do:
    x2x thecomputername:0 -west

    Then suddenly my mouse can go over the two computers, my keyboard works on both as well, and I can even copy-paste between the two computers. It looks like the two computers got united. In a flash, newbies get a new idea of what means unix and X ;-)

  • by 44BSD (701309) on Wednesday November 05 2008, @04:39PM (#25650217)

    echo '[q]sa[ln0=aln256%Pln256/snlbx]sb3135071790101768542287578439snlbxq'|dc

  • grep --color (Score:5, Informative)

    by krappie (172561) on Wednesday November 05 2008, @04:39PM (#25650219)

    grep --color

    For some reason, many people are greatly surprised when they figure out that grep will highlight matches for them.

  • lsof (Score:5, Informative)

    by pak9rabid (1011935) on Wednesday November 05 2008, @04:44PM (#25650347)
    lsof is a LIFE SAVER for trying to find what's still using something in a mounted resource when trying to unmount something. For example:

    lsof /mnt/myMount

    That will list which processes have anything under /mnt/myMount open

    It's also useful to find who's accessing what device. For example, say you're trying to listen to an mp3 and Amarok bitches about the sound device not being available. In that case, you could do something like this (assuming you're using ALSA):

    lsof /dev/snd

    That will list what processes are accessing any of your ALSA sound devices.
    • Re:rev (Score:5, Funny)

      by genner (694963) on Wednesday November 05 2008, @03:58PM (#25649185)

      The rev command has got to be one of the most useless Unix commands I've ever come across. It's almost as if someone's first c program somehow got taken up as a part of standard Unix! Maybe in the days before sed and awk and perl it had some function in pipes that I can't grok, but nowadays other than making hints for video game websites I can't imagine what it's for.

      Unhackable encryption of course.

    • Re:rev (Score:5, Interesting)

      by Chris Pimlott (16212) on Wednesday November 05 2008, @04:14PM (#25649597)

      I sometimes use rev to sort text by the end of the line, not the first. This is often useful when comparing two similar file structures.

      For example:

      $ wc -l foo/* bar/*
            6 foo/dead.letter
          86 foo/xorg.conf
            6 bar/dead.letter
          54 bar/xorg.conf

      $ wc -l foo/* bar/* | rev | sort | rev
          86 foo/xorg.conf
          54 bar/xorg.conf
        152 total
            6 foo/dead.letter
            6 bar/dead.letter

      (Yes, I'm aware you can use sort -k to specify the sort key, but this is quicker and easier)

    • You're not giving echo an asterisk as a paratemer. You're giving the shell an asterisk, which it dutifully expands. echo (which in this case is a shell builtin, but it doesn't have to be then just echoes them back.

      This isn't some echo peculiarity. It works for anything, even commands that don't normally take files, or even with files that look like switches (conversely, if you want to treat all subsequent arguments as files, not switches, most programs have a '--' switch):


      $ ls
      a -l b c
      $ ls *
      -rw-r--r-- 1 marcansoft users 0 2008-11-05 21:58 a
      -rw-r--r-- 1 marcansoft users 0 2008-11-05 21:58 b
      -rw-r--r-- 1 marcansoft users 0 2008-11-05 21:58 c
      $ ls -- *
      a -l b c

      In the second example, ls sees "ls a -l b c" and takes -l as a switch instead of a filename.

    • by elgatozorbas (783538) on Wednesday November 05 2008, @04:43PM (#25650315)
      Maybe an ex-windows user who assumed "delete /bin" was the linux equivalent of "empty wastebasket" ?
      • Re:grep -R (Score:5, Interesting)

        by multipart/mixed (163409) on Wednesday November 05 2008, @04:08PM (#25649441)

        Hell, I think it's probably a GNU extension, because it's still not in Solaris.

        I think rgrep appeared around BSD 4.4, though.

        Oh well. I still surprise people with backticks. *sigh*

      • -exec as a test (Score:5, Informative)

        by Chris Pimlott (16212) on Wednesday November 05 2008, @04:09PM (#25649451)

        One great feature of find that many people are unaware of is that you can use -exec as a test, not just as an action. For example, this is equivalent to your command above:

        find . -exec grep -q {} \; -print

        The "-print" action is only executed if the -exec command returns success.

        You can do a lot of handy things with this. Here's a real-world example from earlier today. I wanted to change the mime-type of all the xml files in my svn repository from "application/xml" to "text/xml":

        find . -name \*.xml -exec sh -c "svn propget svn:mime-type {} | grep -q application/xml" \; -exec svn propset svn:mime-type text/xml {} \;

      • by Anonymous Coward on Wednesday November 05 2008, @04:09PM (#25649459)
        I have a theory that find + xargs + grep is Turing-complete. Can't prove it, but it feels right.
      • Re:A simple search (Score:5, Informative)

        by AKAImBatman (238306) * <(akaimbatman) (at) (gmail.com)> on Wednesday November 05 2008, @04:28PM (#25649953) Homepage Journal

        Xargs is much more fun with complex data processing. e.g.

        Convert all PSDs to PNGs:

        ls *.psd | cut -d . -f 1 | xargs -L1 -i convert {}.psd {}.png

        Parse out and sort column 2 from a semicolon delimited file:

        cat myfile.txt | cut -d \; -f 2 | sort > output.txt

        Oh, I almost forgot about one of my favorite tricks. Count the number of items:

        wc -l
        (paste the list into the window and then type CTRL-D)

        It even works when the list of items has oddities. e.g. I had a list where every other line was blank. So I needed to count n/2 the value. Except that one of the blank lines wouldn't copy, so I actually needed (n+1)/2.

        echo $(($((`wc -l`+1))/2))

        Want to make sure your sig is under 120 characters? Type "wc -c" in, paste it into your terminal, then press CTRL-D. Instant character count.

        Ah, all the fun stuff you can do with Unix tools.

    • by Mish (50810) on Wednesday November 05 2008, @04:35PM (#25650115)
      DISCLAIMER: Don't run this!
      I didn't think I needed to say this, but I just showed someone this and they thought it was a legitemately helpful command...