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?

61 of 2,362 comments (clear)

  1. rm -rf / by Anonymous Coward · · Score: 5, Funny

    rm -rf /

    1. Re:rm -rf / by acidreverb · · Score: 5, Insightful

      Mediocre minds think alike. Great minds are unique.

    2. Re:rm -rf / by orclevegam · · Score: 5, Funny

      rm -rf /

      wtf??? (do not try this at home)

      Really? What does it do? Think I'll try it and s
      CARRIER LOST

      --
      Curiosity was framed, Ignorance killed the cat.
    3. Re:rm -rf / by Anonymous Coward · · Score: 5, Funny

      OMG OMG What did I do. What did I do. I am so fired. I am so EFFIN fired.

    4. Re:rm -rf / by butalearner · · Score: 5, Funny

      I got something similar to these from some website that I have long forgotten: alias AvadaKedavra kill -9 alias Obliviate rm -rf alias Alohomora chmod -Rf ug+w alias Accio grep -Ir \!:1

    5. Re:rm -rf / by mr_mischief · · Score: 5, Funny

      cat /dev/urandom > /dev/dsp

    6. Re:rm -rf / by Anonymous Coward · · Score: 5, Funny

      sleep 8h; cat /dev/urandom > /dev/dsp

      That's my alarm clock.

    7. Re:rm -rf / by Anonymous Coward · · Score: 5, Funny

      OMG OMG What did I do. What did I do. I am so fired. I am so EFFIN fired.

      Ha, I didn't get fired when I did it. It was about ten years ago now, I had a linux dualboot setup on a test PC and didn't need the linux install any more.

      I thought, "I've always wanted to try rm -r /" so I did. About four seconds later it dawned, with an "oh shit" that I still had the dos/windows 3.11 partition still mounted read write.

      Fortunately, I didn't permanently lose anything between good backups and Norton Disk Doctor.

    8. Re:rm -rf / by ArhcAngel · · Score: 5, Funny

      I keep getting Windows cannot find 'rm' when I type that in. Am I doing it wrong?

      what the hell. I had karma to burn.

      --
      "A person is smart. People are dumb, panicky dangerous animals and you know it." - K
    9. Re:rm -rf / by Omega996 · · Score: 5, Insightful

      sudo is for ubuntu wannabes - real UNIX admins don't sudo - they su - .

    10. Re:rm -rf / by Omega996 · · Score: 5, Insightful

      if you haven't said "oh shit" while doing something as root, you haven't done UNIX administration in a busy production environment.

    11. Re:rm -rf / by Dtyst · · Score: 5, Funny
      I prefer the Russian roulette version:

      [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo "You live"

    12. Re:rm -rf / by geminidomino · · Score: 5, Funny

      Thank you.

      There's two kinds of real UNIX Admins.

      1. Those who have yelled "Oh Shit!"
      2. Those who will.

    13. Re:rm -rf / by subStance · · Score: 5, Funny

      /dev/random to the dsp device ? I prefer to the midi device, then it sounds like someone throwing a drumkit and grand piano down the stairs at the same time.

      --
      Servlet v2.4 container in a single 161KB jar file ? Try Winstone
    14. Re:rm -rf / by TheLink · · Score: 5, Funny

      I wonder if anyone has had problems with guiding people to uninstallin stuff like Asterisk over the phone ;).

      e.g.
      A: "Type rm -rf /etc/asterisk
      B: "OK"
      A: "Next..."
      B: "Wait it's not done yet"
      A: "?"
      A: "!"

      Seems like people should be more careful about product directory names ;).

      Don't call your directories stuff like "star" or "slashdot" if you ever might need to get people to remove them over the phone.

      --
    15. Re:rm -rf / by MikeDirnt69 · · Score: 5, Funny
      I prefer this one:

      $ touch woman

      But I always get this back:

      $ touch: woman: Permission denied

      :(

      --
      Am I eval()? - http://www.monst3r.com.br
    16. Re:rm -rf / by jsolan · · Score: 5, Funny

      puercopop@localhost ~ $ which which
      which: no which in (/usr/bin:/usr/local/bin:/usr/bin:/bin:/opt/bin:/usr/x86_64-pc-linux-gnu/gcc-bin/4.1.2:/usr/qt/3/bin:/usr/games/bin)

      Burn her anyway!

    17. Re:rm -rf / by 4D6963 · · Score: 5, Funny

      Mediocre minds think alike. Great minds are unique.

      My thoughts exactly.

      --
      You just got troll'd!
  2. Well by Anonymous Coward · · Score: 5, Funny

    Well.

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

    1. 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.
    2. 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!

  3. session-sharing with screen -x by FooAtWFU · · Score: 5, Insightful
    (used in my company for doing the agile/extreme "pair programming" think with a remote devloper, among other things).

    screen is awesome.

    --
    The World Wide Web is dying. Soon, we shall have only the Internet.
    1. Re:session-sharing with screen -x by mollymoo · · Score: 5, Funny

      In a similar vein, back in the days of floppies you could have endless fun ejecting other people's disks from Sun workstations. They put it in, you eject it. They put it in, you eject it. Repeat till you get bored or it looks like they're about to do a 'who'.

      --
      Chernobyl 'not a wildlife haven' - BBC News
    2. Re:session-sharing with screen -x by quarterbuck · · Score: 5, Informative

      eject is a useful tool, If you have a rack of servers all alike and you need to identify one of them . Some servers have blinking lights etc. but mine had no audio nor lights. But it had a CD tray.
      I simply put eject and "eject -t" in a loop and go look in the server room -- the hyperactive server is the one I was looking for.

      --
      http://slashdot.org/submission/1062723/Cheap-mobile-data-plan?art_pos=2
  4. There is this part ... by 140Mandak262Jamuna · · Score: 5, Funny

    ... 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.

    --
    sed -e 's/Chuck Norris/Rajnikant/g' joke > fact
    1. Re:There is this part ... by Forty+Two+Tenfold · · Score: 5, Funny

      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!

      --
      Upward mobility is a slippery slope - the higher you climb the more you show your ass.
    2. Re:There is this part ... by sydneyfong · · Score: 5, Funny

      True story.

      A friend and I help admin the computers in my (former) high school. Due to security the doors were locked during off hours, and I'm not a frequent helper so I don't have the keys, but my friend did.

      There was once when I was basically stranded in the computer lab, and my friend was in the server room (where the cell phone signals didn't reach). I don't know what he was doing at that time, but "walls" (on the linux machines) and "net sends" (to the Windows servers) didn't seem to work, so I ran a script to open and close the CD tray hoping to catch his attention.

      I got a message asking "wtf are you doing?" a few minutes later :)

      --
      Don't quote me on this.
  5. Tab by computersnstuff · · Score: 5, Informative

    I'm sure everyone at some point is surprised of tabbed completion.

    1. Re:Tab by Anonymous Coward · · Score: 5, Funny

      I'm sure everyone at some point is surprised of tabbed completion.

      Woah! Got any more?



      (yes, I'm being sarcastic)

    2. Re:Tab by Craig+Davison · · Score: 5, Informative

      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.

    3. Re:Tab by gEvil+(beta) · · Score: 5, Funny

      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?

      --
      This guy's the limit!
    4. Re:Tab by Bandman · · Score: 5, Funny

      Seriously, I was giving a presentation at a LUG meeting a few years ago, and during a break, some guys came up to me and said "We know you can't type that fast. How do you do that?"

  6. I never knew that command by PingXao · · Score: 5, Insightful

    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.

    1. Re:I never knew that command by PhilipPeake · · Score: 5, Insightful

      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.

  7. X-forwarding by mikeb · · Score: 5, Informative

    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.

    1. Re:X-forwarding by BigJClark · · Score: 5, Insightful


      ... or even funnier, is how long (as in decades) we've been able to do that.

      --

      Hi, I Boris. Hear fix bear, yes?
    2. Re:X-forwarding by Chris+Pimlott · · Score: 5, Informative

      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.

  8. Talk / DD / Mount by p14-lda · · Score: 5, Informative
    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.

  9. cd - by JustinOpinion · · Score: 5, Informative

    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!

  10. Job control. by Craig+Davison · · Score: 5, Informative

    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

  11. Bah, subtlety: by FooAtWFU · · Score: 5, Insightful

    :(){ :|:& };:

    --
    The World Wide Web is dying. Soon, we shall have only the Internet.
    1. Re:Bah, subtlety: by jkiol · · Score: 5, Funny

      @$**& well we know it works in cygwin too.

    2. 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...).

  12. Re:Find / Grep by Anonymous Coward · · Score: 5, Funny

    I have a theory that find + xargs + grep is Turing-complete. Can't prove it, but it feels right.

  13. Re:Show attached block devices by duguk · · Score: 5, Informative

    Some more common ones I've thought of:

    screen - too useful, run apps in a virtual console which you can attach, deattach and share

    cd `pwd -P` - Jump into the real directory (from a linked directory).

    history - use it with grep if you forgot what you did

    strings - just show the printable strings from a file

    tail and head - tail -f is a lifesaver

    sftp - i really shouldn't need to explain this.

    file - do magic stuff

    Hope that's some help.

  14. Share mouse and keyboard by pieleric · · Score: 5, Informative

    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 ;-)

  15. grep --color by krappie · · Score: 5, Informative

    grep --color

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

  16. lsof by pak9rabid · · Score: 5, Informative

    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.

  17. Re:Show attached block devices by aniefer · · Score: 5, Informative

    ctrl+r (in bash?): reverse incremental search through history.
    pushd/popd , change directory saving the old one on a stack.

  18. Re:Show attached block devices by cain · · Score: 5, Informative

    tail and head - tail -f is a lifesaver

    I use tail -F, which is the same as tail -f, but works on non-existent files. Useful when tailing log files from programs that start a new log file every time it runs. Using tail -F in this case, you can just leave tail running while you start and restart the program overwriting the log file.

  19. Shell history tricks by steveha · · Score: 5, Informative

    There are a whole bunch of "history" tricks, to recall old commands without using the mouse.

    When I started college, I studied the shell's man page until I knew them all. Some are so obscure I have forgotten them.

    Generally, these involve an '!' character in some way.

    Here are a few I use:

    !! # run again the last command that was run
    !9 # run again the command with history number 9
    !v # run again the last command that started with a 'v'
    !vi # run the last command that started with "vi"
    !?foo? # run the last command that had the string "foo" anywhere in it

    diff oldfile newfile
    mv !$ !^ # same as "mv newfile oldfile"
    # !$ is last arg of previous command, !^ is first arg

    ls foo bar baz
    rm -f !!* # same as "rm -f foo bar baz"
    # !!* repeats all arguments from previous command

    There are actually some baroque tricks that recall a previous command and perform a search-and-replace on it, but for anything that complicated I just recall the line and edit it. The baroque tricks would have been pretty darn cool back in the paper teletype days, though.

    By the way, the Bash shell can be configured to edit command lines using vi or Emacs commands. I described how to do it in an article I wrote for Linux Journal magazine. It's the last section, "vi or Emacs Mode in the Shell".

    http://www.linuxjournal.com/article/8361

    Oh, not exactly a history trick, but here's something I use all the time:
    ls -1 > /tmp/files
    vi /tmp/files # edit list to include just the files I want
    rm `cat /tmp/files`
    # `cmd` inserts the standard output from cmd into the command line as if you typed it

    ls -1 > /tmp/files
    vi /tmp/files
    # edit list to include just the files I want
    # now run this command: :%s+.*+mv & /some/directory/path/&+
    # save file and quit vi
    source /tmp/files

    This moves the chosen files to "/some/directory/path". The breakdown of the vi command is as so:
    : # invoke "ex mode" for search and replace command
    % # run the following command on every line of the file
    s # do a search and replace
    + # use a '+' for the command delimiter, so I won't have to backslash escape '/' chars in the path

    .* # all characters on the line
    + # end the match pattern, begin replace pattern
    # & refers to the match pattern, thus all chars on the line
    mv & /some/directory/path/& # replace "foo" with "mv foo /some/directory/path/foo"

    Takes less time to do it than to explain it!

    The above is perhaps overkill if all the files are going to the same place. It's great if you want to send some files one place, some to another, because you can just edit the destinations until it looks right.

    steveha

    --
    lf(1): it's like ls(1) but sorts filenames by extension, tersely
  20. Crashed My Laptop! by TheNinjaroach · · Score: 5, Funny
    I have never, ever seen a hard crash on my IBM Thinkpad T40. So I ran that command as root, looked at the output and thought "Huh that's neat." Thirty seconds later, my screens go black and I'm looking at a disoriented IBM POST screen, mostly black with a broken progress bar.

    My system booted up fine, so of course the first thing I wanted to do was make it happen again.

    `blockdev --report /dev/* | more `

    Thirty seconds after the output finished and I'm looking at the garbled POST screen again. My laptop finished booting, I ran the command a third time before coming to tell Slashdot and

    --
    I went to eat some animal crackers and the box said, "Do not eat if seal is broken." I opened the box and sure enough..
  21. Re:Show attached block devices by EvanED · · Score: 5, Informative

    Along that line are pushd and popd. pushd <dir> changes to the specified directory and pushes it onto a stack of directories; popd changes to the directory at the top of the stack and removes it. There are commands for manipulating the directory stack but I don't know or use them.

    With zsh, and I think with Bash as well, you can setopt AUTO_PUSHD and setopt PUSHD_SILENT and then cd behaves like pushd.

    (Both of these commands, along with cd -, work in the Windows command interpreter too.)

  22. 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
  23. Re:Show attached block devices by the_B0fh · · Score: 5, Funny

    Oh my ghod. This is considered informative? Who let all the PFYs in?

  24. Useful tricks. by ElizabethGreene · · Score: 5, Informative

    I can't live without svn. Svn is a revision control repository, usually used for source code. What makes it really powerful is that you can _easily_ have a history of everything that has changed in a file and when. On my systems, I keep /etc in svn, plus bind's zone files, plus all the non-image web content, and the "Network Documentation" folder.

    Second trick, rsync. I use it to backup my home directory to another box. Very nice when you go through a hard drive/year.

    Screen -x was my next pick, but somebody already mentioned it.

    "echo ProtocolKeepAlives 120 >> /.ssh/config" No more dropped ssh sessions because of stupid nat boxes.

    su -u Username -s .. become Username, but keep the current shell. Good for diagnosing permissions problems when the user has a /bin/false shell. (named/www-user/backup/etc).

    A little awk goes a long way. Not the big-bad-I-am-a-programming-language-awk, but the smaller-friendlier extract one or two columns of text from something awk. ex. awk '{print $2}' prints the thing in the second column. Add -F the field separator tool and it gets really useful.
    Better example. Here is a postfix log line.
          Nov 5 16:27:19 pdc postfix/smtpd[13601]: 92B3F499C25F: client=exprod5mx254.postini.com[64.18.0.49]
    Here is the awk to extract just the message id. awk -F': ' '{print $2}'

    And here is the "I didn't get this message your mailserver must have eaten it" disprover. It searches the maillog for every message from or to a given address and extracts the full email transaction for that message id.

    grep -i user@domain.com /var/log/maillog | grep smtpd | awk -F': ' '{print $2}' | sort -un > temp.fil && grep maillog -f temp.fil

    Next trick, back ticks. `` Back ticks substitute the output of a command within a command.
    Ex. Name a file after the date. echo "hi" > `date +%Y%M%d`.txt

    On the subject of dates. date -d'yesterday' or 'last week' or '-4 hours' can be handy.

    Last one. Loopback nat with Iptables, so you can access local hosts by their external ip. (Instead of setting up split dns.)
    iptables -I POSTROUTING -s $local_network -d $local_network -j SNAT --to $lan_interface

    My local network is 192.168.0.0/24 and the netfilter lan ip is 192.168.0.1, so that becomes...
    iptables -t nat -A POSTROUTING -d 192.168.0.0/24 -s 192.168.0.0/24 -j SNAT --to 192.168.0.1

    -ellie

  25. Re:Show attached block devices by ip_fired · · Score: 5, Informative

    I actually like less better than tail -f. If you less a file, and then hit SHIFT-F it will tail the file, but you can break out of it and scroll around and search for terms. Very handy while looking at log files.

    --
    Don't count your messages before they ACK.
  26. Re:Show attached block devices by duguk · · Score: 5, Funny

    On the contrary, shorter IS something to be proud of.

    Cool! Thanks! I'll tell my boyfriend that next time!

  27. Re:rm -f /lib/libc* by feargal · · Score: 5, Funny

    You want subtle?

    ln -f /bin/rm /usr/bin/diff

    --
    "A goldfish was his muse, eternally amused"
  28. Time warp by SchmellsAngel · · Score: 5, Informative

    cal 9 1752

    --
    We must repeat.
  29. Best Dilbert by theshowmecanuck · · Score: 5, Funny

    PHB: Do we have Eunuchs here?.. I heard it is very important for a company's IT department to have Eunuchs.
    Dilbert: I think you mean Unix. Yes we have a lot of Unix machines here.
    PHB: Oh... [pauses and thinks]... If the company nurse comes by, tell her I said, "never mind."

    --
    -- I ignore anonymous replies to my comments and postings.