Slashdot Mirror


(Useful) Stupid Unix Tricks?

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?

11 of 2,362 comments (clear)

  1. search and replace in files by cain · · Score: 3, Interesting

    Search and replace inplace in files, using perl:
    perl -pi -e "s/searchme/replaceme/g" *

    In all .cpp anh .h files:
    perl -pi -e "s/searchme/replaceme/g" `find . -name \*.cpp -o -name \*.h`

    Or if you're a bash person:
    perl -pi -e "s/searchme/replaceme/g" $(find . -name \*.cpp -o -name \*.h)

  2. Re:Well by FooAtWFU · · Score: 5, Interesting
    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.

    --
    The World Wide Web is dying. Soon, we shall have only the Internet.
  3. Re:grep -R by multipartmixed · · Score: 4, Interesting

    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*

    --

    Do daemons dream of electric sleep()?
  4. Re:rev by Chris+Pimlott · · Score: 4, Interesting

    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)

  5. Re:There is this part ... by infinite9 · · Score: 4, Interesting

    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

    --
    Disconnect your television. Do your own research. Draw your own conclusions. They're probably lying. Don't be a sheep.
  6. Re:Show attached block devices by mollymoo · · Score: 5, Interesting

    du -cks

    OK, it's not a trick or very obscure, but it is a useful set of flags and it spells the name of an animal. Which is cool, if you need to get out more. I need to get out more.

    --
    Chernobyl 'not a wildlife haven' - BBC News
  7. Re:Bah, subtlety: by Nathanbp · · Score: 5, Interesting

    :(){ :|:& };:

    :() defines a function named : with no arguments. { :|:& } is what the function does. :|: calls itself twice (with a pipe between the two), and the & at the end runs it in the background as a new process. The ; finishes off that command, then the last : runs the function, starting the fork bomb (as each run starts 2 new processes, each of which starts 2 new processes...).

  8. Re:There is this part ... by Anonymous Coward · · Score: 3, Interesting

    eject cdrom is great for locating poorly labeled machines too!

  9. Using the "right" interpreter with env by dwheeler · · Score: 4, Interesting

    If you begin your scripts with:

    #!/bin/env python

    and replace "python" with whatever your script interpreter is, then you can have the script automatically use whatever interpreter is first on your PATH. This is especially nice if you're "not sure where the interpreter executable is", e.g., it might not be in "/usr/bin" - so this helps portability. (The POSIX standards GUARANTEE that "env" is in /bin, so this is VERY portable.) This also makes it easy to try out new interpreters (load a test version's binaries in ~/python-beta, add that first to that PATH, and now the test version's interpreter is used.) This does have the extra cost of starting up /bin/env first, but often that's not a big deal.

    Yes, this is a bad idea if the attacker can control the PATH & this is security-relevant. But you can't securely run most interpreters directly anyway, so that's usually not relevant.

    --
    - David A. Wheeler (see my Secure Programming HOWTO)
  10. Re:Well by Technonotice_Dom · · Score: 5, Interesting

    Heck, I used that command in combination with 'at' to act as a makeshift alarm clock[..]

    You mentioned it only in passing, so I thought I'd draw a little more attention to it. The 'at' command is a really handy way to automate one-off tasks that many people seem to miss. The interface is neat too, understanding plain English time specifications.

    I've often seen people add a one-off task to a crontab, then try and forget to remove the entry once it's run!

  11. Re:Show attached block devices by Rotting · · Score: 3, Interesting

    sftp...

    Why use that when you can be funny and:

    cat file | ssh user@remotehost "cat > new_filename"

    or maybe tar up a directory on the fly and send it to the remote host for fun?

    tar cvzf - directory_to_tar | ssh user@remotehost "cat > tarfile.tar.gz"